600 likes | 611 Views
AP Case Study Overview. Barbara Ericson ericson@cc.gatech.edu Jan 2005. 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?.
E N D
AP Case Study Overview Barbara Ericson ericson@cc.gatech.edu Jan 2005 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
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
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
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
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
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 Georgia Institute of Technology
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) 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 Georgia Institute of Technology
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. Georgia Institute of Technology
Fish in a Bounded Environment Georgia Institute of Technology
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
Simulation Class • A simulation of fishwould 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 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
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
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 Georgia Institute of Technology
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 Georgia Institute of Technology
What happens during a step? 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
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() 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
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 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>> Locatable <<interface>> Environment Fish 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."); } 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
Partial Class Diagram 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 0 1 2 3 4 0 1 2 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
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
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 Georgia Institute of Technology
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
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
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) 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
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
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
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
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
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
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
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 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
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 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
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
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 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 Georgia Institute of Technology
Array versus ArrayList 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
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