CSE 115 - Fall 2006 - Introduction to Computer Science for Majors I |
|
Lab 7NoticeThis lab is a modified version of a lab originally developed at Rice University.
IntroductionWe 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! Working From Home? Click here for any special directionsNew Concepts CoveredThe following are the new concepts covered in this lab.
Assignment SpecificationYou will write an Applet with two 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. 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 Helpful HintsBefore 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 WorkIn 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:ReadingMake sure you have read up to chapter 5 in the text.
Lab tasksAt 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 repositoryGet the
Lab DesignOnce again you are required to design your solution to
the lab. Create a file called 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.
Objects as BehaviorMany 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
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 PatternThe 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:
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
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. Strategy FishLet'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 Let's start with a simple fish, the Regular Fish. This fish is simply a fish
that has no particularly interesting behavior. It simply swims around the screen.
Now, If you do not set the Well actually 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 the IFishBehavior 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. This is probably a good time to test things out. Once you know this works, you can write your other behavior classes and hook them up in your class that is used by the Single Fish menu. Some notes about BehaviorsHere are some helpful hints so you can create the other behaviors for your fish.
Composing BehaviorsWe've done composite already in Lab 6, but here we are going to do
it so that a fish can take on a combination of two of the behaviors from
our menu. The composite in this case will know two objects of type
Classes' Public InterfacesWe have provided documentation for thefishBowl.strategy package
and the cse115.utilities package in Javadoc format.
What you hand inOnce you are ready to submit, export your solution as a jar file, just as you did in previous labs, but name your jar file Lab7.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 DatesDue 11:59:59pm the day before your recitation meets the week of October 30th, 2006.
|
Announcements | Labs | Meetings | People | Resources | Schedule | Syllabus |
Last modified: Fri Oct 20 07:35:21 2006 |