CS 201: Data Structures (Winter 2017)

Lab02: Plant a Garden

1. Goals

To work out what it means to implement an interface, see one advantage of having interfaces in practice, and try out working with Lists.

2. Overview

In this lab, you'll be creating a simulation of a Garden. Specifically you'll:

You'll make your own Plant to put in the Garden. You can download the starter files for this lab here.

3. Implementing Plant

The files you downloaded include a Plant interface. Read through the interface; you can also take a look at the Javadoc for the interface here. Now, decide with your partner on a kind of Plant you want to make - I made a Carrot, so you should make something else.

Make a file where you'll implement your Plant (e.g., mine was Carrot.java). Recall that you need to tell the compiler you're implementing an interface; mine starts:

public class Carrot implements Plant { //Instance variables, constructor(s), methods go here }

When implementing an interface, I usually start by copying the entire body of the interface into the new file (e.g., copying all the method headers in Plant.java into Carrot.java). That way I know I have all the method headers I need. Then I change the methods to have bodies, but not yet to do anything, e.g.:

public String getName() { return null; }

Then, I compile everything. Note that you can compile everything in a directory with the command

javac *.java

*.java means everything the ends with .java. Now that I've made sure I've included everything I need to implement the interface, I start thinking through the design of my code: what instance variables will I need to store the state of the object and how do I want the object to behave. Before you start trying to code, make sure to brainstorm with your partner how you'll design your plant so that you're both on the same page. Then, you can start filling in code in the methods.

4. Creating a Garden

Now that you've implemented a Plant, you'll make a Garden that puts some plants in a list, simulates several days (including some watering of the plants), and then displays the plants at the end. Your garden should have a List that contains Plants; there should be at least one of the type of Plant that you implemented in the previous part, plus at least one Carrot (which I implemented). You don't have the code for Carrot, only the class file. However, you can see how to make a new Carrot from the Carrot javadoc. As you're doing this part, make sure you can explain why it's useful for your Garden class that both your plant and my Carrot implement the Plant interface. Your list variable should have the type List, but as when you've instantiated lists before, you'll need to choose an implementation. You can find some implementations by looking in the javadocs. Either LinkedList or ArrayList is a good choice.

Before you start coding, brainstorm how to design Garden. You can try using object oriented style by having your list of Plants be an instance variable and having methods like addPlant(Plant) and waterAllPlants. If you do, you will need to use new Garden() to create a new Garden object inside your main function, which happens to be written in the file Garden.java. If this confuses you, you can move your main function to a new file called, e.g., GardenDemo.java instead.

5. Saving Your Work

Upload your lab to Moodle at the end of class (make sure that both partner's names are at the top!). It's okay if you don't get all the way through the lab; just save what you have. I won't be grading this. Next week, we may take a look at a few people's cool ideas. If you do not want your work shown to the class, please talk to me.

6. Extensions

Once you get the basic simulation working, there are a number of extensions you can try. Pick one(s) that seem interesting to you or that you're not quite sure how to do yet:

This lab modified from one designed by Anna Rafferty.