Skip to content

Instantly share code, notes, and snippets.

@xian
Created March 10, 2019 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xian/59b198a4e64ddfdbc5c8670dd14d4e59 to your computer and use it in GitHub Desktop.
Save xian/59b198a4e64ddfdbc5c8670dd14d4e59 to your computer and use it in GitHub Desktop.
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