Interfaces Lab
Set up
Follow the steps from the Scavenger Hunt to mount the COURSES drive. Make a folder InterfacesLab
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 Interfaces Lab
folder.
I recommend you pull out your reference sheet as well.
Submission
You will be able to submit the completed lab individually on Moodle for an extra engagement. You should make sure to share whatever code you write in class with both partners via email.
Goals
The purpose of this lab is to explore what it means to implement an interface, see an advantage of using interfaces, and get more practice working with List
s 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
Plant
interface. Read through this interface to get an idea for what methods any implementing class needs to provide. -
Decide on two kinds of
Plant
s that you want to implement. Make a file where you’ll implement yourPlant
, for exampleEggplant.kt
:class Eggplant : Plant { // methods go here }
-
Copy over the entire body of the interface, including all method names and the comments.
- Add in stub bodies of the methods, having them just returning a default value (like
0
or""
) for their return type:override fun getName(): String { return "" }
- Make sure everything compiles:
kotlinc Plant.kt Eggplant.kt
Part B
Now, start implementing!
-
Fill in all of the methods of your
Plant
-implementing class. Get creative with the status of your plant depending on its water! -
Create an instance of your plant in the
main
function ofGarden.kt
. -
Compile and run
Garden.kt
to make sure everything works and correct any errors. -
Finally, repeat Exercise 2 with a second class implementing the
Plant
interface.
Part C
Now, replace your code in main
with code to create a garden!
-
In
main
ofGarden.kt
, create aList
ofPlant
s. There is a providedCarrot
class that you can use, too, even without looking at its code; just know that it implements thePlant
interface. -
Create at least one instance of each of your classes and put them in your list.
-
Test your code by printing the name of each plant in the list. Compile via
kotlinc -cp . Garden.kt
. (Don’t forget to importPlant
and your classes at the top ofGarden.kt
!) -
Add a
for
loop 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 have an interface for
Plant
when creating your garden simulation? How would inheritance have changed things? We’ll discuss as a class, so be ready to share your thoughts. -
Submit your completed
.kt
files to the link on Moodle for an engagement credit.
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 ofPlant
s 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
Garden
that takes the name of a plant and waters only plants with that name:water(plantName: 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
Random
library function likeRandom.nextInt()
for this. -
Vary the behavior of the
Plant
that you implemented with an additional variable, such as the age of the plant. -
Play with inheritance – for example, you could have a
Tomato
superclass andCherryTomato
,HeirloomTomato
, etc. to extend that class. -
Read the plants to add from a file. For example, if you had
Tomato
andEggplant
classes, 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.