package com.example.dwelling import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.example.dwelling.ui.theme.DwellingTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { DwellingApp() } } } open class Dwelling(val residents: Int) { open val buildingMaterial: String = "Generic Material" open val capacity: Int = 0 fun hasRoom(): Boolean { return residents < capacity } open fun description(): String { return "This dwelling is made of $buildingMaterial and has space for $capacity." } } class RoundHut(residents: Int) : Dwelling(residents) { override val buildingMaterial: String = "Straw" override val capacity: Int = 4 fun floorArea(radius: Double): Double { return Math.PI * radius * radius } override fun description(): String { return "This is a round hut made of $buildingMaterial with space for $capacity people." } } class SquareCabin(residents: Int) : Dwelling(residents) { override val buildingMaterial: String = "Wood" override val capacity: Int = 6 fun floorArea(length: Double): Double { return length * length } override fun description(): String { return "This is a square cabin made of $buildingMaterial with space for $capacity people." } } class RoundTower(residents: Int, val floors: Int) : Dwelling(residents) { override val capacity: Int = 4 * floors fun floorArea(radius: Double): Double { return Math.PI * radius * radius * floors } override fun description(): String { return "This is a round tower made of $buildingMaterial with $floors floors and space for $capacity people." } } @Composable fun DwellingApp() { val roundHut = RoundHut(3) val squareCabin = SquareCabin(5) val roundTower = RoundTower(4, 3) Column { Text( text = roundHut.description(), style = MaterialTheme.typography.bodyMedium ) Text( text = "Floor Area: ${roundHut.floorArea(4.5)}" ) Spacer(modifier = Modifier.height(16.dp)) Text( text = squareCabin.description() ) Text( text = "Floor Area: ${squareCabin.floorArea(5.0)}" ) Text( text = roundTower.description() ) Text( text = "Floor Area: ${roundTower.floorArea(4.0)}" ) } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter