Skip to content

Instantly share code, notes, and snippets.

@xian xian/Kevmo.kt
Created Mar 10, 2019

Embed
What would you like to do?
import kotlin.math.PI
// declarations
interface ThingWithMass {
fun weightInKilograms(): Double
}
// create a class
open class Animal(var age: Int)
class Cat(age: Int) : Animal(age), ThingWithMass {
override fun weightInKilograms(): Double = 2.0 * age
}
class Dog(age: Int) : Animal(age)
// declare what a glass is
class Glass(var height: Double, var diameter: Double, var fullness: Double) : ThingWithMass {
override fun weightInKilograms(): Double = computeVolume() / 1000
fun computeVolume() = (diameter / 2) * (diameter / 2) * PI * height
}
fun kevmoMain() {
Cat(3)
val kitty = Cat(5)
var myGlass = Glass(3.5, 4.0, .3)
println("volume of the glass is ${myGlass.computeVolume()}")
val yourglass = Glass(6.0, 12.0, 0.7)
println("your glass contains ${yourglass.computeVolume()} milliliters")
val totalWeight = kitty.weightInKilograms() + myGlass.weightInKilograms() + yourglass.weightInKilograms()
println("The weightInKilograms of our cats and glasses is: ${totalWeight}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.