AP Case Study Overview

60
Georgia Institute of Technology AP Case Study Overview Barbara Ericson [email protected] March 2006

description

AP Case Study Overview. Barbara Ericson [email protected] March 2006. 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? - PowerPoint PPT Presentation

Transcript of AP Case Study Overview

Page 1: AP Case Study Overview

Georgia Institute of Technology

AP Case Study Overview

Barbara Ericson

[email protected]

March 2006

Page 2: AP Case Study Overview

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?

Page 3: AP Case Study Overview

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

Page 4: AP Case Study Overview

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

Page 5: AP Case Study Overview

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

Page 6: AP Case Study Overview

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

Page 7: AP Case Study Overview

Georgia Institute of Technology

Narrative Chapters

1. Pat’s exploration• Run the program • Think about what is happening

2. Explanation of the classes by Jamie

3. Modifying behavior• Adding breeding and dying

4. Adding inheritance (specialization)• Adding slow and darter fish

5. Modifying the environment• Unbounded environment

Page 8: AP Case Study Overview

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)

Page 9: AP Case Study Overview

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

Page 10: AP Case Study Overview

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.

Page 11: AP Case Study Overview

Georgia Institute of Technology

Fish in a Bounded Environment

Page 12: AP Case Study Overview

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

Page 13: AP Case Study Overview

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

Page 14: AP Case Study Overview

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?

Page 15: AP Case Study Overview

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?

Page 16: AP Case Study Overview

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

Page 17: AP Case Study Overview

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

Page 18: AP Case Study Overview

Georgia Institute of Technology

What happens during a step?

Fish object id

color

environment

toString

isInEnv

direction

act

location

EnvDisplayobject

showEnvEnvironmentobject

(partial list of methods)

allObjects

objectAt

isEmpty

add

neighborsOf

getNeighbor

getDirection

remove

recordMove

Fish object id

color

environmen

toString

isInEnv

direction

act

locationFish object id

color

environment

toString

isInEnv

direction

act

locationFish object id

color

environment

toString

isInEnv

direction

act

location

Simulationobject step

Page 19: AP Case Study Overview

Georgia Institute of Technology

Step Methodpublic 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 ————");

}

Page 20: AP Case Study Overview

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()

Page 21: AP Case Study Overview

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?

Page 22: AP Case Study Overview

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

Page 23: AP Case Study Overview

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

Fish

<<interface>>Environment

<<interface>>Locatable

Page 24: AP Case Study Overview

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."); }

Page 25: AP Case Study Overview

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?

Page 26: AP Case Study Overview

Georgia Institute of Technology

Partial Class Diagram

Page 27: AP Case Study Overview

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

Page 28: AP Case Study Overview

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

Page 29: AP Case Study Overview

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

Page 30: AP Case Study Overview

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

Page 31: AP Case Study Overview

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.

Page 32: AP Case Study Overview

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

Page 33: AP Case Study Overview

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)

Page 34: AP Case Study Overview

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

Page 35: AP Case Study Overview

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?

Page 36: AP Case Study Overview

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

Page 37: AP Case Study Overview

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

Page 38: AP Case Study Overview

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.

Page 39: AP Case Study Overview

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?

Page 40: AP Case Study Overview

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

Page 41: AP Case Study Overview

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

Page 42: AP Case Study Overview

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

Page 43: AP Case Study Overview

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

Page 44: AP Case Study Overview

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?

Page 45: AP Case Study Overview

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

Page 46: AP Case Study Overview

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

Page 47: AP Case Study Overview

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

Page 48: AP Case Study Overview

Georgia Institute of Technology

Array versus ArrayListFixed 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));

Page 49: AP Case Study Overview

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()

Page 50: AP Case Study Overview

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

Page 51: AP Case Study Overview

Georgia Institute of Technology

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?

Page 52: AP Case Study Overview

Georgia Institute of Technology

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?

Page 53: AP Case Study Overview

Georgia Institute of Technology

Map Interfaces and Classes

<<interface>>Map

<<interface>>SortedMap

TreeMap

HashMap Hashtable

Page 54: AP Case Study Overview

Georgia Institute of Technology

Map Methods

• Add an value to the map for the keyObject put(Object key,Object value); // optional

• Get a value for a keyObject get(Object key);

• Remove the value with the keyObject remove (Object key); // optional

• Check if the key is in the mapboolean containsKey(Object key);

• Check if the value is in the mapboolean containsValue(Object value);

• Remove all objects from the mapvoid clear();

Page 55: AP Case Study Overview

Georgia Institute of Technology

Looping Through a Map

• Get a set of the keysSet keySet = map.keySet();

• Get an iterator on the key setIterator iterator = keySet.iterator();

• Loop till no more items in the iteratorwhile (iterator.hasNext())

{

key = (String) iterator.next();

value = (String) map.get(key);

System.out.println(“key is “ + key + “ value is “ + value;

}

Page 56: AP Case Study Overview

Georgia Institute of Technology

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

Page 57: AP Case Study Overview

Georgia Institute of Technology

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

Page 58: AP Case Study Overview

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

Page 59: AP Case Study Overview

Georgia Institute of Technology

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

Page 60: AP Case Study Overview

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