End of 'Classes'
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package aquarium
|
||||
|
||||
class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40) {
|
||||
import kotlin.math.PI
|
||||
|
||||
var volume: Int
|
||||
open class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40) {
|
||||
|
||||
open var volume: Int
|
||||
get() = width * height * length / 1000
|
||||
set(value) {
|
||||
height = (value * 1000) / (width * length)
|
||||
}
|
||||
|
||||
var water = volume * 0.9
|
||||
open var water = volume * 0.9
|
||||
|
||||
constructor(numberOfFish: Int): this() {
|
||||
val water = numberOfFish * 2000 // cm3
|
||||
@@ -16,4 +18,15 @@ class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40)
|
||||
height = (tank / (length * width)).toInt()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TowerTank(): Aquarium() {
|
||||
override var water = volume * 0.8
|
||||
|
||||
override var volume: Int
|
||||
get() = (width * height * length / 1000 * PI).toInt()
|
||||
set(value) {
|
||||
height = (value * 1000) / (width * length)
|
||||
}
|
||||
|
||||
}
|
||||
23
src/aquarium/Fish.kt
Normal file
23
src/aquarium/Fish.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package aquarium
|
||||
|
||||
abstract class Fish {
|
||||
abstract val color: String
|
||||
}
|
||||
|
||||
class Shark: Fish(), FishAction {
|
||||
override val color = "grey"
|
||||
override fun eat() {
|
||||
println("hunt and eat fish")
|
||||
}
|
||||
}
|
||||
|
||||
class Plecostomus: Fish(), FishAction {
|
||||
override val color = "gold"
|
||||
override fun eat() {
|
||||
println("munch on algae")
|
||||
}
|
||||
}
|
||||
|
||||
interface FishAction {
|
||||
fun eat()
|
||||
}
|
||||
13
src/aquarium/decorations/decorations.kt
Normal file
13
src/aquarium/decorations/decorations.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package aquarium.decorations
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
makeDecorations()
|
||||
}
|
||||
|
||||
fun makeDecorations() {
|
||||
val d1 = Decorations("granite")
|
||||
val d2 = Decorations("slate")
|
||||
val d3 = Decorations("slate")
|
||||
}
|
||||
|
||||
data class Decorations(val rocks: String) {}
|
||||
@@ -30,3 +30,10 @@ private fun buildAquarium() {
|
||||
"height: ${myAquarium2.height}"
|
||||
)
|
||||
}
|
||||
|
||||
fun makeFish() {
|
||||
val shark = Shark()
|
||||
val pleco = Plecostomus()
|
||||
|
||||
println("Shark ${shark.color} \n Plecostomus: ${pleco.color}")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user