CS 201: Data Structures (Winter 2018)

HW07: Zoo displayer reprise

Due: Monday, 02/12 at 22:00

1. Goals

To understand how to implement a List variant using links (specifically, a sorted linked list), and to see how testing up front can help you when writing code.

2. Setup

This is a pair programming assignment. Check Moodle for new partner pairings.

The code that you write for this assignment will build on your ZooDisplayer code and use the SortedList ADT, a variation of the List ADT. The starter files contain:

You should not make any changes to SortedList or EzImage.

You will build on top of ZooDisplayer and ZooAnimal files that you or your partner made for hw03. (If neither of you have a (close to) working version of hw03, please email or talk to me and I'll help you.) Place a copy of these files in the unzipped folder.

3. Specification

There are four parts to this assignment: modifying ZooAnimal to be Comparable, writing testing code for your implementation of SortedList, implementing SortedList, and revising your ZooDisplayer to make use of your SortedList implementation. You should work through each part in order.

Part A: Making ZooAnimal Comparable

In this assignment, you'll be putting ZooAnimals in a SortedList. Rather than having to sort the list after adding all the animals, the animals are always added in sorted order. That means the SortedList will need a way to compare two ZooAnimals. You've seen an example of a class that implements the Comparable interface: String implements this interface. The Comparable interface includes a single method, compareTo(T o). You use this method as follows:
myZooAnimal.compareTo(otherZooAnimal)
This call should return a negative number if myZooAnimal comes before otherZooAnimal in sorted order, 0 if myZooAnimal and otherZooAnimal are equal in the sorted order, and a positive number if myZooAnimal comes after otherZooAnimal in sorted order.

Part B: Writing testing code

In Part C of the assignment, you'll be implementing a SortedLinkedList that stores ZooAnimals. Your list will always be sorted such that add(ZooAnimal newItem) places the item into its appropriate place in the list to maintain sorted order. You'll be implementing the interface SortedList. Take a moment and look at this interface. It's very similar to List, except for the way that add works and the fact that you can't replace an item or add an item at a specific position. (Why not?)

You'll be implementing all of the methods in SortedList, except the optional iterator().

If you write your tests well, it will help you to see when you've correctly implemented the SortedLinkedList in part C. Once you've finished making sure your tests check for the appropriate behaviour and that all tests pass for CarlSortedList<ZooAnimal>, you should switch out CarlSortedList<ZooAnimal> for SortedLinkedList. If you end up implementing iterator(), you should also write tests for that method.

In the past, there was a definite correlation with having good testing code in Part B and having a correct implementation in Part C. Please take this part seriously!

Part C: Implementing SortedLinkedList

You will write a class SortedLinkedList that implements all of the methods in the SortedList interface.

Start off by copying all of the method signatures from SortedList into SortedLinkedList. For the void methods, you don't need to put any code into the method bodies yet; for the methods that return something, just have them return some default value (e.g., false, -1, or null, depending on the type you need to return). You should now be able to compile your code, and run your SortedListTester. All or most of your tests will fail, but you're now set up to start implementing!

Write a private static class Node that store ZooAnimals. Even though your textbook does not make Nodes static, we discussed in class the benefit. As a rule of thumb, a nested class should be static unless there is good reason it needs access to the private instance variables of the outer class.

Begin your implementation by making a constructor for SortedLinkedList. Then go through your methods one by one and implement them.

With each method you implement, re-run SortedListTester and make sure that the tests run as you expect. When you've finished implementing SortedLinkedList, you should be able to run SortedListTester and see that all of your tests pass.

Challenge: Iterator

Implementing the iterator method is optional; if you implement it, you'll be able to use the colon notation in for-each loops to iterate over your SortedList. I advise saving iterator until the end, to do if you have time.

If you're not implementing iterator(), make calling that method throw an UnsupportedOperationException:

public Iterator<ZooAnimal> iterator()
{
   throw new UnsupportedOperationException("iterator() not implemented!");
}

If you choose to implement an iterator, see these notes to find out more about how to do it. If you go this route, you should be able to write an iterator where both next() and hasNext() are O(1).

Part D: Revising ZooDisplayer

Revise ZooDisplayer so that it's done in an object-oriented style.

To implement the two display methods you will likely only have to make small revisions to your existing code.

Note that if you don't implement iterator above, you cannot use the for-each loop (colon syntax) to work through the list. This likely means that your code will by asymptotically slower than if you were using the iterator (which is fine). But make sure you understand why this is the case.

(Optional) Part E: Extending ZooDisplayer

As an optional challenge, make it so that the the user can add or remove animals interactively.

The user should be able to run ZooDisplayer either with:

java ZooDisplayer ZooExample.txt
or with:
java ZooDisplayer

In the former case, the animals in ZooExample.txt should be loaded; in the latter, no animals should be loaded. Then, the program should prompt the user by asking "What would you like to do?". There are five valid answers: "add animal", "remove animal", "display text", "display picture", and "exit":

Make it so your program is robust to the user accidentally adding a space before or after the command that they enter, and if they enter something invalid, prompt them with the valid commands. The program should continue prompting the user until "exit" is typed, although it's okay if it also ends if someone closes all the open pictures windows. Here's a sample interaction with the user, with user input in bold and computer output in black:
What would you like to do?
add animal
What is the species of the animal you're adding?
cat
What is the animal's name?
Eris
And what year was Eris born?
2011
Where can I find a picture of Eris?
cs201/hw07/zooPics/foobar.jpg
You must enter a valid path to an image of the cat.
koala.jpg
Animal added.
What would you like to do?
display my zoo!
Unrecognized command: display my zoo!
Valid commands: add animal, remove animal, display text, display picture, and exit.
What would you like to do?
remove animal
Which animal would you like to remove?
Byron
Byron was not found in the zoo.
What would you like to do?
display image
What would you like to do?
exit
Farewell!
Your output doesn't need to exactly match mine, but it should be able to respond to all of the commands appropriately (you may invent and add more to the list of recognized command if you wish).

4. Code notes

5. Submission and grading

Submit to Moodle a zip archive containing:

Follow the previously established guidelines.

Please make sure that every file you submit has your name and your partner's name at the top. For ZooDisplayer and ZooAnimal, keep the original authors' names in the comments but add your names, noting that you worked on it for hw07. For instance, if I worked on hw03 with Paris Geller and hw07 with Lane Kim, I would have comments like:
/** [Some description of my class]
    @author Paris Geller (hw03)
    @author Jed Yang (hw03 and hw07)
    @author Lane Kim (hw07)
*/

Assignment requirements

This is a partial list of the things that we'll be looking for when evaluating your work (in addition to style):

Grading

This assignment modified from one designed by Anna Rafferty.

Start early, have fun, and discuss questions on Moodle.