-
Notifications
You must be signed in to change notification settings - Fork 0
/
17_ClassAndInheritance.kt
93 lines (78 loc) · 2.36 KB
/
17_ClassAndInheritance.kt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import kotlin.math.PI
import kotlin.math.sqrt
fun main() {
val squareCabin = SquareCabin(6,50.0)
val roundHut = RoundHut(3, 10.0)
val roundTower = RoundTower(6,15.5)
with(squareCabin) {
println("\nSquare Cabin\n===========")
println("Capacity: ${capacity}")
println("Material: ${buildingMaterial}")
println("Has room? ${hasRoom()}")
println("Floor area: ${floorArea()}")
getRoom()
}
with(roundHut) {
println("\nRound Hut\n=========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Has room? ${hasRoom()}")
println("Floor area: ${floorArea()}")
getRoom()
println("Carpet Length: ${calculateMaxCarpetLength()}")
}
with(roundTower) {
println("\nRound Tower\n==========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Has room? ${hasRoom()}")
println("Floor area: ${floorArea()}")
getRoom()
println("Carpet Length: ${calculateMaxCarpetLength()}")
}
}
abstract class Dwelling(private var residents: Int) {
abstract val buildingMaterial: String
abstract val capacity: Int
abstract fun floorArea(): Double
fun hasRoom(): Boolean {
return residents<capacity
}
fun getRoom() {
if(hasRoom()) {
residents++
println("You got a room!")
}
else
println("Sorry, at capacity and no rooms left.")
}
}
class SquareCabin(resident: Int, val length: Double): Dwelling(resident) {
override val buildingMaterial = "Wood"
override val capacity = 6
override fun floorArea(): Double {
return length*length
}
}
open class RoundHut(
resident: Int,
val radius: Double): Dwelling(resident) {
override val buildingMaterial = "Straw"
override val capacity = 5
override fun floorArea(): Double {
return PI * radius * radius
}
fun calculateMaxCarpetLength(): Double {
return sqrt(2.0) * radius
}
}
class RoundTower(
resident: Int,
radius: Double,
val floors: Int = 2): RoundHut(resident,radius) {
override val buildingMaterial = "Stone"
override val capacity = 4 * floors
override fun floorArea(): Double {
return super.floorArea() * floors
}
}