Inheritance Lab
Set up
Follow the steps from the Scavenger Hunt to mount the COURSES drive. Make a folder InheritanceLab in your STUWORK/username folder and open it in VSCode for today’s labwork.
Download the starter code, unzip it, and drag it into your InheritanceLab folder.
I recommend you pull out your reference sheet as well.
Goals
The purpose of this lab is to explore what it means to work with an abstract super class, see an advantage of using inheritance, and get more practice working with Lists in Kotlin.
In this lab, you’ll be creating a simulation of a garden. In doing so, you will:
- instantiate some plants and put the plants in a
List - simulate several days, including watering the plants
- display the plants
Part A
First, we’ll do some planning and basic setup.
-
The starter files include a
Plantabstract class. Read through this class to get an idea for what methods are provided and what you will need to define. The files also include aCarrot.classfile, which I have already written and compiled, and which inherits and completes implementation of thePlantclass. -
Decide on two additional kinds of
Plants that you want to implement (notCarrot, that’s taken!). Make a file where you’ll implement yourPlant, for exampleEggplant.kt:class Eggplant : Plant() { // defining abstract instance variable override val species: String = "Eggplant" // abstract method implementation goes here } -
You are required to define at least the things marked
abstract, so define yourspeciesas well as copying over the signature ofgetStatus(). - Add in a stub body of the method, having it just return a default value (like
"") for the return type:override fun getStatus() { return "" } - Make sure everything compiles:
kotlinc Plant.kt Eggplant.kt
Part B
Now, start implementing!
-
Fill in the
getStatusmethod of yourPlant-implementing class. Get creative with the status of your plant depending on its water! Remember that you can define additional functions and access the super class’ variables withsuper. -
Create an instance of your plant in the
mainfunction ofGarden.kt. -
Compile and run
Garden.ktto make sure everything works and correct any errors. -
Finally, repeat with a second class inheriting from the
Plantabstract class.
Part C
Now, replace your code in main with code to create a garden!
- In
mainofGarden.kt, create aListofPlants:val myPlants : MutableList<Plant> = mutableListOf() -
Create at least one instance of each of your classes and put them in your list. There is a provided
Carrotclass that you should use, too, even without looking at its code; just know that it inherits fromPlanttoo. -
Test your code by printing each plant in the list, since the
toStringmethod is provided inPlant. Compile viakotlinc -cp . Garden.kt. (Don’t forget to import your classes at the top ofGarden.kt!) -
Add a
forloop that simulates several days of time elapsing, during which you should probably water your plants (make sure to recompile and run to test every change you make along the way!). -
Display the plants in the garden at the end of
main. - Reflect: why is it beneficial to use inheritance for
Plantwhen creating your garden simulation? How would it being an interface have changed things?
Submission
Submit your completed .kt files to the link on Moodle for an engagement credit. You should make sure to share whatever code you write in class with both partners via email. Remember that labs are optional to complete and remain open until the last day of classes, but are great practice.
Want more to do?
Once you have the basic simulation working, you can add some extensions! Try one out, and ask questions anywhere you have them.
-
Use object-oriented style to complete the full garden simulation (with more than two plants in a list, simulating several days and watering at least sometimes, then displaying all plants). Write down some notes on paper about how to design
Garden. You should have your list ofPlants be an instance variable and have methods likeaddPlant(plant: Plant)andwaterAllPlants(). Once you’ve designed it on paper, go ahead and code it up! -
Add a method in
Gardenthat takes the name of a plant and waters only plants of species:water(plantSpecies: String). -
Choose a random amount by which to vary the amount of water (e.g., maybe the sprinkler hits some plants more than others, or maybe the rainfall is lighter/heavier than expected). Try using a
Randomlibrary function likeRandom.nextInt()for this. -
Vary the behavior of the
Plantthat you implemented with an additional variable, such as the age of the plant. -
Play with inheritance – for example, you could have a
Tomatosuperclass andCherryTomato,HeirloomTomato, etc. to extend that class. -
Play with interfaces – you could have a
Containerinterface for plants that are in containers and so can be moved around. -
Read the plants to add from a file. For example, if you had
TomatoandEggplantclasses, your file could have lines saying either tomato or eggplant. For each line, you could add to the garden a new plant of the appropriate type.