Lab 5

Notice

This lab is a modified version of a lab originally developed at Rice University.


Working from home

If you are working from home, you must download the latest version of the JAR file named Classlibs.jar and place it in your

c:\projects\CSE115\Classlibs\Spring2006\
folder.

You will also be making one final update to Green this semester, by downloading this file and unzipping it to C:\ (NOTE THE DIFFERENCE! - Really just unzip to C:\ this time.)


Introduction

Here's your chance to play around with the behaviors for some shapes (fish) moving about (more accurately swimming about) the screen.

NOTE: Starting with this lab, submissions that do not compile will receive an automatic grade of 10 for the assignment - no exceptions! Your code must compile when you submit it.


New Concepts Covered

The following are the new concepts covered in this lab.


Assignment Specification

You will write an Applet that will allow the user to create different kinds of fish in their fish bowl. The user should be able to select from a menu the following types of images:

When a fish is created, it should begin its swim at a random position, be created with a random color and move accordingly. Parameterize values where possible. You can always select random values for these when the fish are created.


Helpful Hints

Before you start programming, look over the notes from the lectures. Very special (not-so) secret tip: review your lecture notes before beginning this assignment. If you don't understand something that is covered in that lecture, see your TA or instructor during their office hours.

Stephen Wong's insightful commentary on inheritance and polymorphism

A living, if not rather fishy, analogy

Biologists were thinking about abstract behaviors and properties long before computers existed.   In an effort to make sense of an exceedingly complex world, biologists have attempted to classify all living things into different levels of abstraction called taxonomy trees.   This is of course the famous "Kingdom/Phylum/Class/Order/Family/Genus/Species" classification scheme.

Every organism can fits in this scheme at the least abstract level, "Species" (ignoring sub-species).    "Genus" does not refer to any specific Species, but rather it refers to a collective abstraction of a number of different species.   Likewise, any given "Class" is a further abstraction of the "Genus"'s that it contains.  

Let's look at it in the other direction: 

Suppose we look at the animals that make up the Family "Salmonidae".   These are herring-like fishes with soft ray-less adipose fins (a small fin just below the tail) that live in salt water but spawn in fresh water.  These fish live around the world.   A fine set of fishes, no doubt.   

Now consider the Genus, "Oncorhynchus".    All these fish are Salmonidae , so they have every characteristic of that Family.  However, they are more specialized and are native only in the Pacific Ocean from California to Alaska.  They are stockier than their Altantic cousins, "Salmo", and have the unfortunate characteristic of dying once they have spawned.  The males do get a rather studly (from a female salmon's point of view) hooked snout when spawning, but it costs them their lives.    Their Salmo brethren are not very sympathetic, turning up their straight fishy noses as they head back to the sea after their freshwater trysts.  

Everywhere one talks about a Salmonidae, one can talk about a Oncorhynchus.  Likewise for a Salmo, every characteristic that applies to a Salmonidae also applies to it.  If you need a Salmonidae, you will do perfectly well if handed a Oncorhynchus or Salmo.   That is, a Oncorhynchus and Salmo are indestinguishable from a Salmonidae because that is exactly what they are.

We could take this analogy one step further and consider the species tshawyntscha, the "King" or Chinook" salmon.   The largest of the Oncorhynchus, this salmon grows up to 60 pounds and is characterized by a thick but streamlined body and black-lined mouth.   This is opposed to its cousin, kisutch, the "Coho" salmon which is smaller and has a white-lined mouth.

Once again, if it is a Pacific salmon you want, either a Chinook or Coho will do.   However, let us suppose you were fishing and you hooked a Pacific salmon.   Would they react the same?  A Coho will break your rod in half and jump a 100 times.  A Chinook won't jump much, but it will break your rod in half and take your arm with it.   Same Genus, but since they're different species, they do their own particular thing their own particular way.

Yeah, we can do this in the computer too.   After all, aren't we modeling the real world, whatever that may be?

The concept of an object having all the characteristics and behaviors of a more abstract object is called "inheritance".   Inheritance also implies the substitutability of an object for any object of a higher abstract level.  This is called "polymorphism" ("changing into many things").     Inheritance and polymorphism are the foundations of object-oriented programming and are what make it different than any other programming paradigm.  This is the source of OOP's power and flexibility.


Preparatory work

In order to be able to carry out the tasks required during your lab session, you must come prepared. Do the following before coming to your lab session:

Reading

Make sure you have read chapter 4 of the Brown notes.


Lab tasks

At your lab session your teaching assistant will briefly discuss how to carry out each of the tasks below. She or he will also be available to answer questions you might have. You must carry out each of the following tasks.

Get the project skeleton & disconnect from the repository

Get the Lab5 project skeleton from the LabSkeletons repository and disconnect it from the repository.

 

Lab Design

This time there is no more hand-holding. You will design your solution to the lab. You will create the class diagram. You are required to submit your class diagram in a file named Lab5.dia, with your lab solution in your jar file.

Since you are creating more classes on your own this time, you should also comment your code appropriately. Each class file should have comments indicating what the class does and each method should have comments about what its functionality is.

The assignment specification gives you a hint as to what classes you'll need. Recall the first step is to ask, what has already been written for me? You can explore the CSE115.FishBowl package to see what classes are available for your use. The classes you should pay most attention to are the CSE115.FishBowl.Applet (which your lab5.Applet extends), the CSE115.FishBowl.FishHatchery interface (which your lab5.FishHatchery implements), and the CSE115.FishBowl.Fish (your fish specializes this class). You should also look on the resources page to see the CSE115 JavaDocs page for additional information.

Remember to design and code iteratively. I suggest, starting out with getting a CSE115.FishBowl.Fish to swim about the screen. Watch its behavior. Recall that you can write classes to change the behavior. Once you've done that, you can begin work on your lab5.FishHatchery. Write only the code for the createFish() method at first. Make sure you've removed the original Fish code you wrote in Applet.

Chameleon Fish

This is a special type of CSE115.FishBowl.Fish. It moves like the original, except that as it is moving, it changes colors randomly. Luckily for us, there is a method named randomColor() that is available for us to call in the NGP.Utilities class. This method returns a random color - a different one each time it is called. Create your Chameleon Fish class. Make sure your code tells Java that a Chameleon Fish is a fish. Then you can partially override the update method. The update method will include the following:

this.setColor(NGP.Utilities.randomColor());

Now, add the code in your hatchery class to create the Chameleon Fish and test it out.

Hints for Other Fish

Dizzy Fish rotate around while they are swimming. Note the setRotation() method that is available on the fish.

Random Swimming fish move a random distance (either forwards or backwards) each time they move.

Dying fish have two distinct things that happen as they swim. The first is that they start off swimming normally, but slowly, over time, will eventually stop moving. As they are slowing down, they are also changing color so that they will eventually be black. Notice that the CSE115.Utilities.ColorChanger.darken(java.awt.Color) (Thanks Ben!) method will help you here.

Leader is a fish that combines any of the two behaviors previously defined, think a Chameleon Dizzy fish, or a Randomly Swimming Dying Fish. Use your imagination.

Follower fish follow their leader about the screen. Therefore, they need to know who their leader is...

Surprise fish is an opportunity for you to earn extra credit by using your imagination and creating a fish that has behavior that is nothing like what we have previously described for you. The TAs will have some ideas in recitation for you that you can implement or you can come up with an idea completely on your own. Please note that simply another combination fish will not be satisfactory for this extra credit. You must come up with a behavior that is distinct from ones we've described in this lab.


What you hand in

Once you are ready to submit, export your solution as a jar file, just as you did in previous labs, but name your jar file Lab5.jar

Remember to check the box that says "Include Java Source Files" or your Java code will not be included in your submission. Remember that you must also hand in a UML diagram for your solution.

Use the electronic submission program that corresponds to your recitation. For further directions on the submission program, please refer back to the earlier labs.

Due dates

This lab will be due for everyone at 11:59:59pm the day before your lab meets, the week of March 6th.