-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_4.scala
More file actions
65 lines (42 loc) · 1.21 KB
/
Copy pathtutorial_4.scala
File metadata and controls
65 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
object Tutorial_4 {
def main(args: Array[String]) = {
calcInterest();
patterMatching();
q3();
}
def calcInterest() = {
print("Enter a value: ");
val deposit: Double = scala.io.StdIn.readDouble();
val rate: Double = if (deposit <= 20000)
2;
else if (deposit <= 200000)
4;
else if (deposit <= 2000000)
3.5
else
6.5
val interest: Double = deposit*rate/100;
println(s"Interest of $deposit = $interest");
}
def patterMatching() = {
print("Enter a integer: ");
val num: Int = scala.io.StdIn.readInt();
num match
case x if x<=0 => println("Negative/Zero")
case y if y%2==0 => println("Even");
case z if z%2==1 => println("Odd");
}
def toUpper(str: String): String = str.toUpperCase();
def toLower(str: String): String = str.toLowerCase();
def formatNames(name: String)(callback: String => String) = callback(name);
def q3() = {
var name = "Benny";
println(formatNames(name)(toUpper));
name = "Niroshan";
println(name.substring(0,1) + formatNames(name.substring(1, 2))(toUpper) + name.substring(2));
name = "Saman";
println(formatNames(name)(toLower));
name = "Kumara";
println(name.substring(0,5) + formatNames(name.substring(5))(toUpper));
}
}