Practice Time: Generic Class

 

Practice Time

Using type hierarchies with generic classes follows a pretty basic pattern that we introduced in the previous segment. There was a lot of material introducing generics, but basically, when you are building them, it boils down to the following steps:

  • Create a type/class hierarchy. The parent is non-specific and the sub-types/subclasses are specializations.
  • There is at least one shared property between the classes/types, and it has a different value depending on the subtype (otherwise, having the sub-types is pointless).
  • We then have a class that uses all the subtypes, and performs different actions depending on what the values of the subtype’s properties are.

Let’s put this into practice using building materials and a building that needs certain amounts of those materials.

  1. Create a new package and file and call them Buildings.
  2. Create a class BaseBuildingMaterial with a property numberNeeded that is set to 1. You always need 1 of the base material.
  3. Create two subclasses, Wood and Brick. For BaseBuildingMaterial you need 4 units of wood or 8 units of brick. Now you have a type hierarchy.
  4. Create a generic class Building that can take any building material as its argument, and only building materials.
  5. A building always requires 100 base materials. Add a property baseMaterialsNeeded and set it to 100.
  6. Add another property, actualMaterialsNeeded and use a one-line function to calculate this from numberNeeded of the passed-in material.
  7. Add a method build() that prints the type and number of materials needed.
  • Hint: Use reflection to get the class and simple name: instance::class.simpleName
  1. Create a main function and make a building using Wood.
  2. If you did this correctly, running main() will print something like “400 Wood required”

BaseBuildingMaterial Parent Class
// TODO 1L Create a new package and file and call them Buildings.
package Buildings

// TODO 2: Create a class BaseBuildingMaterial with a property numberNeeded that is set to 1.
// You always need 1 of the base material.
open class BaseBuildingMaterial(open val numberNeeded: Int = 1) {
}
Wood Child Class
package Buildings

// TODO 3.1: Create two subclasses, Wood.
// For BaseBuildingMaterial you need 4 units of wood
class Wood: BaseBuildingMaterial() {
override val numberNeeded = 4
}
Brick Child Class
package Buildings

// TODO 3.2: Create two subclasses, Brick.
// For BaseBuildingMaterial you need 8 units of brick.
// Now you have a type hierarchy.
class Brick : BaseBuildingMaterial() {
override val numberNeeded = 8
}
Building Generic Class
package Buildings

// TODO 4: Create a generic class Building that can take any building material as its argument,
// and only building materials.
class Building<T: BaseBuildingMaterial>(val buildingMaterial: T) {

// TODO 5: A building always requires 100 base materials. Add a property baseMaterialsNeeded
// and set it to 100
val baseMaterialsNeeded: Int = 100

// TODO 6: Add another property, actualMaterialsNeeded and
// use a one-line function to calculate this from numberNeeded of the passed-in material.
var actualMaterialsNeeded: Int = baseMaterialsNeeded * buildingMaterial.numberNeeded

// TODO 7: Add a method build() that prints the type and number of materials needed.
fun build(){
println("Number of materials needed: $actualMaterialsNeeded to build: $buildingMaterial")
}
}

Main.kt function
import Buildings.Brick
import Buildings.Building
import Buildings.Wood

// File: Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/24/2023
// Email: nolvertou@gmail.com
// Description: Practice Time: Using a generic class

// TODO 8: Create a main function and make a building using Wood and Brick.
fun main() {
Building(Wood()).build()
Building(Brick()).build()
}









Comments

Popular posts from this blog

Summary of Lifecycle State and Activity Lifecycle