Animal House!

Purpose

To design a Zoo, utilizing the strategies learned in previous labs and lectures, such as abstract classes, combined with a new class of self-referential data, a list. This lab intends to show how you can combine these two strategies to easily implement even complicated class hierarchies by thoroughly thinking through the data definition and knowing how to implement that definition in the frameworks of Java.

 

Conclusion

We have walked through the design and implementation of a problem involving complicated data. Not only do we have self-referential classes of data, but also class hierarchies. The Data Analysis was a difficult task, but once completed lent itself easily to the implementation of the classes. In addition, this well thought out Data Analysis also leads to structured data that others can understand, as well as a program that is easily adaptable.

Part A:

Download an example of the list of Integers and Strings code (zip format).

Study the structure of the lists.

What is defined in  AList? Why can we reuse the same AList for creating both lists of Integers and Strings?

Study the contains() method. Why are its definitions different in ConsIntegers and EmptyIntegers classes.

What about the count() method?

P.S. Notice how Strings and Integers are compared in the ConsIntegers, ConsStrings classes’ insert() method. We are using compareTo() that returns an int. Make sure you understand the logic behind it before starting the HW. If you don’t, now would be a good time to ask a tutor/TA about it.

Part B:

Download the code (zip format) for the project. This code already includes both of the jar files needed to get the project running in Metrowerks as well as the Metrowerks project file.

The project currently has an implementation of the Animal class hierarchy. Look over the Animal class and the Lion class. Make sure everything looks familiar (it should! we’ve done something similar in the last lab).

What if we are starting a large collection of lions? We want to be able to add these lions to a long list of lions and then search that list for a lion with a particular name, Simba for example. How do we do this? We need self-referential data! What is self-referential data? Self-referential data is data that is defined in terms of itself. This is how we will view our list.

After some data analysis, we discover that there are two kinds of Lists, EmptyLists and NonEmptyLists (we will use Cons and Empty). This is what we have all these dummy classes like EmptyLion, ConsLion, LionList for.

A list is one of two types, in the same way that an animal was one of three types! We have a parent and a child class!

What belongs in the parent class LionList?

What goes in only one of the two children, either ConsLion or EmptyLion?

Use what you learned from creating generic lists, like lists of Integers and Strings to write member data and constructors for EmptyLion, ConsLion, LionList. Be careful to determine which ones will actually have any of the above.

Now lets develop a couple of methods to operate on a list of Lions.

Write a method find() that determines if Simba is in the list of Lions. What  method does it remind you of that you’ve seen today...hmmmm, I don’t know, could it be boolean contains()...

If we decide to make lion steaks, how many pounds will we have using all the lions? Call this method steaks(). Would this method be similar to int count() in any way?

Write a method that sorts Lions by weight. Remember what method sort() made use of in lists of Integers...hint, hint insert(). So how many methods do you have to write to do the sorting?

Write tests for the sort method.

Part C:

So what if we wanted to create a Zoo that would be a collection of different animals?

What changes would we have to make? Do you think we’d have to start over with our class definitions? Could we reuse our existing class hierarchy?

Think about a theoretical solution, if you have time, implement it (actually shouldn’t take more than 5 minutes).

Check if all the methods will work on an animal in the zoo.

Develop tests.