dwelling new
Wed Nov 20 2024 18:41:19 GMT+0000 (Coordinated Universal Time)
Saved by
@signup1
package com.example.dwellings
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
open class Dwelling(
val name: String,
val capacity: Int
) {
open fun description(): String {
return "The $name can accommodate $capacity people."
}
}
class RoundHut(
capacity: Int,
private val radius: Double
) : Dwelling("Round Hut", capacity) {
fun floorArea(): Double {
return Math.PI * radius * radius
}
override fun description(): String {
return super.description() + " It has a floor area of ${"%.2f".format(floorArea())} square meters."
}
}
class SquareCabin(
capacity: Int,
private val sideLength: Double
) : Dwelling("Square Cabin", capacity) {
fun floorArea(): Double {
return sideLength * sideLength
}
override fun description(): String {
return super.description() + " It has a floor area of ${"%.2f".format(floorArea())} square meters."
}
}
class RoundTower(
capacity: Int,
private val radius: Double,
private val floors: Int
) : Dwelling("Round Tower", capacity) {
fun floorArea(): Double {
return Math.PI * radius * radius * floors
}
override fun description(): String {
return super.description() + " It has $floors floors and a total floor area of ${"%.2f".format(floorArea())} square meters."
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val roundHut = RoundHut(4, 5.0)
val squareCabin = SquareCabin(6, 4.0)
val roundTower = RoundTower(8, 3.5, 3)
val dwellings = listOf(roundHut, squareCabin, roundTower)
dwellings.forEach { dwelling ->
println(dwelling.description())
}
}
}
content_copyCOPY
Comments