Preview:
package com.example.myapplication



import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable

// Base Dwelling Class
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 people."
    }
}

// Round Hut Class
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."
    }
}

// Square Cabin Class
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."
    }
}

// Round Tower Class
class RoundTower(residents: Int, val floors: Int) : Dwelling(residents) {
    override val capacity: Int
        get() = 4 * floors

    fun floorArea(radius: Double): Double {
        return Math.PI * radius * radius * floors
    }

    override fun description(): String {
        return "This is a round tower with $floors floors, made of $buildingMaterial and has space for $capacity people."
    }
}

// Main Activity
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DwellingsTheme {
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    DwellingApp()
                }
            }
        }
    }

    @Composable
    fun DwellingApp() {
        val roundHut = RoundHut(3)
        val squareCabin = SquareCabin(2)
        val roundTower = RoundTower(4, 3)

        Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
            Text(text = roundHut.description(), style = MaterialTheme.typography.bodyMedium)
            Spacer(modifier = Modifier.height(8.dp))
            Text(text = "Floor Area: ${roundHut.floorArea(4.5)} m²", style = MaterialTheme.typography.bodyMedium)

            Spacer(modifier = Modifier.height(16.dp))

            Text(text = squareCabin.description(), style = MaterialTheme.typography.bodyMedium)
            Spacer(modifier = Modifier.height(8.dp))
            Text(text = "Floor Area: ${squareCabin.floorArea(5.0)} m²", style = MaterialTheme.typography.bodyMedium)

            Spacer(modifier = Modifier.height(16.dp))

            Text(text = roundTower.description(), style = MaterialTheme.typography.bodyMedium)
            Spacer(modifier = Modifier.height(8.dp))
            Text(text = "Floor Area: ${roundTower.floorArea(4.0)} m²", style = MaterialTheme.typography.bodyMedium)
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun DwellingAppPreview() {
        DwellingApp()
    }
}

// Theme
@Composable
fun DwellingsTheme(content: @Composable () -> Unit) {
    val colorScheme = lightColorScheme(
        primary = androidx.compose.ui.graphics.Color(0xFF6200EE),
        onPrimary = androidx.compose.ui.graphics.Color.White,
        secondary = androidx.compose.ui.graphics.Color(0xFF03DAC6),
        onSecondary = androidx.compose.ui.graphics.Color.Black,
    )
    MaterialTheme(
        colorScheme = colorScheme,
        typography = MaterialTheme.typography,
        content = content
    )
}
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