CSE 115 - Fall 2006 - Banner
   CSE 115 - Fall 2006 - Introduction to Computer Science for Majors I
CSE 115 - Fall 2006 - Left Navigation CSE 115 - Fall 2006 - Lab 8

Lab 8

Notice

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


Introduction

We have studied the fish for some time now, but want to be able to do something a little special, a little out of the ordinary. We want to be able to tell a fish to change it's behavior while our program is running to watch the effect this has on the other fish in the tank. Therefore, we need a way for a fish to respond when we click upon it with our mouse. Also, we want to introduce a new type of behavior - a leading/following behavior into our tank.


Working From Home? Click here for any special directions


New Concepts Covered

The following are the new concepts covered in this lab.
  • Delegation (Revisited)
  • Inheritance
  • Event Handling with the mouse
  • Objects as Behaviors (Strategy - Revisited)
  • Holder Pattern


Assignment Specification

You will write an Applet with four menus that will allow the user to create different kinds of fish in their fish bowl.

From the first menu, the user should be able to select from a menu the following types of fish:

* Regular - a fish that swims across the screen.
* Chameleon - a fish that changes colors randomly as it swims about the screen.
* Dizzy - a fish that spins while it swims.
* Random Swim - a fish that moves in a random way each time it is ready to move.
* Surprise - your extra credit fish from before (if you implemented it - sorry - no double dipping, no additional extra credit this time)

The second menu allows the user to select any combination of Chameleon, Dizzy, or Random Swim and create a fish with that behavior.

The third menu allows the user to create a Leader Fish. When created, a Leader Fish takes on the behavior that is currently selected in the multi-behavior menu.

The fourth menu allows the user to create a Follower Fish. Follower fish follow their leader around the screen.

 

Additionally, clicking on any fish on the screen at any time should make that fish become the new leader and all of the the follower fish should abandon the old leader to begin following the new one.


Helpful Hints

Before you start programming, look over the notes from the lectures. Very special (not-so) secret tip: review the lectures on Interfaces, Polymorphism, Design Patterns, and Inheritance 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.


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 up to chapter 6 in the text.


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 Lab8 project skeleton from the LabSkeletons repository and disconnect it from the repository.

 

Lab Design

Once again you are required to design your solution to the lab. Create a file called Lab8.dia in your project and submit it with your jar file.

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?

Once again, remember to design and code iteratively.

 

Code Reuse

In this lab, you will be re-using most of the code you wrote in Lab 7 with perhaps some minor changes. Therefore, you should make use of the Copy-Paste feature of eclipse so that you do not need to do all the work you did in Lab 7 over again.


Leader Fish

When creating our leader fish, we need to know what type of behavior has been currently selected in the multi-menu. Right now, the only object who knows what those selections are is the multi-menu itself. Well, we can fix that. All we need to do is create an object that holds onto that information. The multi-menu should be able to communicate with this object as well as the leader menu. Whenever a change is made to the multi-menu it notifies the object of the change. Whenever, a leader fish is created, it asks this object for what the currently-selected behavior is. This object is an implementation of the Holder design pattern.

After you create your holder and make both the multi-menu and the leader menu able to communicate with it, you're all set to create leaders all over the place.

Everyone needs Groupies (or is it gupies?)

How will the leader know that s/he is great if we do not give some little fishies to follow the leader around? Let's work on the Follwer fish next. The Follower fish need to have a connection to their leader, they need to *know* who they are following around. When they move, they need to find out the way their leader moves, and move accordingly. You can set up a behavior that will do this, but we must also remember when we are creating a new follower to tell it who its leader is. How do we know that? How do we know who the current leader is? Well, it seems as though we need another Holder to keep track of that information for us. When a new leader fish is created, it becomes the "current leader". Follower Fish ride the wave of current popularity. They will follow whoever is the current leader. Once a new leader is in the tank, the old leader is as interesting to them as yesterday's news and they abandon the old leader to become a star on some VH1 show.

Let's look at what all fish have in common - they can demonstrate a certain behavior. Each behavior described above are unique from the others, but in all cases, each fish can exhibit any one or multiple of the behaviors. This idea is encapsulated in the fishbowl.strategy.Fish class. It has the ability to set it's current behavior. You can set the behavior of a fish to be anything that implements the fishbowl.strategy.IFishBehavior interface.

Leaders should be Dynamic!

One last thing to do before we are done - that is make it so that when we click on a fish - it becomes the new leader and all the other fish follow it. The new leader's behavior should then become the behavior that is indicated by multi-menu and the leader holder should be notified of the change in authority.

In order to do this, we will no longer be able to use our fishbowl.strategy.Fish. That fish simply isn't good enough. Sure it could swim and have behaviors, but no ability to respond to clicks. There needs to be a new fish in town. This is where the lessons about inheritance come into play. We will create a better fish, one that can do all that a fishbowl.strategy.Fish can do and a bit more.

Start out by creating a subclass of fishbowl.strategy.Fish. You can leave the body of the class empty for now, but you should go back and change all of your menu classes to create instances of your new Fish class, not the fishbowl.strategy.Fish anymore.

Now, we will need our fish to be able to respond to clicks. We can do this by adding a listener to the fish. Giving the fish a listener allows for the system to keep track of when a user does something with a fish. We are most interested in when our fish gets clicked on and we can use a MouseListener to have the system pay attention to when our fish is getting clicked. The TAs will go into a further explanation of how to attach a mouse listener to the fish.

So, once the fish is clicked on, you simply have to get the information from the mult-behavior holder and tell the leader holder that this fish is the new leader in town and you're done!


Classes' Public Interfaces

We have provided documentation for the fishBowl.strategy package and the cse115.utilities package in Javadoc format.


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 Lab8.jar

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

Due 11:59:59pm the day before your recitation meets the week of November 6th, 2006.

 

CSE 115 - Fall 2006 - Footer

 

 
Last modified: Fri Oct 20 07:44:12 2006