Title: AP "Case Studies"
1AP "Case Studies"
- A big program that all AP students are expected
to understand - Approximately 20 of the possible points on the
AP exam will come from case study questions - First Case Study introduced in 1994
- Directory Manager (Pascal)
- Big Int (C)
- Marine Biology (C)
- Marine Biology Simulation (Java)
2The Story
- A CS student, Pat, gets a summer job working for
marine biologists. - Hired to enhance an existing program that
simulates fish movement in a bounded environment. - Needs to understand existing program
- Designs, codes, and tests modifications
- Occasionally Pat turns to an experienced
programmer, Jamie, for help. - Narrative is Pats report of summer job.
3The Program
4The Chapters
- Experiment with existing program (run it)
- Guided tour of the code by Jamie
- Add breeding and dying
- Add two new kinds of fish (inheritance)
- Provide alternative representations (unbounded
environment, others)
5Marine Biology Simulation
- Data for the simulation is stored in text files
- oneFish.dat
- bounded 7 5
- Fish 3 2 North
- "Bounded" environment with 7 rows and 5 columns
- Fish starts from Row 3 Column 2 (zero based)
facing "North" (up)
6- bounded 7 5
- Fish 3 2 North
7manyFish.dat
- bounded 46 46
- Fish 0 0 North
- Fish 0 1 South
- Fish 0 3 East
- Fish 0 6 West
- Fish 0 10 North
- Fish 0 15 South
- Fish 0 21 East
- Fish 0 28 North
- Fish 0 36 South
- Fish 0 45 East
- Fish 1 0 North
- Fish 1 1 South
- and so on
8There are two charts you will need to fill out
that keep track of fish movement
9SimpleMBSDemo1.java
- public class SimpleMBSDemo1
-
- . . . .
- public static void main(String args)
-
- // Construct an empty environment and
several fish in the context - // of that environment.
- BoundedEnv env new BoundedEnv(ENV_ROWS,
ENV_COLS) - Fish f1 new Fish(env, new Location(2,
2)) - Fish f2 new Fish(env, new Location(2,
3)) - Fish f3 new Fish(env, new Location(5,
8)) - // Construct an object that knows how to
draw the environment with - // a delay display the initial
configuration of the environment. - SimpleMBSDisplay display new
SimpleMBSDisplay(env, DELAY) - display.showEnv()
- // Run the simulation for the specified
number of steps. - for ( int i 0 i lt NUM_STEPS i )
10SimpleMBSDemo2.java
- public class SimpleMBSDemo2
-
- private static final int ENV_ROWS 10
// rows in environment - private static final int ENV_COLS 10
// columns in environment - private static final int NUM_STEPS 15
// number of timesteps - private static final int DELAY 1000
// delay in milliseconds - public static void main(String args)
-
- // Construct an empty environment and
several fish in the context - // of that environment.
- BoundedEnv env new BoundedEnv(ENV_ROWS,
ENV_COLS) - Fish f1 new Fish(env, new Location(2,
2)) - Fish f2 new Fish(env, new Location(2,
3)) - Fish f3 new Fish(env, new Location(5,
8)) - // Construct an object that knows how to
draw the environment with - // a delay.
- SimpleMBSDisplay display new
SimpleMBSDisplay(env, DELAY)
11Marine Biology Simulation
- 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. - To model fish swimming in a bounded environment,
the program has Fish objects and an Environment
object. - The purpose of the program is to simulate fish
moving in the environment, so the program also
has a Simulation object. - There are other useful, but less important
"utility classes."
12The Simulation class
- Has a step method that executes a single timestep
- A timestep is where each fish has an opportunity
to move once - To have the fish continue to move, we would use a
loop to drive the simulation - //code from SimpleMBSDemo2.java
- for(int i 0 i lt NUM_STEPS i)
- sim.step()
13The Simulation class is pretty simple
- public class Simulation
-
- private Environment theEnv
- private EnvDisplay theDisplay
- public Simulation(Environment env, EnvDisplay
display) -
- theEnv env
- theDisplay display
-
- public void step()
-
- Locatable theFishes theEnv.allObjects()
- for ( int index 0 index lt theFishes.length
index ) -
- ((Fish)theFishesindex).act()
-
- theDisplay.showEnv()
-
14One step in the simulation
15The Environment interface
- public interface Environment
-
- Direction randomDirection()
- Direction getDirection(Location fromLoc,
Location toLoc) - Location getNeighbor(Location fromLoc,
Direction compassDir) - ArrayList neighborsOf(Location ofLoc)
- int numObjects()
- Locatable allObjects()
- boolean isEmpty(Location loc)
- Locatable objectAt(Location loc)
- void add(Locatable obj)
- void remove(Locatable obj)
- void recordMove(Locatable obj, Location
oldLoc)
16The BoundedEnv class
- a class that implements the Environment interface
- BoundedEnv env
- new BoundedEnv(ENV_ROWS, ENV_COLS)
17The Direction class
- Contains constants for the directions
- Direction.NORTH
- Direction.SOUTH
- //and so on. . .
- and functions to find relative directions
- Direction toRight(int degrees)
- Direction toLeft(int degrees)
- Direction reverse()
- boolean equals(Object other)
- String toString()
- What would this be?
- Direction.NORTH.toRight(90)
18The Location class
- Stores a location with a row and column
- Location(int row, int col)
- int row()
- int col()
- boolean equals(Object other)
- String toString()
- What would this display?
- Location one new Location(4,5)
- Location two new Location(3,5)
- System.out.println(one)
- System.out.println(two.col()one.col())
- System.out.println(two.equals(one))
19What is the output of this program fragment?
- BoundedEnv env new BoundedEnv(10, 10)
- Location loc1 new Location(2,3)
- Location loc2 new Location(5,3)
- Direction dir1 env.getDirection(loc1,loc2)
- System.out.println(dir1)
- Direction dir2 dir1.toLeft(90)
- System.out.println(dir2)
- Direction dir3 dir2.reverse()
- System.out.println(dir3)
- Location loc3 env.getNeighbor(loc1,dir1)
- System.out.println(loc3)
- Location loc4 env.getNeighbor(
- new
Location(5,2),dir3) - System.out.println(loc4)
20The Fish class
- Fish f1 new Fish(env, new Location(2, 2))
- Fish f2 new Fish(env, new Location(2, 3))
- Fish f3 new Fish(env, new Location(5, 8))
- Each fish has a Location
- All fish share a reference (handle) to their
environment
21The Fish class constructors
- public Fish(Environment env, Location loc)
- public Fish(Environment env, Location loc,
Direction dir) - public Fish(Environment env, Location loc,
Direction dir, Color col) - //helper methods
- private void initialize(Environment env, Location
loc, Direction dir, Color col) - protected Color randomColor()
22The Fish class "accessors"
- public int id()
- public Environment environment()
- public Color color()
- public Location location()
- public Direction direction()
- public boolean toString()
23The Fish class "modifiers"
- public void act()
- //helper methods
- protected void move()
- protected Location nextLocation()
- protected ArrayList emptyNeighbors()
- protected void changeLocation(Location newLoc)
- protected void changeDirection(Direction newDir)
24What is the output of this program?
- public class WhatOutput
-
- public static void main(String args)
-
- BoundedEnv env new BoundedEnv(5, 4)
- Fish nemo new Fish(env,new
Location(3,2)) - Fish dory new Fish(env,new
Location(2,3)) - int nRow nemo.location().row()
- int nCol nemo.location().col()
- nCol
- nemo.changeLocation(new
Location(nRow,nCol)) - nRow dory.location().row()
- nCol dory.location().col()
- nRow--
- dory.changeLocation(new
Location(nRow,nCol)) - System.out.println(nemo.location())
- System.out.println(dory.location())
-
25What is the output of this program?
- public class WhatOutput
-
- public static void main(String args)
-
- BoundedEnv env new BoundedEnv(10, 10)
- Location loc1 new Location(2,3)
- Location loc2 new Location(5,3)
- Location loc3 new Location(3,3)
- Direction dir1 env.getDirection(loc1,lo
c2) - Location loc4 env.getNeighbor(loc1,dir1
) - System.out.println(loc4)
- System.out.println(loc3 loc4)
- System.out.println(loc3.equals(loc4))
- System.out.println(env.neighborsOf(loc1))
- System.out.println(env.neighborsOf(loc1)
-
.contains(loc4)) -
26Reading the MBSCS documentation
- The previous program uses three methods from the
Environment interface - getDirection()
- getNeighbor()
- neighborsOf()
- One method from the Location class
- equals()
- One method from the ArrayList class
- contains()
27toString()
- There are "invisible" calls to the toString()
method for the Location class - System.out.println(loc4)
- //is the same as
- System.out.println(loc4.toString())
28toString()
- Appendix C "Black Box Classes" has the
documentation for the Location class - public String toString()
- Returns a string indicating the row and column of
the location in (row,col) format
29The Environment documentation
- You can find the documentation for the
Environment interface in the Java code, but it's
a little hard to read - Direction getDirection(Location fromLoc, Location
toLoc) - / Returns the adjacent neighbor (whether valid
or invalid) of a location in the specified
direction. - _at_param fromLoc starting location for
search - _at_param compassDir direction in which to
look for adjacent neighbor - _at_return neighbor of ltcodegtfromLoclt/codegt in
given direction /
30The Environment documentation
- You can create documentation that is easier to
read using Java's "JavaDoc" feature. In RealJ - With your MarineBiologySimulation project open
choose ToolsJavaDoc Project Files - Select the folder where you want the
documentation to be stored - Open the documentation in a browser like Netscape
or Explorer
31The Environment documentation
32The Fish class
- Three constructors
- public Fish(Environment env, Location loc)
- public Fish(Environment env, Location loc,
Direction dir) - public Fish(Environment env, Location lo,
Direction dir, Color col) - Rather than duplicating code, each constructor
calls a private method - private void initialize(Environment env, Location
loc, Direction dir, Color col)
33The Fish class
- public Fish(Environment env, Location loc)
-
- initialize(env, loc, env.randomDirection(),
randomColor()) -
- If the direction and color aren't specified,
methods are used to generate random values
34The Fish class IDs
- Every Fish has a unique ID
- private static int nextAvailableID 1
- Since it is static, all fish share a single ID
"mailbox" - private void initialize(Environment env, Location
loc, Direction dir, Color col) -
- theEnv env
- myId nextAvailableID
- nextAvailableID
- myLoc loc
- myDir dir
- myColor col
- theEnv.add(this)
- // object is at location myLoc in environment
35The Fish class
- When a Fish is constructed, it is immediately
added to the Environment (no fish out of water!) - private void initialize(Environment env, Location
loc, Direction dir, Color col) -
- theEnv env
- myId nextAvailableID
- nextAvailableID
- myLoc loc
- myDir dir
- myColor col
- theEnv.add(this)
- // object is at location myLoc in environment
36super()
- Refers to the Parent class
- Here, Person is the parent of ProWrestler
- class Person
-
- private String myName
- public Person(String sName)myName sName
- public String toString()return myName
-
- class ProWrestler extends Person
-
-
37super()
- What do ProWrestler's have that normal people
don't? A gimmick! - class ProWrestler extends Person
-
- private String myGimmick
- public ProWrestler(String sName,
- String sGimmick)
-
- super(sName)
- myGimmick sGimmick
-
- public String getGimmick()return myGimmick
- public String toString()
-
- return super.toString() ", " myGimmick
-
38super()
- class ProWrestler extends Person
-
- private String myGimmick
- public ProWrestler(String sName,
- String sGimmick)
-
- super(sName)
- //calls parent constructor
- myGimmick sGimmick
-
- public String getGimmick()return myGimmick
- public String toString()
-
- return super.toString() ", " myGimmick
-
39super()
- class ProWrestler extends Person
-
- private String myGimmick
- public ProWrestler(String sName,
- String sGimmick)
-
- super(sName)
- //calls parent constructor
- myGimmick sGimmick
-
- public String getGimmick()return myGimmick
- public String toString()
-
- return super.toString() ", " myGimmick
- //calls the toString method of the
- //parent class
-
40super()
- public class SuperDemo
-
- public static void main(String args)
-
- ProWrestler theWorst new ProWrestler
- ("Yeti", "Giant guy covered in toilet
paper") - System.out.println(theWorst)
-
-
41- public class superWhatOutput
-
- public static void main(String args)
-
- Thingy aBunch new Thingy5
- for(int nI 0 nI lt aBunch.length nI)
- if(nI 2 0)
- aBunchnI new Thingy(nI)
- else
- aBunchnI new Widget(nI,'x')
- for(int nI 0 nI lt aBunch.length nI)
- aBunchnI.write()
-
-
- class Thingy
-
- private int myNum
- public Thingy(int nNum)myNum nNum
- class Widget extends Thingy
-
- private char myLetter
- public Widget(int nNum, char cLetter)
-
- super(nNum)
- myLetter cLetter
-
- public String toString()
-
- return myLetter super.toString()
-
-
42Instance variables vs. Class variables
- class SomeClass
-
- double myDouble
- static String myString
-
- myDouble is an "instance variable" every
instance of SomeClass has one - myString is a "class variable" all instances of
SomeClass share the same String "mailbox"