Skip to content

Commit ac9c80f

Browse files
committed
add Classes folder and project solution
1 parent ac44529 commit ac9c80f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

7-classes/Calculator.kt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Calculator(val name: String) {
2+
3+
init {
4+
println("$name's Calculator 🧮")
5+
}
6+
7+
fun add(a: Int, b: Int): Int {
8+
return a + b
9+
}
10+
11+
fun subtract(a: Int, b: Int): Int {
12+
return a - b
13+
}
14+
15+
fun multiply(a: Int, b: Int): Int {
16+
return a * b
17+
}
18+
19+
fun divide(a: Int, b: Int): Any {
20+
if (b == 0) {
21+
return "Error! Dividing by zero is not allowed."
22+
} else {
23+
return a / b
24+
}
25+
}
26+
27+
fun power(a: Int, b: Int): Int {
28+
var result = 1
29+
30+
for (i in 1..b) {
31+
result *= a
32+
}
33+
return result
34+
}
35+
}
36+
37+
fun main() {
38+
var myCalculator = Calculator("Codey")
39+
println(myCalculator.add(5, 7))
40+
println(myCalculator.subtract(45, 11))
41+
println(myCalculator.divide(8, 0))
42+
println(myCalculator.power(2, 1))
43+
}

0 commit comments

Comments
 (0)