Download presentation
Presentation is loading. Please wait.
1
Barbara Ericson ericson@cc.gatech.edu Jan 2005
AP Case Study Overview Barbara Ericson Jan 2005 Georgia Institute of Technology
2
Georgia Institute of Technology
Learning Goals Understand the case-study design and implementation What is the purpose of the case study? What are the classes? How to extend the case study? What is good about the design? How to study for the exam? Georgia Institute of Technology
3
Georgia Institute of Technology
Purpose Provide a larger example of an object-oriented program To use Run the program Look at the code and modify it To learn from An example of good design, documentation, and code Discuss design issues To extend Deepen understanding of inheritance and polymorphism by using them to extend a program Georgia Institute of Technology
4
Georgia Institute of Technology
Example Simulation A simulation of a real-world environment A simulation of fish in a lake Job simulation Master / apprentice Pat is learning from Jamie about how to do Object-Oriented design Georgia Institute of Technology
5
Georgia Institute of Technology
Setting the Stage Pat, gets a summer job programming for marine biologists Needs to modify an existing program written by Jamie Runs the program Tries to understand it Designs, codes, and tests modifications The narrative is her report Georgia Institute of Technology
6
What is in the Case Study?
Source and class files for main classes Jar files for other classes In Code directory Narrative Report by Pat In Narrative directory Data files for setting up simulations In DataFiles directory Javadoc html documentation In Documentation directory How to run in several development environments In ExecutionInformation directory Georgia Institute of Technology
7
Georgia Institute of Technology
Narrative Chapters Pat’s exploration Run the program Think about what is happening Explanation of the classes by Jamie Modifying behavior Adding breeding and dying Adding inheritance (specialization) Adding slow and darter fish Modifying the environment Unbounded environment Chapters 1-4 are on exam A, chapters 1-5 are on exam AB. Georgia Institute of Technology
8
What is the Goal of the Program?
“I worked on a simulation of fish moving in a relatively small body of water, such as a lake.” First fish can only move Later the fish can breed and die (Chapter 3) Later add slow fish and darter fish (Chapter 4) Later add other environments (Chapter 5) Chapter 5 is for the AB exam Georgia Institute of Technology
9
Georgia Institute of Technology
Chapter 1 - Exploration Run the main methods in the simple demos SimpleMBSDemo1, SimpleMBSDemo2 Underline the nouns and verbs in the specification on the next slide Do a walk-through with students acting as fish (blindfolded) and the environment Who does what? Use CRC cards to determine the Classes you might need Read the chapter and do the analysis. Georgia Institute of Technology
10
Chapter 1 – Analysis Exericse
The marine biology simulation case study is a simulation program designed to help marine biologists study fish movement in a small, bounded environment such as a lake or bay. For modeling purposes, the biologists think of the environment as a rectangular grid, with fish moving from cell to cell in the grid. Each cell contains zero or one fish. Underline the nouns and verbs in the specification above. Nouns are candidate classes. Verbs are usually behavior. Create a card for each noun. What is the responsibility of an object of this type in the simulation? Does this object need to work with objects of another class to accomplish a task? Georgia Institute of Technology
11
Fish in a Bounded Environment
Georgia Institute of Technology
12
Exploring the Code – Chapter 2
In this chapter you get a guided tour of the code What are the main classes and interfaces? Simulation, Fish, Environment What other classes and interfaces are there? Locatable, Location, Direction, EnvDisplay, RandNumGenerator, Debug Georgia Institute of Technology
13
Georgia Institute of Technology
Simulation Class A simulation of fish would need a simulation class To set up the simulation objects To run the simulation Usually a simulation will take place over time So we need something to say that a unit of time has passed A simulation time step The pictures show here are from running the main method of MBSGUI with the data file of one fish. Georgia Institute of Technology
14
Georgia Institute of Technology
Fish Class A simulation of fish would require a Fish class What data does a fish need to know about itself? What can fish do? Georgia Institute of Technology
15
BoundedEnvironment Class
A simulation of fish in a bounded environment like a lake or bay Does it matter if it is a lake or bay? What is a bounded-environment? What data does the environment need to know about? What can an environment do? Georgia Institute of Technology
16
The First Simple Simulation
Create a bounded environment with a number of rows and columns Create the fish and set their locations Create the display class to display the simulation SimpleMBSDisplay Loop and tell each of the objects in the simulation to do something each time through the loop Update the display to show the changes The first simple simulation class is SimpleMBSDemo1. Georgia Institute of Technology
17
The Second Simple Simulation
The driver Creates an Environment object Creates Fish objects Creates an EnvDisplay object SimpleMBSDisplay Creates a Simulation object Loops sending the step message to the simulation object The first simple simulation class is SimpleMBSDemo2. Georgia Institute of Technology
18
What happens during a step?
The simulation object asks the environment object for an array of all objects in the environment. The simulation object loops through the objects in the environment and sends the act message to each. The simulation object sends the showEnv() method to the EnvDisplay object to update the display. Georgia Institute of Technology
19
Georgia Institute of Technology
Step Method public void step() { // Get all the fish in the environment and ask each // one to perform the actions it does in a timestep. Locatable[] theFishes = theEnv.allObjects(); for ( int index = 0; index < theFishes.length; index++ ) ((Fish)theFishes[index]).act(); } theDisplay.showEnv(); Debug.println(theEnv.toString()); Debug.println("———— End of Timestep ————"); Georgia Institute of Technology
20
Georgia Institute of Technology
Debug Class Used to handle messages for debugging You can turn on debugging messages turnOn() You can turn off debugging messages turnOff() You can print messages when debugging is on print(message) or println(message) You can restore the previous state restoreState() What do you think of this class? Should all these methods be static? Georgia Institute of Technology
21
Georgia Institute of Technology
Step Method Exercise Why is the return type from the method allObjects() an array of Locatable objects? Look at the Fish.java class Look at the Locatable Javadoc documentation Why do you need to cast to Fish before you send the message act? Do Locatable objects have an act method? Why do you need to send the message showEnv() to the EnvDisplay object? Georgia Institute of Technology
22
What happens in the act method?
Originally it checks that the fish is in the environment And if so sends the fish the move message The move method invokes nextLocation() to get the new location to move to which works with environment to get a random next location from the empty neighbors Removes the location behind the fish It checks if the new location is different from the old location If so it calls changeLocation to do the move It gets the new direction to face from the environment It calls changeDirection to use the new direction This describes the original act method. Later chapters add breeding and dying to act. Georgia Institute of Technology
23
Georgia Institute of Technology
Fish and Environment Fish and environment have a bi-directional association A fish keeps track of the environment it is in The environment keeps track of the Locatable objects in it Fish are locatable objects When a fish object is created It asks the environment to add the fish When a fish object moves It needs to update the environment <<interface>> Environment <<interface>> Locatable Fish Bi-directional associations can be tricky. Each side of the relationship needs to make sure than when one side changes the other is informed. Since all fish should be in an environment they are added to the environment when they are created. When a fish moves location it needs to inform the environment so that it agrees with the fish. Georgia Institute of Technology
24
Georgia Institute of Technology
The Move Method /** Moves this fish in its environment. **/ protected void move() { // Find a location to move to. Debug.print("Fish " + toString() + " attempting to move. "); Location nextLoc = nextLocation(); // If the next location is different, move there. if ( ! nextLoc.equals(location()) ) // Move to new location. Location oldLoc = location(); changeLocation(nextLoc); // Update direction in case fish had to turn to move. Direction newDir = environment().getDirection(oldLoc, nextLoc); changeDirection(newDir); Debug.println(" Moves to " + location() + direction()); } else Debug.println(" Does not move."); Maybe this method should be renamed to tryToMove() since that is what it does. Georgia Institute of Technology
25
Georgia Institute of Technology
Move Method Exercise Why does the fish have to work with the environment object to figure out where to move to? Why does the fish class determine which neighbors are empty? When does the environment record that the fish changed location? Why are the locations checked to be equal using equals() not ==? Why does the environment determine the new direction for the fish to face? Why doesn’t the code check if the new direction is different from the old direction before changing it? Why can’t fish go backwards? Georgia Institute of Technology
26
Georgia Institute of Technology
Partial Class Diagram Not all the classes are shown here. For example a fish has a Location object and a Direction object. You can’t show associations between two interface objects. There is an implied association between Environment objects and Locatable objects. There is an implied association between the EnvDisplay and the Environment. Georgia Institute of Technology
27
Georgia Institute of Technology
Location Class Stores row and column information Implements Comparable A location is less than another location when it is above and to the left of the other Overrides equals True if the row and column values are the same 1 2 3 4 Rows and columns are indexed starting with 0. Rows are usually given first and are the vertical rows of the grid. Columns are usually specified second and are the horizontal columns of the grid. This may be confusing to your students who are used to x,y (horizontal, vertical). 1 2 Georgia Institute of Technology
28
Georgia Institute of Technology
Direction Class Represents a direction Has constants for the compass directions Can also create using degrees 0 is North 90 is East Can get a new direction toRight, toLeft, reverse Overrides equals Georgia Institute of Technology
29
RandNumGenerator Class
Class used to create and hold a pseudo-random number generator java.util.Random Used to pick something The color for a fish The location to move to Used to get consistent results Especially if you use the same seed Georgia Institute of Technology
30
Georgia Institute of Technology
Interfaces Used Locatable (object with a location) Has a method to get a location Used to allow you to put other types of objects in an environment Environment (tracks locatable objects in a grid) Has methods to add and return Locatable objects Has methods to get neighbors of a location Including a method to get a neighbor in a direction Has methods to get the direction between two locations Used to allow you to change the environment class (like switch to unbounded environment) EnvDisplay (display an environment) Has a method to show the environment Used to allow different display classes The separation of the Environment class and EnvDisplay class is a good way to break things into a model and a view. That way you can change the view without bothering the model. Georgia Institute of Technology
31
Adding Breeding and Dying – Ch 3
Problem Specification: A fish should ... have a 1 in 7 chance of breeding, breed into all empty neighboring locations, attempt to move when it does not breed, never move backwards, and have a 1 in 5 chance of dying after it has bred or moved. Georgia Institute of Technology
32
Modified Act Algorithm
If this fish isn’t in the environment return if a random number from 0-1 is in 1/7 range try to breed into all empty neighbors If breeding failed try to move to an empty neighbor but still can’t move backwards If a random number from 0-1 is in 1/5 range die Georgia Institute of Technology
33
Georgia Institute of Technology
Breed Algorithm Get a list of empty neighboring locations For each location in the list Create a new fish at that location Use the same environment as the parent Remember than the constructor for fish adds the fish to the environment Use the same color as the parent To make it easier for subclasses to create a child that is the same type Use a protected method generateChild(loc) You can’t override private methods so generateChild can’t be private. You could use a public method but that means that other classes can call it. Using protected means that both children classes and classes in the same package can invoke the method so it limits the access a bit more than public. I would rename the breed() method to tryToBreed() since that is what it now does. Georgia Institute of Technology
34
Georgia Institute of Technology
Die Algorithm The only reference to a fish object is in the Environment So remove the fish from the environment Sets the reference to the fish to null Then it can be garbage collected whenever garbage collection runs Georgia Institute of Technology
35
Breeding and Dying Exercise
How would you handle increasing the odds that a fish will die as it ages? How would you handle requiring a male and female to be in close contact before a female fish can breed? How about if a fish has to be a certain age before it can breed? How would you randomly assign the dying probability? Georgia Institute of Technology
36
Georgia Institute of Technology
Advanced Simulations Use MBSGUI Open, create, edit and save environment files using the File menu Modify the random number generation using the Seed menu Modify what Run does with the Run menu Zoom in or out with the View menu Georgia Institute of Technology
37
Georgia Institute of Technology
Edit the Environment In the File menu click Edit environment You can add new fish by picking the type of fish and color and then clicking in a grid location You can save the result as an environment file Georgia Institute of Technology
38
Adding New Types of Fish – Chapter 4
Darter Fish A darter fish darts two cells forward if both the first and second cells in front of it are empty. If the first cell is empty but the second cell is not, then the darter fish moves forward only one space. If the first cell is not empty, then the darter reverses its direction but does not change its location. Slow Fish A slow fish moves so slowly that, even when it does not breed, it only has a 1 in 5 chance of moving. Georgia Institute of Technology
39
Georgia Institute of Technology
Using Inheritance Darter Fish and Slow Fish are both types of Fish They meet the substitution test They have the same attributes and operations But they specialize behavior So some methods will be overridden What method or methods would you override for each new type of Fish? Georgia Institute of Technology
40
Georgia Institute of Technology
Darter Fish Changes Change the definition of nextLocation If the two locations ahead are both empty Return the location two positions ahead If the location just one ahead is empty but the next is filled Return the location one ahead If the first position ahead is filled Return the current location In move if the new location equals the current location Reverse the direction Modify generateChild to create a DarterFish Create constructors using super() to initialize inherited private fields Georgia Institute of Technology
41
Darter Fish and Dynamic Binding
The DarterFish class needs to override move, nextLocation, and generateChild When a DarterFish object gets an act message it doesn’t find one in DarterFish so it calls the one in Fish When it needs to move it calls the one in DarterFish The correct method is called at run-time due to dynamic binding (run-time binding). When a message is sent the actual class of the object is checked first for the corresponding method. If one doesn’t exist then the parent is checked and so on until the method is found. Georgia Institute of Technology
42
Georgia Institute of Technology
Slow Fish Changes Add an attribute that represents the probability of moving 1/5 chance Modify the nextLocation method to only move to a random empty neighbor If a random number is in the 1/5 range Modify generateChild to create a SlowFish Create constructors Using super to initialize inherited fields Georgia Institute of Technology
43
Slow Fish and Dynamic Binding
The SlowFish class needs to override nextLocation and generateChild When a SlowFish object gets an act message it doesn’t find one in SlowFish so it calls the one in Fish When it needs the nextLocation it uses the one from SlowFish The correct method is called at run-time due to dynamic binding (run-time binding). When a message is sent the actual class of the object is checked first for the corresponding method. If one doesn’t exist then the parent is checked and so on until the method is found. Georgia Institute of Technology
44
Georgia Institute of Technology
Fish Types Exercise What would you need to do to create fish that swam only diagonally? What would you need to do to create fish that never die? What would you need to do to create fish that only stay at the bottom two rows of the environment? What would you need to do to create fish that only have one baby? Georgia Institute of Technology
45
Adding Environment Types – Ch 5
The original code used a BoundedEnv Represents a rectangular bounded environment like a lake or a bay Pat is asked to add an UnboundedEnv To represent a much larger area like an ocean To represent areas that are not rectangular Georgia Institute of Technology
46
Georgia Institute of Technology
BoundedEnv Class Has a 2d array of Locatable objects Fast to check if a location is empty or get an object at a location array[loc.row()][loc.col()] Slow to create an array of all objects Has to loop through the 2d array and if there is an object at that location add it to the array to return Inherits from SquareEnvironment Abstract class that defines an environment with square cells BoundedEnv also keeps track of the number of objects in it. This is why it can create an array of the correct size before it adds the objects in the environment to the array. Georgia Institute of Technology
47
Georgia Institute of Technology
UnboundedEnv Class Stores a list of objects in the environment As an ArrayList Takes up less space for large environments No space saved for “empty” locations Slow to find an object at a location Must search the list to see if any of the objects are at the location Fast to return an array of all objects in the list Can use toArray() Inherits from SquareEnvironment The array list stores a list of Locatable objects. Georgia Institute of Technology
48
Array versus ArrayList
Fixed size once created Can grow and shrink Can contain objects and primitives Can only contain objects Must declare element type Element type is object Fish[] myArray = new Fish[15]; ArrayList myList = new ArrayList(); myArray[index] = new Fish(loc); myList.add(new Fish(loc)); Fish f = myArray[index]; Fish f = (Fish) myList.get(index); for ( int k = 0; k < myArray.length; k++ ) System.out.println( myArray[index]); for ( int k = 0; k < myList.size(); k++ ) System.out.println(myList.get(k)); Georgia Institute of Technology
49
Georgia Institute of Technology
UnboundedEnv Details Returns -1 for the number of rows and columns since it is unbounded Another idea would have been to move that out of the interface and into BoundedEnv Must search through the ArrayList for objectAt(loc) Compare the current object’s location to the passed loc using equals Checks that there isn’t more than one object at the old or new location For recordMove() Georgia Institute of Technology
50
Running with UnboundedEnv
Use data files from DataFiles\UnboundedEnvDataFiles The display will show the fish with 0,0 at the top left You can use the scroll bars to see the fish that have moved beyond the original display area Georgia Institute of Technology
51
UnboundedEnv Exercises
The same data file will result in different fish configurations for a BoundedEnv versus the UnboundedEnv And not just because there is no boundary so there are more places to move to Do a fish simulation with students acting as fish And change the order they are asked to move Do they end up in the same location after each step? Georgia Institute of Technology
52
Extensions to UnboundedEnv
Modify UnboundedEnv to make it easy to find an object at a location Use a java.util.TreeMap which maps a Location object to a Locatable object And still keeps the keys in sorted order by Location What is the performance difference for objectAt(loc)? Do you get more consistent test behavior? Georgia Institute of Technology
53
Map Interfaces and Classes
HashMap Hashtable <<interface>> SortedMap TreeMap Georgia Institute of Technology
54
Georgia Institute of Technology
Map Methods Add an value to the map for the key Object put(Object key,Object value); // optional Get a value for a key Object get(Object key); Remove the value with the key Object remove (Object key); // optional Check if the key is in the map boolean containsKey(Object key); Check if the value is in the map boolean containsValue(Object value); Remove all objects from the map void clear(); Georgia Institute of Technology
55
Georgia Institute of Technology
Looping Through a Map Get a set of the keys Set keySet = map.keySet(); Get an iterator on the key set Iterator iterator = keySet.iterator(); Loop till no more items in the iterator while (iterator.hasNext()) { key = (String) iterator.next(); value = (String) map.get(key); System.out.println(“key is “ + key + “ value is “ + value; } You can use values method to get a Collection of values in the map. You can use the entrySet method to get a Set of key and value entries. Georgia Institute of Technology
56
Which Collection Should you Use?
List - objects in order with duplicates allowed ArrayList - not thread-safe so fastest Vector - thread-safe LinkedList - best when frequent insertions and deletions in the list Set - no order and no duplicates allowed HashSet - not sorted TreeSet - sorted based on Comparable interface Map HashMap - not thread-safe Hashtable - thread-safe TreeMap - keys are sorted You will mostly use ArrayList, HashSet, and HashMap if you don’t need your collections to be thread-safe. Earliest versions of Java only had Vector and Hashtable as collections. By thread-safe we mean that several threads of execution can access the collection. The methods in that collection are synchronized so that only one thread has access at a time. Georgia Institute of Technology
57
What is good about the case study?
Models good design, documentation, and coding practices Larger group of classes than students often encounter So plenty to learn from Uses a compelling problem Creating a simulation for scientist Context for design discussions Students learn by changing existing code There are some problems with the case study. The code doesn’t use Java standards like names for accessors and modifiers (getX, setX). It uses myField for field names which is not standard in Java but was used in C++. The emptyNeighbors() method should be moved to Environment, it doesn’t belong in the Fish class. Georgia Institute of Technology
58
Georgia Institute of Technology
Studying for the Exam Know the classes and their responsibilities Understand the code for the relevant classes Understand how Fish and Environment work together What happens when fish are created, move, breed, and die Understand how to create subclasses of Fish Understand the design decisions and tradeoffs Understand inheritance and polymorphism For the A exam focus on Simulation, Fish, DarterFish, and SlowFish. For the AB exam also study the Environment interface and the BoundedEnv and UnboundedEnv classes. Georgia Institute of Technology
59
Graphical User Interface Help
Click on Help and then Help again Contains instructions for customizing the MBSGUI Change how fish are displayed Add new types of fish Add new types of environments Add a menu to control breeding and dying Georgia Institute of Technology
60
Georgia Institute of Technology
Summary The case study is provided as an example of a larger, well designed object-oriented program With distributed responsibility With interfaces to allow different classes to be “plugged-in” by implementing the interfaces With extensions to fish and environment Promotes design discussion and non-trivial test questions Georgia Institute of Technology
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.