Problem 1: Complete the implementation of Calculator.scala
Problem 2: Elbonia has a simple sliding scale income tax scheme. Your tax rate is determined by your income according to the following table:
Income
|
Rate
|
< $20000
|
0%
|
< $30000
|
5%
|
< $40000
|
11%
|
< $60000
|
23%
|
< $100000
|
32%
|
>= $100000
|
50%
|
Implement a tax calculator for the Elbonians: TaxCalculator.scala
Your calculator should throw an "Income can't be negative" exception if the input is negative.
Problem 3: Complete ShapeMaker.scala. Here's a sample output:
Enter a positive integer: 10
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
*
**
***
****
*****
******
*******
********
*********
**********
*
***
*****
*******
*********
***********
***********
*********
*******
*****
***
*
Problem 4: Complete VectorCalculator.scala. Additional information about arrays in Scala can be found on the web. Here, for example
Problem 5: Complete the implementation of MatrixCalculator.scala.
Here's a sample output:
Enter a positive integer: 5
39 58 28 57 1
77 68 99 57 85
26 83 51 40 64
0 55 67 57 6
21 96 22 64 13
trace = 228
Problem 6: Rewrite the following Java method in Mystery.scala
import java.util.*;
public class Mystery {
public static void main(String args[]) {
Scanner kbd = new Scanner(System.in);
System.out.print("enter a positive integer: ");
Random r = new Random();
int k = kbd.nextInt();
for(inti = 0; i< 10; i++) {
System.out.println("trial " + i);
for(int j = 0; j < 100; j++) {
//int k = r.nextInt(100);
k = (k * k) % 100;
if (k < j/3) continue;
if (k < j/2) break;
System.out.println("k = " + k);
}
}
}
}
Hint: Use Scala's break blocks.
Problem 7: Assume the following declarations have been made:
var x = 10
lazy val y = 1 / 0
Without using Scala, what are the values (with their types) of the following expressions, if the expressions contain errors, write "error":
1. if (false) 3
2. if (false) if (true) 3 else 4 // dangling else
3. if (x = 10) true else false
4. if (true) 1 else 1/0 // conditional eval
5. if (if (true) false else true) true else false
6. y + 10
7. println("100") // tricky!
8. true || 3 == 1/0 // short circuit eval
9. false && (1 / 0 > 0) // short circuit eval
Now check your answers using the Scala interpreter.
Problem 8: Assume the following declarations have been made:
var x = 10
Without using Scala, what are the values (with their types) of the following expressions, if the expressions contain errors, write "error".
1. {1; 2; 3} + {4; 5; 6}
2. {1; 2; {3; 4; {5; 6} } }
3. if (false) {2; 3} else {4; {5; 6}} + {10; 20}
4. {10; 20} + if (false) {2; 3} else {4; {5; 6}}
5. {var x = 20; var y = x + 1; x + y}
6. // { var y = x + 1; var x = 5; var z = x + 1; x + y + z}
7. {var x = 3; var y = {var x = 9; x + 1}; x + y} // shadowning
8. {def tri(n: Int): Int = if (n == 0) 0 else n + tri(n - 1); tri(5) }
// recursive blocks
Now check your answers using the Scala interpreter. Ignore the warning messages, but not the error messages.
Attachment:- Assignment.rar