From 01d373c5a783c6cf7b2364bd5564ba2f32e8e76f Mon Sep 17 00:00:00 2001 From: Andrew Kemp Date: Tue, 3 Mar 2020 18:37:04 -0500 Subject: [PATCH] End of 'Classes' --- src/Book.kt | 15 ++++++++++ src/aquarium/Aquarium.kt | 19 +++++++++++-- src/aquarium/Fish.kt | 23 +++++++++++++++ src/aquarium/decorations/decorations.kt | 13 +++++++++ src/aquarium/main.kt | 7 +++++ src/spice/Spice.kt | 38 +++++++++++++++++++++++++ src/spice/main.kt | 9 ++++++ 7 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/Book.kt create mode 100644 src/aquarium/Fish.kt create mode 100644 src/aquarium/decorations/decorations.kt create mode 100644 src/spice/Spice.kt create mode 100644 src/spice/main.kt diff --git a/src/Book.kt b/src/Book.kt new file mode 100644 index 0000000..a1f7ffc --- /dev/null +++ b/src/Book.kt @@ -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 + } +} \ No newline at end of file diff --git a/src/aquarium/Aquarium.kt b/src/aquarium/Aquarium.kt index e65e4ab..89b7c5f 100644 --- a/src/aquarium/Aquarium.kt +++ b/src/aquarium/Aquarium.kt @@ -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) + } + } \ No newline at end of file diff --git a/src/aquarium/Fish.kt b/src/aquarium/Fish.kt new file mode 100644 index 0000000..d12cb9d --- /dev/null +++ b/src/aquarium/Fish.kt @@ -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() +} \ No newline at end of file diff --git a/src/aquarium/decorations/decorations.kt b/src/aquarium/decorations/decorations.kt new file mode 100644 index 0000000..fac899a --- /dev/null +++ b/src/aquarium/decorations/decorations.kt @@ -0,0 +1,13 @@ +package aquarium.decorations + +fun main(args: Array) { + makeDecorations() +} + +fun makeDecorations() { + val d1 = Decorations("granite") + val d2 = Decorations("slate") + val d3 = Decorations("slate") +} + +data class Decorations(val rocks: String) {} \ No newline at end of file diff --git a/src/aquarium/main.kt b/src/aquarium/main.kt index 47a880f..e7d33f6 100644 --- a/src/aquarium/main.kt +++ b/src/aquarium/main.kt @@ -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}") +} diff --git a/src/spice/Spice.kt b/src/spice/Spice.kt new file mode 100644 index 0000000..8cb45b4 --- /dev/null +++ b/src/spice/Spice.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/spice/main.kt b/src/spice/main.kt new file mode 100644 index 0000000..db52e23 --- /dev/null +++ b/src/spice/main.kt @@ -0,0 +1,9 @@ +package spice + +fun main(args: Array) { + 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) +} \ No newline at end of file