Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlin.math.PI
- import kotlin.math.sqrt
- fun main() {
- //Imprime instancias de SquareCabin,RoundHut y Round Tower
- printInstance(SquareCabin(3,10.0))
- printInstance(RoundHut(4,5))
- printInstanceRH(RoundHut(4,5))//Para imprimir calculateMaxCarpetSize()
- printInstance(RoundTower(6,9))
- printInstanceRH(RoundTower(4,5))//Para imprimir calculateMaxCarpetSize()
- }
- //Imprime cualquier instancia de Dwelling
- fun printInstance(casa:Dwelling){
- when (casa){
- is SquareCabin->println ("\nSquareCabin")
- is RoundTower->println ("\nRoundTower")
- is RoundHut ->println ("\nRoundHut")
- }
- with (casa) {
- println("============")
- println("Capacity: ${capacity}")
- println("Material: ${buildingMaterial}")
- println("Has room? ${hasRoom()}")
- println("Floor area: ${floorArea()}")
- }
- }
- //Solo para instancias de RoundHut
- fun printInstanceRH(casa:RoundHut){
- println ("Carpet Size:%.2f.format(${casa.calculateMaxCarpetSize()})")
- }
- //--------------------------------------------------------------------------
- //-------------- Clase Madre Dwelling-------------------------------------
- abstract class Dwelling(private var residents: Int) {
- abstract val buildingMaterial: String
- abstract val capacity: Int
- fun hasRoom(): Boolean {return residents < capacity}
- abstract fun floorArea(): Double
- }
- //-------------- SquareCabin hereda de Dwelling -----------------------
- class SquareCabin(residents: Int,val length: Double) : Dwelling(residents){
- override val buildingMaterial = "Wood"
- override val capacity = 6
- override fun floorArea(): Double {return length * length}
- }
- //---------------Round Hut hereda de Dwelling ---------------------------
- open class RoundHut(residents: Int,val radius :Int) : Dwelling(residents) {
- override val buildingMaterial = "Straw"
- override val capacity = 5
- override fun floorArea(): Double {return PI * radius * radius}
- fun calculateMaxCarpetSize(): Double {
- val diameter = 2 * radius //Calculo del diametro
- return sqrt(diameter * diameter / 2.0)
- }
- open fun algo (){println ("Test")}//Debug
- }
- //---------------Round Tower , hereda de RoundHut---------------------------
- class RoundTower(
- residents: Int,
- radius :Int,
- val floors: Int=10) : RoundHut(residents,radius) {
- override val buildingMaterial = "Stone"
- override val capacity = 4 * floors
- }
- //-------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement