End of 'Classes'

This commit is contained in:
2020-03-03 18:37:04 -05:00
parent 8dcf7dd6bf
commit 01d373c5a7
7 changed files with 121 additions and 3 deletions

15
src/Book.kt Normal file
View File

@@ -0,0 +1,15 @@
open class Book(title: String, author: String) {
private var currentPage: Int = 1
open fun readPage() {
currentPage.inc()
}
}
class eBook(title: String, author: String, var format: String = "text"): Book(title, author) {
private var wordsRead = 0
override fun readPage() {
wordsRead += 250
}
}

View File

@@ -1,14 +1,16 @@
package aquarium 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 get() = width * height * length / 1000
set(value) { set(value) {
height = (value * 1000) / (width * length) height = (value * 1000) / (width * length)
} }
var water = volume * 0.9 open var water = volume * 0.9
constructor(numberOfFish: Int): this() { constructor(numberOfFish: Int): this() {
val water = numberOfFish * 2000 // cm3 val water = numberOfFish * 2000 // cm3
@@ -17,3 +19,14 @@ class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40)
} }
} }
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
View 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()
}

View 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) {}

View File

@@ -30,3 +30,10 @@ private fun buildAquarium() {
"height: ${myAquarium2.height}" "height: ${myAquarium2.height}"
) )
} }
fun makeFish() {
val shark = Shark()
val pleco = Plecostomus()
println("Shark ${shark.color} \n Plecostomus: ${pleco.color}")
}

38
src/spice/Spice.kt Normal file
View File

@@ -0,0 +1,38 @@
package spice
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF),
YELLOW(0xFFFF00);
}
sealed class Spice(val name: String, val spiciness: String = "mild", color: SpiceColor) : SpiceColor by color {
abstract fun prepareSpice()
}
interface Grinder {
fun grind()
}
interface SpiceColor {
val color: Color
}
object YellowSpiceColor : SpiceColor {
override val color = Color.YELLOW
}
data class SpiceContainer(val spice: Spice) {
val label = spice.name
}
class Curry(name: String, spiciness: String, color: SpiceColor = YellowSpiceColor):
Spice(name, spiciness, color), Grinder {
override fun grind() {
}
override fun prepareSpice() {
grind()
}
}

9
src/spice/main.kt Normal file
View File

@@ -0,0 +1,9 @@
package spice
fun main(args: Array<String>) {
val spiceCabinet = listOf(SpiceContainer(Curry("Yellow Curry", "mild")),
SpiceContainer(Curry("Red Curry", "medium")),
SpiceContainer(Curry("Green Curry", "spicy")))
for(element in spiceCabinet) println(element.label)
}