Advertisement
AntonioVillanueva

Ejercicio Type checks and casts KOTLIN

Nov 19th, 2021
1,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.58 KB | None | 0 0
  1. import kotlin.math.PI
  2. import kotlin.math.sqrt
  3.  
  4. fun main() {  
  5.     //Imprime instancias de SquareCabin,RoundHut y Round Tower
  6.     printInstance(SquareCabin(3,10.0))
  7.    
  8.     printInstance(RoundHut(4,5))
  9.     printInstanceRH(RoundHut(4,5))//Para imprimir calculateMaxCarpetSize()
  10.    
  11.     printInstance(RoundTower(6,9))
  12.     printInstanceRH(RoundTower(4,5))//Para imprimir calculateMaxCarpetSize()    
  13. }
  14.  
  15. //Imprime cualquier instancia de Dwelling
  16. fun printInstance(casa:Dwelling){
  17.     when (casa){
  18.         is SquareCabin->println ("\nSquareCabin")
  19.         is RoundTower->println ("\nRoundTower")        
  20.         is RoundHut ->println ("\nRoundHut")      
  21.     }
  22.  
  23.     with (casa) {
  24.         println("============")
  25.         println("Capacity: ${capacity}")
  26.         println("Material: ${buildingMaterial}")
  27.         println("Has room? ${hasRoom()}")
  28.         println("Floor area: ${floorArea()}")
  29.     }
  30. }
  31.  
  32. //Solo para instancias de RoundHut
  33. fun printInstanceRH(casa:RoundHut){  
  34.     println ("Carpet Size:%.2f.format(${casa.calculateMaxCarpetSize()})")
  35. }
  36.  
  37. //--------------------------------------------------------------------------
  38. //--------------   Clase Madre Dwelling-------------------------------------
  39. abstract class Dwelling(private var residents: Int) {
  40.     abstract val buildingMaterial: String
  41.     abstract val capacity: Int
  42.     fun hasRoom(): Boolean {return residents < capacity}
  43.     abstract fun floorArea(): Double  
  44. }
  45. //--------------   SquareCabin    hereda de Dwelling -----------------------
  46. class SquareCabin(residents: Int,val length: Double) : Dwelling(residents){
  47.     override val buildingMaterial = "Wood"
  48.     override val capacity = 6  
  49.     override fun floorArea(): Double {return length * length}
  50. }
  51. //---------------Round Hut    hereda de Dwelling ---------------------------
  52. open class RoundHut(residents: Int,val radius :Int) : Dwelling(residents) {
  53.     override val buildingMaterial = "Straw"
  54.     override val capacity = 5  
  55.     override fun floorArea(): Double {return PI * radius * radius}  
  56.     fun calculateMaxCarpetSize(): Double {
  57.         val diameter = 2 * radius //Calculo del diametro
  58.         return sqrt(diameter * diameter / 2.0)
  59.     }
  60.     open fun algo (){println ("Test")}//Debug
  61. }
  62. //---------------Round Tower , hereda de RoundHut---------------------------
  63. class RoundTower(
  64.     residents: Int,
  65.     radius :Int,    
  66.     val floors: Int=10) : RoundHut(residents,radius) {
  67.     override val buildingMaterial = "Stone"
  68.     override val capacity = 4 * floors        
  69. }
  70. //-------------------------------------------------------------------------
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement