CSE 115 - Spring 2007 - Banner
   CSE 115 - Spring 2007 - Introduction to Computer Science for Majors I
CSE 115 - Spring 2007 - Left Navigation CSE 115 - Spring 2007 - Lab 7

Lab 7

Notice

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


Introduction

We want to study the behavior of fish in an aquarium for some biological research. When we create a fish, we want it to exhibit a particular type of behavior as it goes around the screen. We want to observe the fish behaviors to see what will happen when we get lots of fish in the tank. We also want a way for a fish to exhibit a combination of more than one behavior. Maybe as it swims in a circle, it also needs to emit a strange radioactive glow. We'll concentrate on behaviors that are similar to this, and some that you might not expect fish to have! We even 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.


Working From Home? Click here for any special directions


New Concepts Covered

The following are the new concepts covered in this lab.
  • Delegation
  • Objects as Behaviors (Strategy)
  • Null Object
  • Composite


Assignment Specification

You will write an Applet with two menus that will allow the user to create different kinds of fish in their fish bowl. We've done some of the graphics work for you on this lab, especially getting the menus set up. Your main task will be to get your fish to behave appropriately.

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 - an opportunity for extra credit (should you choose to accept it).

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

When a fish is created, it should be created with a random color and move according to the behavior selected for it. Note that the cse115.utilities.Random class and its randomColor method could be extremely useful here.

Additionally, clicking on any fish on the screen at any time should make that fish take on the behavior of the second menu (combination behavior) and swim around the screen with the new behavior.


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 and Design Patterns 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 and including 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 Lab7 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 Lab7.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. See below for some suggestions.


Objects as Behavior

Many people make the mistake of thinking that objects can only represent data. However, objects are used to model concepts both tangible and intangible. It is important to remember that we can just as easily represent processes with objects as we can simple data. Once we free our minds from the self-imposed prison, we can begin programming at the correct level.

Some words of inspiration for our journey from Stephen Wong:

To quote from Gamma, Helm, Johnson and Vlissides' (known in the industry as "The Gang of Four") "bible", Design Patterns, Elements of Reusable Object-Oriented Software, design patterns

...are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.

A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design.  The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities.

Design patterns are the biggest breakthrough in object-oriented design in the last twenty years and arguably the hottest area of modern OO research. 

But design patterns are hardly just just a theoretical endeavor.  Design patterns have their roots in industry, in the ways that programmers "in the trenches" have found through much trial and error to solve common design problems.   Design patterns take these concrete solutions and abstract their behaviors and structure so that we can use them to understand and solve a wide variety of problems.

At the heart of a design pattern is often a UML diagram that shows the classes involved in the pattern.

The Strategy Design Pattern

The strategy design pattern  is a way of abstracting behavior and capturing that behavior in an object.   An embodiment of the classic tenent of separating the invariant and variant behaviors of a system, it enables one to break one's design into two parts: 

  1. The "context" part of the design deals with the behavior at an abstract level.  That is, it doesn't care exactly what the behavior does, only that it does it.  The behavior is thus referred to as the "strategy" for accomplishing a task.   The context is the "invariant" part of the design in that its behaviors do not change from situation to situation.   Thus it can be encapsulated into a single object.
  2. The "strategy" part of the design captures the "variant" nature of the design where the particular actions taken when accomplishing a task change from situation to situation, perhaps dynamically as the program is run.   The strategy section consists of an abstract strategy class and a series of concrete strategy subclasses.   Each subclass represents are possible specific action that could be taken when the strategy is executed by the context.    The variant nature of the design has been abstracted and captured in a tree hierachy.

The context deals with the strategies at the abstract strategy level.   The abstract class contains an abstract method that that the context believes that it is executing.   In actuality, at any given moment, the context is holding one of the concrete subclasses.   Thus when the strategy's method is called, the resultant behavior is that of the concrete strategy being held.

The strategy pattern is one of the most fundamental and common design patterns and forms the basis for many other patterns.  One is well advised to know it well.

The biggest benefits of the strategy pattern are

  1. A clean delineation between the variant  and invariant components.
  2. The ability to dynamically change the behavior of a system without  explicitly changing classes.

Classic scenarios where the strategy design is used are to change the language used in message windows depending on what country the program is being run in.   Or changing the behavior of a button depending on what options had been previously selected.  Netscape plug-ins are an example of a strategy pattern.


Creating your solution for this Lab (Suggestions and Hints)

Create your Applet class (note that the skeleton does not contain an Applet class file. Your Applet should extend the fishbowl.Applet class. A good size for this lab when you are running it is 640 x 480. Run the Applet and see what you get.

You need to add functionality to the menus. Look at the Javadocs to see what methods are available on the fishbowl.Applet to help get the menus set up. There is a method that your Applet will inherit that will help display the proper information on the menus. You will need to create some classes to get the menus to appear properly. You should do this before proceeding onto creating any fish.

Once you have the text of the menus apppearing, you should work first on getting a regular fish to appear in the tank. You can simply create an instance of fishbowl.Fish to accomplish this. Note that the Fish is an IGraphic and it also knows how to add itself to the DrawingCanvas. Once you have created it, it will auto-magically appear in the tank.

Now, you need to create a fish that can demonstrate a certain behavior. Our fishbowl.Fish does not allow for this. Therefore, we need to make a specialization of the fishbowl.Fish that can support the use of a specialized behavior. Create a subclass of the fishbowl.Fish that will partially override the update() method from its parent.

This fish should be able to have its behavior set to one of the different types of behaviors talked about earlier in this assignment. We have not written any behaviors yet, but it should hopefully make sense to you that behaviors will all implement a common interface. You can create that interface and make sure your fish has an instance variable of that type and has a mutator to change what specific behavior the fish has during the program.

You should make it so that your program now creates a new instance of the fish class you created when the user selects to create a fish.

You should then think about what the fish will have to do every time update is called. The method update is called whenever the fish is supposed to move about the screen. Therefore, each fish has to do what its parent did on each update, but also what its currently set behavior tells it to do. Therefore, we need a method in our interface to call whenever we want to update our graphic on the screen based on our current behavior. Add this method to the interface and call it from your fish class' update method. Run the program again and you should receive an error - more specifically a NullPointerException. Why?

We need to set an initial value for the behavior of the fish, and we especially need to set a value for a fish that is regular (that has no special behavior). A regular fish would simply swim around like any normal fish and do nothing special. But this doing nothing is just as valid as the other types of fish behaviors, so we'd better provide a class for it. Create a do nothing class that implements your behavior interface. It's method (from the interface) should have an empty definition -- the true definition of doing nothing. The class you just created is an example of the Null Object design pattern. While it was omitted from the Gang of Four book, it is incredibly important and powerful. It allows us to give to represent nothingness in our system in the same way we are representing doing something. Try your code out again and make sure you have a working Regular fish before moving on.

Now, you want to create your other behaviors for the other fish and test each out one at a time. You may have to change the way you wrote your interface for Behavior as you get into behaviors that are more complex than the DoNothing Behavior.

Note that NullPointerExceptions are not good to have in your program. For most of you, you will still have one even at this point. Run your program and click "Make it" before clicking any behavior. If you get a NullPointerException, you need to fix it before turning in your assignment.

Some notes about Behaviors

Here are some helpful hints so you can create the other behaviors for your fish.

  • Cameleon Fish - Uses that pesky cse115.utilities.Random class' randomColor() method every time it updates.
  • 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.
  • 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 a combination of fish behaviors 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.

Composing Behaviors

Now we need to create a composite behavior to help our multi-behavior menu work. That way, a fish can take on a combination of two of the behaviors from our menu. The composite in this case will know two objects whose type is the interface you created for the behaviors. The composite will also implement that interface as well. The composite's method simply calls the method of both behaviors it knows. Getting this class working is the key to making the Multi Fish menus work. The TAs will discuss in recitation how to leverage this pattern to make the menu work fully.

Dynamically Changing Behaviors

One last thing to do before we are done - that is make it so that when we click on a fish - it takes on the currently selected behavior that is indicated by multi-menu.

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 multi-behavior menu and tell the fish what its new behavior is and you're done!


Classes' Public Interfaces

Check out the Javadocs on the Resources page to see more information about the classes in hte fishbowl package.


What you hand in

When you are finished, you need to export your solution from the Eclipse environment so that you can submit it. You need to follow the same steps as you did for disconnecting from the repository in Lab 2 . If you are not sure how to do this, please refer back to those instructions. This time, you should name your Jar file Lab7.jar. Your JAR should include your Java Source Code files and a UML diagram representing your solution to the problem named Lab7.dia.

Then you can submit the Lab7.jar file using the electronic submission program. If you do not remember how to use the the submission program, refer back to earlier labs.

Not so secret tip - we spent time in Lab 6 focusing on what our code looked like and you will be awarded/deducted points based on that as well as your code's functionality. You can safely assume that the same will apply to Lab 7.


Due Dates

Due 11:59:59pm the day before your recitation meets the week of March 26, 2007.

 

CSE 115 - Spring 2007 - Footer

 

 
Last modified: Mon Mar 12 15:24:54 2007
© Adrienne Decker