Day39 of #100DaysOfCode

#CodeTogether Day 39/100

Hii folks 🙌

Today I started Unit 2 of the Android Basic with Kotlin.

Unit 2: Layouts

Pathway 1: Get user input in an app - Part 1

Source: https://developer.android.com/courses/android-basics-kotlin/course

How to declare a class

If we want to declare a class, we just need to use the keyword class:

class MainActivity { }

Classes have a unique default constructor. If the class has no body, curly braces can be omitted.

class Empty

Parameters are written just after the name. Brackets are not required if the class doesn’t have any content:

class Person(name: String, surname: String)

And if they have:

class Person(val name: String, val surname: String) {
// ...
}

Class Inheritance

First, we will create an abstract Dwelling class, which will be our base class

Any class can be the base class of a class hierarchy or a parent of other classes.

abstract class Dwelling(private var residents: Int) {

abstract val buildingMaterial: String
abstract val capacity: Int

fun hasRoom(): Boolean {
return residents < capacity
}
}

then, we will move forward to create the subclasses and then modify the classes in the hierarchy to leverage the inheritance.

/**
* Program that implements classes for different kinds of dwellings.
* Shows how to:
* Create class hierarchy, variables and functions with inheritance,
* abstract class, overriding, and private vs. public variables.
*/

import kotlin.math.PI
import kotlin.math.sqrt

fun main() {
val squareCabin = SquareCabin(6, 50.0)
val roundHut = RoundHut(3, 10.0)
val roundTower = RoundTower(4, 15.5)

with(squareCabin) {
println("\nSquare Cabin\n============")
println("Capacity: ${capacity}")
println("Material: ${buildingMaterial}")
println("Floor area: ${floorArea()}")
}

with(roundHut) {
println("\nRound Hut\n=========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Floor area: ${floorArea()}")
println("Has room? ${hasRoom()}")
getRoom()
println("Has room? ${hasRoom()}")
getRoom()
println("Carpet size: ${calculateMaxCarpetSize()}")
}

with(roundTower) {
println("\nRound Tower\n==========")
println("Material: ${buildingMaterial}")
println("Capacity: ${capacity}")
println("Floor area: ${floorArea()}")
println("Carpet size: ${calculateMaxCarpetSize()}")
}
}


/**
* Defines properties common to all dwellings.
* All dwellings have floorspace,
* but its calculation is specific to the subclass.
* Checking and getting a room are implemented here
* because they are the same for all Dwelling subclasses.
*
* @param residents Current number of residents
*/
abstract class Dwelling(private var residents: Int) {
abstract val buildingMaterial: String
abstract val capacity: Int

/**
* Calculates the floor area of the dwelling.
* Implemented by subclasses where shape is determined.
*
* @return floor area
*/
abstract fun floorArea(): Double

/**
* Checks whether there is room for another resident.
*
* @return true if room available, false otherwise
*/
fun hasRoom(): Boolean {
return residents < capacity
}

/**
* Compares the capacity to the number of residents and
* if capacity is larger than number of residents,
* add resident by increasing the number of residents.
* Print the result.
*/
fun getRoom() {
if (capacity > residents) {
residents++
println("You got a room!")
} else {
println("Sorry, at capacity and no rooms left.")
}
}

}

/**
* A square cabin dwelling.
*
* @param residents Current number of residents
* @param length Length
*/
class SquareCabin(residents: Int, val length: Double) : Dwelling(residents) {
override val buildingMaterial = "Wood"
override val capacity = 6

/**
* Calculates floor area for a square dwelling.
*
* @return floor area
*/
override fun floorArea(): Double {
return length * length
}

}

/**
* Dwelling with a circular floorspace
*
* @param residents Current number of residents
* @param radius Radius
*/
open class RoundHut(
val residents: Int, val radius: Double) : Dwelling(residents) {

override val buildingMaterial = "Straw"
override val capacity = 4

/**
* Calculates floor area for a round dwelling.
*
* @return floor area
*/
override fun floorArea(): Double {
return PI * radius * radius
}

/**
* Calculates the max length for a square carpet
* that fits the circular floor.
*
* @return length of carpet
*/
fun calculateMaxCarpetSize(): Double {
val diameter = 2 * radius
return sqrt(diameter * diameter / 2)
}

}

/**
* Round tower with multiple stories.
*
* @param residents Current number of residents
* @param radius Radius
* @param floors Number of stories
*/
class RoundTower(
residents: Int,
radius: Double,
val floors: Int = 2) : RoundHut(residents, radius) {

override val buildingMaterial = "Stone"

// Capacity depends on the number of floors.
override val capacity = floors * 4

/**
* Calculates the total floor area for a tower dwelling
* with multiple stories.
*
* @return floor area
*/
override fun floorArea(): Double {
return super.floorArea() * floors
}
}

Today I Learned:

That is all for Day39 ✅

Thanks for reading, See you tomorrow!

--

--

--

https://www.linkedin.com/in/itskushagra

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why should mobile app developers profile their apps before they release them for public use?

Hacks to Save Queries in Microsoft Dynamics 365 Supply Chain Management.

Taro: Integrating with Slack

High Level AWS Route 53 (DNS)& Cloud Front (CDN) Overview

What are Metatags in HTML?🚀

Metatags,SEO and Responsive Website

Learning about Ruby Instance Methods

Scaling for Worlds

Domain Driven Design

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kushagra Kesav

Kushagra Kesav

https://www.linkedin.com/in/itskushagra

More from Medium

Day50 of #100DaysOfCode

Day77 of #100DaysOfCode

Day36 of #100DaysOfCode

Day46 of #100DaysOfCode