A Reference Materials Giventostudentsduringexam

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

AP

Computer Science A Exam



Appendix
(Quick Reference)
20062007









2006 The College Board. All rights reserved. College Board, Advanced Placement Program,
AP, and the acorn logo are registered trademarks of the College Board.





Content of Appendices


Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . A Exam J ava Quick Reference
Appendix B . . . . . . . . . . . . . . . . . . . . . . . . . Source Code for Visible Classes
Appendix C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Black Box Classes
Appendix E . . . . . . . . . . . . . . . . . . . . . . . . . A Exam J MBS Quick Reference
Appendix G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Index for Source Code

- i -




Appendix A A J ava Quick Reference
Appendix A -- A Exam Java Quick Reference

Accessible Methods from the J ava Library That May Be Included on the Exam

class java.lang.Object
boolean equals(Object other)
String toString()


interface java.lang.Comparable
*
int compareTo(Object other) // returns a value <0 if this is less than other
// returns a value =0 if this is equal to other
// returns a value >0 if this is greater than other

class java.lang.Integer implements java.lang.Comparable
*
Integer(int value)
int intValue()

class java.lang.Double implements java.lang.Comparable
*
Double(double value)
double doubleValue()

class java.lang.String implements java.lang.Comparable
*
int length()
String substring(int from, int to) // returns the substring beginning at from
// and ending at to-1
String substring(int from) // returns substring(from, length())
int indexOf(String str) // returns the index of the first occurrence of str;
// returns -1 if not found

class java.lang.Math
static int abs(int x)
static double abs(double x)
static double pow(double base, double exponent)
static double sqrt(double x)
static double random() // returns a double in the range [0.0, 1.0)


class java.util.ArrayList<E>
int size()
boolean add(E obj) // appends obj to end of list; returns true
void add(int index, E obj) // inserts obj at position index (0 ,
// moving elements at position index and higher
// to the right (adds 1 to their indices) and adjusts size
size) index
E get(int index)
E set(int index, E obj) // replaces the element at position index with obj
// returns the element formerly at the specified position
E remove(int index) // removes element from position index, moving elements
// at position index + 1 and higher to the left
// (subtracts 1 from their indices) and adjusts size
// returns the element formerly at the specified position


*
The AP J ava subset uses the "raw" Comparable interface, not the generic Comparable<T> interface.
-A1-
Appendix B Simulation.java
Appendix B -- Source Code for Visible Classes
This appendix contains implementations of the visible core classes covered in Chapters 1 - 4:
Si mul at i on, Fi sh, Dar t er Fi sh, and Sl owFi sh. Information about the Envi r onment interface can be found in
Appendix C.
Simulation.java
/**
* Marine Biology Simulation:
* A Si mul at i on object controls a simulation of fish
* movement in an Envi r onment .
*
* @version 1 July 2002
**/

publ i c cl ass Si mul at i on
{
// Instance Variables: Encapsulated data for each simulation object
pr i vat e Envi r onment t heEnv;
pr i vat e EnvDi spl ay t heDi spl ay;

/** Constructs a Simulation object for a particular environment.
* @par amenv the environment on which the simulation will run
* @par amdi spl ay an object that knows how to display the environment
**/
publ i c Si mul at i on( Envi r onment env, EnvDi spl ay di spl ay)
{
t heEnv = env;
t heDi spl ay = di spl ay;

// Display the initial state of the simulation.
t heDi spl ay. showEnv( ) ;
Debug. pr i nt l n( " - - - - - - - - I ni t i al Conf i gur at i on - - - - - - - - " ) ;
Debug. pr i nt l n( t heEnv. t oSt r i ng( ) ) ;
Debug. pr i nt l n( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
}

/** Runs through a single step of this simulation. **/
publ i c voi d st ep( )
{
// Get all the fish in the environment and ask each
// one to perform the actions it does in a timestep.
Locat abl e[ ] t heFi shes = t heEnv. al l Obj ect s( ) ;
f or ( i nt i ndex = 0; i ndex < t heFi shes. l engt h; i ndex++ )
{
( ( Fi sh) t heFi shes[ i ndex] ) . act ( ) ;
}

// Display the state of the simulation after this timestep.
t heDi spl ay. showEnv( ) ;
Debug. pr i nt l n( t heEnv. t oSt r i ng( ) ) ;
Debug. pr i nt l n( " - - - - - - - - End of Ti mest ep - - - - - - - - " ) ;
}
}
-B1-
Appendix B Fish.java

Fish.java (includes breeding and dying modifications from Chapter 3)

i mpor t j ava. awt . Col or ;
i mpor t j ava. ut i l . Ar r ayLi st ;
i mpor t j ava. ut i l . Random;

/**
* Marine Biology Simulation:
* A Fi sh object represents a fish in the Marine Biology
* Simulation. Each fish has a unique ID, which remains constant
* throughout its life. A fish also maintains information about its
* location and direction in the environment.
*
* Modification History:
* - Modified to support a dynamic population in the environment:
* fish can now breed and die.
*
* @version 1 July 2002
**/

publ i c cl ass Fi sh i mpl ement s Locat abl e
{
// Class Variable: Shared among ALL fish
pr i vat e st at i c i nt next Avai l abl eI D = 1; // next avail unique identifier

// Instance Variables: Encapsulated data for EACH fish
pr i vat e Envi r onment t heEnv; // environment in which the fish lives
pr i vat e i nt myI d; // unique ID for this fish
pr i vat e Locat i on myLoc; // fish's location
pr i vat e Di r ect i on myDi r ; // fish's direction
pr i vat e Col or myCol or ; // fish's color
// THE FOLLOWING TWO INSTANCE VARIABLES ARE NEW IN CHAPTER 3 !!!
pr i vat e doubl e pr obOf Br eedi ng; // defines likelihood in each timestep
pr i vat e doubl e pr obOf Dyi ng; // defines likelihood in each timestep


// constructors and related helper methods

/** Constructs a fish at the specified location in a given environment.
* The Fish is assigned a random direction and random color.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
**/
publ i c Fi sh( Envi r onment env, Locat i on l oc)
{
i ni t i al i ze( env, l oc, env. r andomDi r ect i on( ) , r andomCol or ( ) ) ;
}
-B2-
Appendix B Fish.java

/** Constructs a fish at the specified location and direction in a
* given environment. The Fish is assigned a random color.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
* @par amdi r direction the new fish is facing
**/
publ i c Fi sh( Envi r onment env, Locat i on l oc, Di r ect i on di r )
{
i ni t i al i ze( env, l oc, di r , r andomCol or ( ) ) ;
}

/** Constructs a fish of the specified color at the specified location
* and direction.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
* @par amdi r direction the new fish is facing
* @par amcol color of the new fish
**/
publ i c Fi sh( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
{
i ni t i al i ze( env, l oc, di r , col ) ;
}

/** Initializes the state of this fish.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which this fish will live
* @par aml oc location of this fish in env
* @par amdi r direction this fish is facing
* @par amcol color of this fish
**/
pr i vat e voi d i ni t i al i ze( Envi r onment env, Locat i on l oc, Di r ect i on di r ,
Col or col )
{
t heEnv = env;
myI d = next Avai l abl eI D;
next Avai l abl eI D++;
myLoc = l oc;
myDi r = di r ;
myCol or = col ;
t heEnv. add( t hi s) ;

// object is at location myLoc in environment

// THE FOLLOWING INITIALIZATIONS ARE NEW IN CHAPTER 3 !!!
// For now, every fish is equally likely to breed or die in any given
// timestep, although this could be individualized for each fish.
pr obOf Br eedi ng = 1. 0/ 7. 0; // 1 in 7 chance in each timestep
pr obOf Dyi ng = 1. 0/ 5. 0; // 1 in 5 chance in each timestep
}
-B3-
Appendix B Fish.java

/** Generates a random color.
* @r et ur n the new random color
**/
pr ot ect ed Col or r andomCol or ( )
{
// There are 256 possibilities for the red, green, and blue attributes
// of a color. Generate random values for each color attribute.
Randomr andNumGen = RandNumGener at or . get I nst ance( ) ;
r et ur n new Col or ( r andNumGen. next I nt ( 256) , // amount of red
r andNumGen. next I nt ( 256) , // amount of green
r andNumGen. next I nt ( 256) ) ; // amount of blue
}


// accessor methods

/** Returns this fish's ID.
* @r et ur n the unique ID for this fish
**/
publ i c i nt i d( )
{
r et ur n myI d;
}

/** Returns this fish's environment.
* @r et ur n the environment in which this fish lives
**/
publ i c Envi r onment envi r onment ( )
{
r et ur n t heEnv;
}

/** Returns this fish's color.
* @r et ur n the color of this fish
**/
publ i c Col or col or ( )
{
r et ur n myCol or ;
}

/** Returns this fish's location.
* @r et ur n the location of this fish in the environment
**/
publ i c Locat i on l ocat i on( )
{
r et ur n myLoc;
}

/** Returns this fish's direction.
* @r et ur n the direction in which this fish is facing
**/
publ i c Di r ect i on di r ect i on( )
{
r et ur n myDi r ;
}
-B4-
Appendix B Fish.java

/** Checks whether this fish is in an environment.
* @r et ur n t r ue if the fish is in the environment
* (and at the correct location); f al se otherwise
**/
publ i c bool ean i sI nEnv( )
{
r et ur n envi r onment ( ) . obj ect At ( l ocat i on( ) ) == t hi s;
}

/** Returns a string representing key information about this fish.
* @r et ur n a string indicating the fish's ID, location, and direction
**/
publ i c St r i ng t oSt r i ng( )
{
r et ur n i d( ) + l ocat i on( ) . t oSt r i ng( ) + di r ect i on( ) . t oSt r i ng( ) ;
}


// modifier method

// THE FOLLOWING METHOD IS MODIFIED FOR CHAPTER 3 !!!
// (was originally a check for aliveness and a simple call to move)
/** Acts for one step in the simulation.
**/
publ i c voi d act ( )
{
// Make sure fish is alive and well in the environment -- fish
// that have been removed from the environment shouldn't act.
i f ( ! i sI nEnv( ) )
r et ur n;

// Try to breed.
i f ( ! br eed( ) )
// Did not breed, so try to move.
move( ) ;

// Determine whether this fish will die in this timestep.
Randomr andNumGen = RandNumGener at or . get I nst ance( ) ;
i f ( r andNumGen. next Doubl e( ) < pr obOf Dyi ng )
di e( ) ;
}

-B5-
Appendix B Fish.java
// internal helper methods

// THE FOLLOWING METHOD IS NEW FOR CHAPTER 3 !!!
/** Attempts to breed into neighboring locations.
* @r et ur n t r ue if fish successfully breeds;
* f al se otherwise
**/
pr ot ect ed bool ean br eed( )
{
// Determine whether this fish will try to breed in this
// timestep. If not, return immediately.
Randomr andNumGen = RandNumGener at or . get I nst ance( ) ;
i f ( r andNumGen. next Doubl e( ) >= pr obOf Br eedi ng )
r et ur n f al se;

// Get list of neighboring empty locations.
Ar r ayLi st empt yNbr s = empt yNei ghbor s( ) ;
Debug. pr i nt ( " Fi sh " + t oSt r i ng( ) + " at t empt i ng t o br eed. " ) ;
Debug. pr i nt l n( " Has nei ghbor i ng l ocat i ons: " + empt yNbr s. t oSt r i ng( ) ) ;

// If there is nowhere to breed, then we're done.
i f ( empt yNbr s. si ze( ) == 0 )
{
Debug. pr i nt l n( " Di d not br eed. " ) ;
r et ur n f al se;
}

// Breed to all of the empty neighboring locations.
f or ( i nt i ndex = 0; i ndex < empt yNbr s. si ze( ) ; i ndex++ )
{
Locat i on l oc = ( Locat i on) empt yNbr s. get ( i ndex) ;
gener at eChi l d( l oc) ;
}

r et ur n t r ue;
}

// THE FOLLOWING METHOD IS NEW FOR CHAPTER 3 !!!
/** Creates a new fish with the color of its parent.
* @par aml oc location of the new fish
**/
pr ot ect ed voi d gener at eChi l d( Locat i on l oc)
{
// Create new fish, which adds itself to the environment.
Fi sh chi l d = new Fi sh( envi r onment ( ) , l oc,
envi r onment ( ) . r andomDi r ect i on( ) , col or ( ) ) ;
Debug. pr i nt l n( " New Fi sh cr eat ed: " + chi l d. t oSt r i ng( ) ) ;
}

-B6-
Appendix B Fish.java
/** Moves this fish in its environment.
**/
pr ot ect ed voi d move( )
{
// Find a location to move to.
Debug. pr i nt ( " Fi sh " + t oSt r i ng( ) + " at t empt i ng t o move. " ) ;
Locat i on next Loc = next Locat i on( ) ;

// If the next location is different, move there.
i f ( ! next Loc. equal s( l ocat i on( ) ) )
{
// Move to new location.
Locat i on ol dLoc = l ocat i on( ) ;
changeLocat i on( next Loc) ;

// Update direction in case fish had to turn to move.
Di r ect i on newDi r = envi r onment ( ) . get Di r ect i on( ol dLoc, next Loc) ;
changeDi r ect i on( newDi r ) ;
Debug. pr i nt l n( " Moves t o " + l ocat i on( ) + di r ect i on( ) ) ;
}
el se
Debug. pr i nt l n( " Does not move. " ) ;
}

/** Finds this fish's next location.
* A fish may move to any empty adjacent locations except the one
* behind it (fish do not move backwards). If this fish cannot
* move, next Locat i on returns its current location.
* @r et ur n the next location for this fish
**/
pr ot ect ed Locat i on next Locat i on( )
{
// Get list of neighboring empty locations.
Ar r ayLi st empt yNbr s = empt yNei ghbor s( ) ;

// Remove the location behind, since fish do not move backwards.
Di r ect i on opposi t eDi r = di r ect i on( ) . r ever se( ) ;
Locat i on l ocat i onBehi nd = envi r onment ( ) . get Nei ghbor ( l ocat i on( ) ,
opposi t eDi r ) ;
empt yNbr s. r emove( l ocat i onBehi nd) ;
Debug. pr i nt ( " Possi bl e new l ocat i ons ar e: " + empt yNbr s. t oSt r i ng( ) ) ;

// If there are no valid empty neighboring locations, then we're done.
i f ( empt yNbr s. si ze( ) == 0 )
r et ur n l ocat i on( ) ;

// Return a randomly chosen neighboring empty location.
Randomr andNumGen = RandNumGener at or . get I nst ance( ) ;
i nt r andNum= r andNumGen. next I nt ( empt yNbr s. si ze( ) ) ;
r et ur n ( Locat i on) empt yNbr s. get ( r andNum) ;
}

-B7-
Appendix B Fish.java
/** Finds empty locations adjacent to this fish.
* @r et ur n an ArrayList containing neighboring empty locations
**/
pr ot ect ed Ar r ayLi st empt yNei ghbor s( )
{
// Get all the neighbors of this fish, empty or not.
Ar r ayLi st nbr s = envi r onment ( ) . nei ghbor sOf ( l ocat i on( ) ) ;

// Figure out which neighbors are empty and add those to a new list.
Ar r ayLi st empt yNbr s = new Ar r ayLi st ( ) ;
f or ( i nt i ndex = 0; i ndex < nbr s. si ze( ) ; i ndex++ )
{
Locat i on l oc = ( Locat i on) nbr s. get ( i ndex) ;
i f ( envi r onment ( ) . i sEmpt y( l oc) )
empt yNbr s. add( l oc) ;
}

r et ur n empt yNbr s;
}

/** Modifies this fish's location and notifies the environment.
* @par am newLoc new location value
**/
pr ot ect ed voi d changeLocat i on( Locat i on newLoc)
{
// Change location and notify the environment.
Locat i on ol dLoc = l ocat i on( ) ;
myLoc = newLoc;
envi r onment ( ) . r ecor dMove( t hi s, ol dLoc) ;

// object is again at location myLoc in environment
}

/** Modifies this fish's direction.
* @par am newDi r new direction value
**/
pr ot ect ed voi d changeDi r ect i on( Di r ect i on newDi r )
{
// Change direction.
myDi r = newDi r ;
}

// THE FOLLOWING METHOD IS NEW FOR CHAPTER 3 !!!
/** Removes this fish from the environment.
**/
pr ot ect ed voi d di e( )
{
Debug. pr i nt l n( t oSt r i ng( ) + " about t o di e. " ) ;
envi r onment ( ) . r emove( t hi s) ;
}

}
-B8-
Appendix B DarterFish.java
DarterFish.java

i mpor t j ava. awt . Col or ;

/**
* Marine Biology Simulation:
* The Dar t er Fi sh class represents a fish in the Marine
* Biology Simulation that darts forward two spaces if it can, moves
* forward one space if it can't move two, and reverses direction
* (without moving) if it cannot move forward. It can only "see" an
* empty location two cells away if the cell in between is empty also.
* In other words, if both the cell in front of the darter and the cell
* in front of that cell are empty, the darter fish will move forward
* two spaces. If only the cell in front of the darter is empty, it
* will move there. If neither forward cell is empty, the fish will turn
* around, changing its direction but not its location.
*
* Dar t er Fi sh objects inherit instance variables and much
* of their behavior from the Fi sh class.
*
* @version 1 July 2002
**/

publ i c cl ass Dar t er Fi sh ext ends Fi sh
{

// constructors

/** Constructs a darter fish at the specified location in a
* given environment. This darter is colored yellow.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
**/
publ i c Dar t er Fi sh( Envi r onment env, Locat i on l oc)
{
// Construct and initialize the attributes inherited from Fish.
super ( env, l oc, env. r andomDi r ect i on( ) , Col or . yel l ow) ;
}

/** Constructs a darter fish at the specified location and direction in a
* given environment. This darter is colored yellow.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
* @par amdi r direction the new fish is facing
**/
publ i c Dar t er Fi sh( Envi r onment env, Locat i on l oc, Di r ect i on di r )
{
// Construct and initialize the attributes inherited from Fish.
super ( env, l oc, di r , Col or . yel l ow) ;
}

-B9-
Appendix B DarterFish.java
/** Constructs a darter fish of the specified color at the specified
* location and direction.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
* @par amdi r direction the new fish is facing
* @par amcol color of the new fish
**/
publ i c Dar t er Fi sh( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
{
// Construct and initialize the attributes inherited from Fish.
super ( env, l oc, di r , col ) ;
}


// redefined methods

/** Creates a new darter fish.
* @par aml oc location of the new fish
**/
pr ot ect ed voi d gener at eChi l d( Locat i on l oc)
{
// Create new fish, which adds itself to the environment.
Dar t er Fi sh chi l d = new Dar t er Fi sh( envi r onment ( ) , l oc,
envi r onment ( ) . r andomDi r ect i on( ) ,
col or ( ) ) ;
Debug. pr i nt l n( " New Dar t er Fi sh cr eat ed: " + chi l d. t oSt r i ng( ) ) ;
}

/** Moves this fish in its environment.
* A darter fish darts forward (as specified in next Locat i on) if
* possible, or reverses direction (without moving) if it cannot move
* forward.
**/
pr ot ect ed voi d move( )
{
// Find a location to move to.
Debug. pr i nt ( " Dar t er Fi sh " + t oSt r i ng( ) + " at t empt i ng t o move. " ) ;
Locat i on next Loc = next Locat i on( ) ;

// If the next location is different, move there.
i f ( ! next Loc. equal s( l ocat i on( ) ) )
{
changeLocat i on( next Loc) ;
Debug. pr i nt l n( " Moves t o " + l ocat i on( ) ) ;
}
el se
{
// Otherwise, reverse direction.
changeDi r ect i on( di r ect i on( ) . r ever se( ) ) ;
Debug. pr i nt l n( " Now f aci ng " + di r ect i on( ) ) ;
}
}

-B10-
Appendix B DarterFish.java
-B11-
/** Finds this fish's next location.
* A darter fish darts forward two spaces if it can, otherwise it
* tries to move forward one space. A darter fish can only move
* to empty locations, and it can only move two spaces forward if
* the intervening space is empty. If the darter fish cannot move
* forward, next Locat i on returns the fish's current
* location.
* @r et ur n the next location for this fish
**/
pr ot ect ed Locat i on next Locat i on( )
{
Envi r onment env = envi r onment ( ) ;
Locat i on oneI nFr ont = env. get Nei ghbor ( l ocat i on( ) , di r ect i on( ) ) ;
Locat i on t woI nFr ont = env. get Nei ghbor ( oneI nFr ont , di r ect i on( ) ) ;
Debug. pr i nt l n( " Locat i on i n f r ont i s empt y? " +
env. i sEmpt y( oneI nFr ont ) ) ;
Debug. pr i nt l n( " Locat i on i n f r ont of t hat i s empt y? " +
env. i sEmpt y( t woI nFr ont ) ) ;
i f ( env. i sEmpt y( oneI nFr ont ) )
{
i f ( env. i sEmpt y( t woI nFr ont ) )
r et ur n t woI nFr ont ;
el se
r et ur n oneI nFr ont ;
}

// Only get here if there isn't a valid location to move to.
Debug. pr i nt l n( " Dar t er i s bl ocked. " ) ;
r et ur n l ocat i on( ) ;
}

}
Appendix B SlowFish.java
-B12-
SlowFish.java

i mpor t j ava. awt . Col or ;
i mpor t j ava. ut i l . Ar r ayLi st ;
i mpor t j ava. ut i l . Random;

/**
* Marine Biology Simulation:
* The Sl owFi sh class represents a fish in the Marine Biology
* Simulation that moves very slowly. It moves so slowly that it only has
* a 1 in 5 chance of moving out of its current cell into an adjacent cell
* in any given timestep in the simulation. When it does move beyond its
* own cell, its movement behavior is the same as for objects of the
* Fi sh class.
*
* Sl owFi sh objects inherit instance variables and much of
* their behavior from the Fi sh class.
*
* @version 1 July 2002
**/

publ i c cl ass Sl owFi sh ext ends Fi sh
{
// Instance Variables: Encapsulated data for EACH slow fish
pr i vat e doubl e pr obOf Movi ng; // defines likelihood in each timestep


// constructors

/** Constructs a slow fish at the specified location in a
* given environment. This slow fish is colored red.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
**/
publ i c Sl owFi sh( Envi r onment env, Locat i on l oc)
{
// Construct and initialize the attributes inherited from Fish.
super ( env, l oc, env. r andomDi r ect i on( ) , Col or . r ed) ;

// Define the likelihood that a slow fish will move in any given
// timestep. For now this is the same value for all slow fish.
pr obOf Movi ng = 1. 0/ 5. 0; // 1 in 5 chance in each timestep
}

Appendix B SlowFish.java
-B13-
/** Constructs a slow fish at the specified location and direction in a
* given environment. This slow fish is colored red.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
* @par amdi r direction the new fish is facing
**/
publ i c Sl owFi sh( Envi r onment env, Locat i on l oc, Di r ect i on di r )
{
// Construct and initialize the attributes inherited from Fish.
super ( env, l oc, di r , Col or . r ed) ;

// Define the likelihood that a slow fish will move in any given
// timestep. For now this is the same value for all slow fish.
pr obOf Movi ng = 1. 0/ 5. 0; // 1 in 5 chance in each timestep
}

/** Constructs a slow fish of the specified color at the specified
* location and direction.
* (Precondition: parameters are non-null; l oc is valid
* for env.)
* @par amenv environment in which fish will live
* @par aml oc location of the new fish in env
* @par amdi r direction the new fish is facing
* @par amcol color of the new fish
**/
publ i c Sl owFi sh( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
{
// Construct and initialize the attributes inherited from Fish.
super ( env, l oc, di r , col ) ;

// Define the likelihood that a slow fish will move in any given
// timestep. For now this is the same value for all slow fish.
pr obOf Movi ng = 1. 0/ 5. 0; // 1 in 5 chance in each timestep
}


// redefined methods

/** Creates a new slow fish.
* @par aml oc location of the new fish
**/
pr ot ect ed voi d gener at eChi l d( Locat i on l oc)
{
// Create new fish, which adds itself to the environment.
Sl owFi sh chi l d = new Sl owFi sh( envi r onment ( ) , l oc,
envi r onment ( ) . r andomDi r ect i on( ) ,
col or ( ) ) ;
Debug. pr i nt l n( " New Sl owFi sh cr eat ed: " + chi l d. t oSt r i ng( ) ) ;
}

Appendix B SlowFish.java
-B14-
/** Finds this fish's next location. A slow fish moves so
* slowly that it may not move out of its current cell in
* the environment.
**/
pr ot ect ed Locat i on next Locat i on( )
{
// There's only a small chance that a slow fish will actually
// move in any given timestep, defined by probOfMoving.
Randomr andNumGen = RandNumGener at or . get I nst ance( ) ;
i f ( r andNumGen. next Doubl e( ) < pr obOf Movi ng )
r et ur n super . next Locat i on( ) ;
el se
{
Debug. pr i nt l n( " Sl owFi sh " + t oSt r i ng( ) +
" not at t empt i ng t o move. " ) ;
r et ur n l ocat i on( ) ;
}
}

}
Appendix C Black Box Classes
-C1-
Appendix C -- Black Box Classes
This appendix contains summary class documentation for the Envi r onment interface and the MBS
utility classes covered in Chapters 1 - 4 (Debug, Di r ect i on, EnvDi spl ay, Locat abl e, Locat i on, and
RandNumGener at or ).
Environment interface
publ i c i nt numRows( )
Returns number of rows in this environment.

publ i c i nt numCols( )
Returns number of columns in this environment.

publ i c bool ean isValid( Locat i on l oc)
Returns t r ue if l oc is valid in this environment; otherwise returns f al se.

publ i c i nt numCellSides( )
Returns the number of sides around each cell.

publ i c i nt numAdjacentNeighbors( )
Returns the number of adjacent neighbors around each cell.

publ i c Di r ect i on randomDirection( )
Generates a random direction. The direction returned by r andomDi r ect i on reflects the direction from a cell
in the environment to one of its adjacent neighbors.

publ i c Di r ect i on getDirection( Locat i on f r omLoc, Locat i on t oLoc)
Returns the direction from one location to another.

publ i c Locat i on getNeighbor( Locat i on f r omLoc, Di r ect i on compassDi r )
Returns the adjacent neighboring location of a location in the specified direction (whether valid or invalid).

publ i c j ava. ut i l . Ar r ayLi st neighborsOf( Locat i on of Loc)
Returns the adjacent neighboring locations of a specified location. Only neighbors that are valid locations in the
environment will be included.

publ i c i nt numObjects( )
Returns the number of objects in this environment.

publ i c Locat abl e[ ] allObjects( )
Returns all the objects in this environment.

publ i c bool ean isEmpty( Locat i on l oc)
Returns t r ue if l oc is a valid location in the context of this environment and is empty; f al se otherwise.

publ i c Locat abl e objectAt( Locat i on l oc)
Returns the object at location l oc; null if l oc is not in the environment or is empty.

publ i c voi d add( Locat abl e obj )
Adds a new object to this environment at the location it specifies.
(Precondition: obj . l ocat i on( ) is a valid empty location.)

Appendix C Black Box Classes
-C2-
publ i c voi d remove( Locat abl e obj )
Removes the object from this environment.
(Precondition: obj is in this environment.)

publ i c voi d recordMove( Locat abl e obj , Locat i on ol dLoc)
Updates this environment to reflect the fact that an object moved.
(Precondition: obj . l ocat i on( ) is a valid location and there is no other object there.
Postcondition: obj is at the appropriate location (obj . l ocat i on( ) ), and either ol dLoc is equal to
obj . l ocat i on( ) (there was no movement) or ol dLoc is empty.)

Debug class
publ i c st at i c bool ean isOn( )
Checks whether debugging is on (not necessary when using Debug. pr i nt and Debug. pr i nt l n).

publ i c st at i c bool ean isOff( )
Checks whether debugging is off (not necessary when using Debug. pr i nt and Debug. pr i nt l n).

publ i c st at i c voi d turnOn( )
Turns debugging on.

publ i c st at i c voi d turnOff( )
Turns debugging off.

publ i c st at i c voi d restoreState( )
Restores the previous debugging state. If there is no previous state to restore, r est or eSt at e turns debugging
off.

publ i c st at i c voi d print( St r i ng message)
Prints debugging message without appending a newline character at the end. If debugging is turned on, message
is printed to Syst em. out without a newline.

publ i c st at i c voi d println( St r i ng message)
Prints debugging message, appending a newline character at the end. If debugging is turned on, message is
printed to Syst em. out followed by a newline.

Appendix C Black Box Classes
-C3-
Direction class
Public class constants: NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST,
WEST, NORTHWEST
Note: these are class constants of type Di r ect i on (e.g., Di r ect i on. NORTH) that represent compass
directions in degrees (0, 45, 90, 135, 180, 225, 270, 315, respectively)

publ i c Direction( )
Constructs a default Di r ect i on object facing North.

publ i c Direction( i nt degr ees)
Constructs a Di r ect i on object - initial compass direction in degrees.

publ i c Direction( St r i ng st r )
Constructs a Di r ect i on object - compass direction specified as a string, e.g. "North".

publ i c i nt inDegrees( )
Returns this direction value in degrees.

publ i c bool ean equals( Obj ect ot her )
Returns true if other has the same number of degrees as this direction; false otherwise.

publ i c Di r ect i on toRight( )
Returns the direction that is a quarter turn to the right of this Di r ect i on object.

publ i c Di r ect i on toRight( i nt deg)
Returns the direction that is deg degrees to the right of this Di r ect i on object.

publ i c Di r ect i on toLeft( )
Returns the direction that is a quarter turn to the left of this Di r ect i on object.

publ i c Di r ect i on toLeft( i nt deg)
Returns the direction that is deg degrees to the left of this Di r ect i on object.

publ i c Di r ect i on reverse( )
Returns the direction that is the reverse of this Di r ect i on object.

publ i c St r i ng toString( )
Returns a string indicating the direction.

publ i c st at i c Di r ect i on randomDirection( )
Returns a random direction in the range of [0, 360) degrees.

Appendix C Black Box Classes
-C4-
EnvDisplay interface
publ i c voi d showEnv( )
Shows the current state of the environment.

Locatable interface
publ i c Locat i on location( )
Returns the location of this object.

Location class (implements Comparable)
publ i c Location( i nt r ow, i nt col )
Constructs a Locat i on object.

publ i c i nt row( )
Returns the row coordinate of this location.

publ i c i nt col( )
Returns the column coordinate of this location.

publ i c bool ean equals( Obj ect ot her )
Returns t r ue if ot her is at the same row and column as the current location; f al se otherwise.

publ i c i nt compareTo( Obj ect ot her )
Compares this location to ot her for ordering. Returns a negative integer, zero, or a positive integer as this
location is less than, equal to, or greater than ot her . Locations are ordered in row-major order.
(Precondition: ot her is a Locat i on object.)

publ i c St r i ng toString( )
Returns a string indicating the row and column of the location in (row, col) format.

RandNumGenerator class
publ i c st at i c j ava. ut i l . RandomgetInstance( )
Returns a random number generator. Always returns the same Randomobject to provide a better sequence of
random numbers.


Appendix E J MBS Quick Reference
-E1-
Appendix E -- A Exam JMBS Quick Reference

Quick Reference for Core Classes and Interfaces



Simulation Class

publ i c Simulation( Envi r onment env, EnvDi spl ay di spl ay)
publ i c voi d step( )

Environment Interface

publ i c i nt numRows( )
publ i c i nt numCols( )

publ i c bool ean isValid( Locat i on l oc)
publ i c i nt numCellSides( )
publ i c i nt numAdjacentNeighbors ) (
publ i c Di r ect i on randomDirection( )
publ i c Di r ect i on getDirection( Locat i on f r omLoc, Locat i on t oLoc)
publ i c Locat i on getNeighbor Locat i on f r omLoc, Di r ect i on compassDi r ) (
publ i c Ar r ayLi st neighborsOf( Locat i on of Loc)

publ i c i nt numObjects( )
publ i c Locat abl e[ ] allObjects( )
publ i c bool ean isEmpty( Locat i on l oc)
publ i c Locat abl e objectAt( Locat i on l oc)

publ i c voi d add( Locat abl e obj )
publ i c voi d remove( Locat abl e obj )
publ i c voi d recordMove( Locat abl e obj , Locat i on ol dLoc)













Appendix E J MBS Quick Reference
-E2-
Quick Reference for Fish Class

Fish Class (implements Locatable)

publ i c Fish( Envi r onment env, Locat i on l oc)
publ i c Fish( Envi r onment env, Locat i on l oc, Di r ect i on di r )
publ i c Fish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
private void initialize(Environment env, Location loc, Direction dir,
Color col)
protected Color randomColor()

publ i c i nt id( )
publ i c Envi r onment environment( )
publ i c Col or color( )
publ i c Locat i on location ( )
publ i c Di r ect i on direction( )
publ i c bool ean isInEnv( )
publ i c St r i ng toString( )

publ i c voi d act( )

protected boolean breed()
protected void generateChild(Location loc)
protected void move()
protected Location nextLocation()
protected ArrayList emptyNeighbors()
protected void changeLocation(Location newLoc)
protected void changeDirection(Direction newDir)
protected void die()


Quick Reference for Specialized Fish Subclasses

DarterFish Class (extends Fish)

publ i c DarterFish( Envi r onment env, Locat i on l oc)
publ i c DarterFish( Envi r onment env, Locat i on l oc, Di r ect i on di r )
publ i c DarterFish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )

protected void generateChild(Location loc)
protected void move()
protected Location nextLocation()

SlowFish Class (extends Fish)

publ i c SlowFish( Envi r onment env, Locat i on l oc)
publ i c SlowFish( Envi r onment env, Locat i on l oc, Di r ect i on di r )
publ i c SlowFish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )

protected void generateChild(Location loc)
protected Location nextLocation()
Appendix E J MBS Quick Reference
Quick Reference for Utility Classes and Interfaces
(public constants, constructors, and methods only)


Case Study Utility Classes and Interfaces

Debug Class

st at i c bool ean isOn( )
st at i c bool ean isOff( )
st at i c voi d turnOn( )
st at i c voi d turnOff( )
st at i c voi d restoreState( )
st at i c voi d print( St r i ng message)
st at i c voi d println( St r i ng message)

EnvDisplay Interface

voi d showEnv( )

Locatable Interface

Locat i on location( )



-E3-








Location Class
(implements Comparable)

Location( i nt r ow, i nt col )
i nt row( )
i nt col( )
bool equals( Obj ect ot her ) ean
i nt compareTo( Obj ect ot her )
St r i ng toString( )








Direction Class

NORTH, EAST, SOUTH, WEST, NORTHEAST,
NORTHWEST, SOUTHEAST, SOUTHWEST

Direction( )
Direction( i nt degr ees)
Direction( St r i ng st r )
i nt inDegrees( )
bool ean equals( Obj ect ot her )
Di r ect i on toRight( )
Di r ect i on toRight( i nt degr ees)
Di r ect i on toLeft( )
Di r ect i on toLeft( i nt degr ees)
Di r ect i reverse( ) on
St r i ng toString( )
st at i c Di r ect i on randomDirection( )

RandNumGenerator Class

st at i c RandomgetInstance( )

Appendix E J MBS Quick Reference
-E4-
Java Library Utility Classes

java.util.ArrayList Class(Partial)

bool ean add( Obj ect obj )
voi d add( i nt i ndex, Obj ect obj )
Obj ect get t i ndex) ( i n
Obj ect remove( i nt i ndex)
bool ean remove( Obj ect obj )
Obj e set( i nt i ndex, Obj ect obj ) ct
i nt size( )


java.awt.Color Class(Partial)

black, blue, cyan, gray, green,
magenta, orange, pink, red,
white, yellow

Color( i nt r , i nt g, i nt b)

java.util.Random Class(Partial)

i nt nextInt ) ( i nt n
doubl e nextDouble( )

Appendix G Source Code Index
-G1-
Appendix G: Index for Source Code
This appendix provides an index for the J ava source code found in Appendix B. Methods are grouped
in alternating gray and white blocks based on the page on which they are listed.
Simulation.java
Simulation( Envi r onment env, EnvDi spl ay di spl ay)
B1
step( )
B1

Fish.java
Fish( Envi r onment env, Locat i on l oc)
B2
Fish( Envi r onment env, Locat i on l oc, Di r ect i on di r )
B3
Fish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
B3
initialize( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
B3
randomColor( )
B4
id( )
B4
environment( )
B4
color( )
B4
location( )
B4
direction( )
B4
isInEnv( )
B5
toString( )
B5
act( )
B5
breed( )
B6
generateChild( Locat i on l oc)
B6
move( )
B7
nextLocation( )
B7
emptyNeighbors( )
B8
changeLocation( Locat i on newLoc)
B8
changeDirection( Di r ect i on newDi r )
B8
die( )
B8

DarterFish.java
DarterFish( Envi r onment env, Locat i on l oc)
B9
DarterFish( Envi r onment env, Locat i on l oc, Di r ect i on di r )
B9
DarterFish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
B10
generateChild( Locat i on l oc)
B10
move( )
B10
nextLocation( )
B11

-G2-
SlowFish.java
SlowFish( Envi r onment env, Locat i on l oc)
B12
SlowFish( Envi r onment env, Locat i on l oc, Di r ect i on di r )
B13
SlowFish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )
B13
generateChild( Locat i on l oc)
B13
nextLocation( )
B14

You might also like