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.Arrangement
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.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
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
open class Dwelling(val resident:Int){
open val buildingMaterial:String = "Genericmateril"
open val capacity:Int =0
fun hasRoom():Boolean{return resident<capacity}
open fun description():String{
return "This is dwelling is made of $buildingMaterial and has space for $capacity"
}
}
class roundhut(resident: Int):Dwelling(resident){
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 roundhut is made of $buildingMaterial and has space for $capacity"
}
}
class squareCabin(resident: Int):Dwelling(resident){
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 squareCabin is made of $buildingMaterial and has space for $capacity"
}
}
class roundtower(resident: Int,val floors:Int):Dwelling(resident){
override val buildingMaterial: String = "wood"
override val capacity: Int = 4 * floors
fun floorArea(radius:Double):Double{
return Math.PI*radius*radius*floors
}
override fun description(): String {
return "This is roundtower is made of $buildingMaterial and has space for $capacity"
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
DwellingApp()
}
}
}
@Composable
fun DwellingApp(){
val hut = roundhut(3)
val cabin = squareCabin(5)
val tower = roundtower(4,3)
Column(modifier = Modifier.fillMaxSize().padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
Text(text=hut.description())
Spacer(modifier = Modifier.height(8.dp))
Text(text="FloorArea:${hut.floorArea(4.5)}")
Spacer(modifier = Modifier.height(8.dp))
Text(text=cabin.description())
Spacer(modifier = Modifier.height(8.dp))
Text(text="FloorArea:${cabin.floorArea(4.5)}")
Spacer(modifier = Modifier.height(8.dp))
Text(text=tower.description())
Spacer(modifier = Modifier.height(8.dp))
Text(text="FloorArea:${tower.floorArea(4.5)}")
Spacer(modifier = Modifier.height(8.dp))
}
}
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