CSE 115 - Spring 2007 - 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! 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 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. 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. 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 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 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 and including Chapter 6 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. See below for some suggestions.
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. Creating your solution for this Lab (Suggestions and Hints)Create your You need to add functionality to the menus. Look at the Javadocs to see what methods are available on the 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 Now, you need to create a fish that can demonstrate a certain behavior. Our 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 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 Note that Some notes about BehaviorsHere are some helpful hints so you can create the other behaviors for your fish.
Composing BehaviorsNow 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 BehaviorsOne 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 InterfacesCheck out the Javadocs on the Resources page to see more information about the classes in htefishbowl package.
What you hand inWhen 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 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 DatesDue 11:59:59pm the day before your recitation meets the week of March 26, 2007.
|
Announcements | Labs | Meetings | People | Resources | Schedule | Syllabus |
Last modified: Mon Mar 12 15:24:54 2007 © Adrienne Decker |