Practice Time: Creating an instance of an SimpleSpice class

 

Practice Time

Earlier, we created and filtered a list of spices. Spices are much better represented as objects than as simple strings. Because they are objects, we can perform different things with them - such as cooking.

To recap, let's make a simple Spice class. It doesn't do much, but it will serve as the starting point for the next practice.

  1. Create class, SimpleSpice.
  2. Let the class be a property with a String for the name of the spice, and a String for the level of spiciness.
  3. Set the name to curry and the spiciness to mild.
  4. Using a string for spiciness is nice for users, but not useful for calculations. Add a heat property to your class with a getter that returns a numeric value for spiciness. Use a value of 5 for mild.
  5. Create an instance of SimpleSpice and print out its name and heat.
// File:            Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/17/2023
// Email: nolvertou@gmail.com
// Description: Learning how to create an instance of a class

fun main() {
// TODO 5: Create an instance of SimpleSpice and print out its name and heat.
val simpleSpice = SimpleSpice()
println("Spice Name: ${simpleSpice.name}, Spiciness: ${simpleSpice.spiciness}, Level: ${simpleSpice.heat}")
}

// TODO 1: Create class, SimpleSpice.
class SimpleSpice{
// TODO 2: Let the class be a property with a String for the name of the spice, and a String for the level of spiciness.
// TODO 3: Set the name to curry and the spiciness to mild.
val name: String = "curry"
val spiciness: String = "mild"
// TODO 4: Add a heat property to your class with a getter that returns a numeric value for spiciness. Use a value of 5 for mild.
val heat: Int get() {return 5 }




}
Output:
Spice Name: curry, Spiciness: mild, Level: 5

Process finished with exit code 0





Comments

Popular posts from this blog