A Reference Materials Giventostudentsduringexam

download A Reference Materials Giventostudentsduringexam

of 29

Transcript of A Reference Materials Giventostudentsduringexam

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    1/29

    APComputer 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.

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    2/29

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    3/29

    Content of Appendices

    Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . A Exam Java Quick Reference

    Appendix B . . . . . . . . . . . . . . . . . . . . . . . . . Source Code for Visible Classes

    Appendix C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Black Box Classes

    Appendix E . . . . . . . . . . . . . . . . . . . . . . . . . A Exam JMBS Quick Reference

    Appendix G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Index for Source Code

    - i -

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    4/29

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    5/29

    Appendix A A Java Quick Reference

    Appendix A -- A Exam Java Quick Reference

    Accessible Methods from the Java 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 ifthis is less thanother// returns a value = 0 ifthis is equal toother// returns a value > 0 ifthis is greater thanother

    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 atfrom

    // and ending atto-1

    String substring(int from) // returnssubstring(from, length())

    int indexOf(String str) // returns the index of the first occurrence ofstr;// 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 adouble in the range [0.0, 1.0)

    class java.util.ArrayList

    int size()

    boolean add(E obj) // appendsobj to end of list; returnstrue

    void add(int index, E obj) // insertsobj at positionindex (0 ,

    // moving elements at positionindex 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 positionindex withobj

    // returns the element formerly at the specified position

    E remove(int index) // removes element from positionindex,moving elements// at positionindex + 1 and higher to the left

    // (subtracts 1 from their indices) and adjusts size// returns the element formerly at the specified position

    *The AP Java subset uses the "raw"Comparable interface, not the genericComparable interface.

    -A1-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    6/29

    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 inAppendix C.

    Simulation.java

    /**

    * Marine Biology Simulation:

    * A Si mul at i onobject 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

    pri 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.

    * @param env the environment on which the simulation will run* @param di spl ayan 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 gurat 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 mestep - - - - - - - - ") ;

    }}

    -B1-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    7/29

    Appendix B Fish.java

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

    i mpor t j ava. awt . Col or ;i mport j ava. ut i l . Arr ayLi st ;i mpor t j ava. ut i l . Random;

    /**

    * Marine Biology Simulation:

    * A Fi shobject 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 Locatabl e{

    // Class Variable: Shared among ALL fishpr 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 fishpri vat e Envi r onment t heEnv; // environment in which the fish livespr i vat e i nt myI d; // unique ID for this fishpr i vat e Locat i on myLoc; // fish's location

    pr i vat e Di r ect i on myDi r ; // fish's directionpr 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 timesteppr 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 ocis valid* for env.)* @param env environment in which fish will live

    * @par am l 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 ecti on( ) , r andomCol or ( ) ) ;}

    -B2-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    8/29

    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 ocis valid* for env.)* @param env environment in which fish will live* @par am l oc location of the new fish in env

    * @par am di 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 , randomCol or( ) ) ;}

    /** Constructs a fish of the specified color at the specified location

    * and direction.

    * (Precondition: parameters are non-null; l ocis valid* for env.)* @param env environment in which fish will live* @par am l oc location of the new fish in env* @par am di r direction the new fish is facing* @param col 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 ocis valid* for env.)* @param env environment in which this fish will live* @par am l oc location of this fish in env* @par am di r direction this fish is facing

    * @param col 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 ecti 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.probOf Br eedi ng = 1. 0/ 7. 0; // 1 in 7 chance in each timestepprobOf Dyi ng = 1. 0/ 5. 0; // 1 in 5 chance in each timestep

    }

    -B3-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    9/29

    Appendix B Fish.java

    /** Generates a random color.

    * @r et urn the new random color**/

    pr otect 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.Random r andNumGen = RandNumGener at or . get I nst ance( ) ;r etur n new Col or ( r andNumGen. next I nt ( 256) , // amount of red

    r andNumGen. next I nt ( 256) , // amount of greenr andNumGen. next I nt ( 256) ) ; // amount of blue

    }

    // accessor methods

    /** Returns this fish's ID.

    * @r et urn 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 urn 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 urn 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 urn the location of this fish in the environment**/

    publ i c Locat i on l ocat i on( ){

    r etur n myLoc;}

    /** Returns this fish's direction.

    * @r et urn the direction in which this fish is facing**/

    publ i c Di r ecti on di r ecti on( ){

    r et ur n myDi r ;}

    -B4-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    10/29

    Appendix B Fish.java

    /** Checks whether this fish is in an environment.

    * @r et urn trueif the fish is in the environment* (and at the correct location); f al seotherwise**/

    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 urn a string indicating the fish's ID, location, and direction**/

    publ i c St r i ng t oSt r i ng( ){

    ret urn i d( ) + l ocat i on( ) . t oSt r i ng( ) + di rect 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( ) )

    return;

    // Try to breed.i f ( ! breed( ) )

    // Did not breed, so try to move.move( ) ;

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

    di e( ) ;}

    -B5-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    11/29

    Appendix B Fish.java

    // internal helper methods

    // THE FOLLOWING METHOD IS NEW FOR CHAPTER 3 !!!/** Attempts to breed into neighboring locations.

    * @r et urn trueif fish successfully breeds;* f al seotherwise**/

    pr otect ed bool ean br eed( ){// Determine whether this fish will try to breed in this// timestep. If not, return immediately.Random r andNumGen = RandNumGener at or . get I nst ance( ) ;i f ( r andNumGen. next Doubl e( ) >= probOf Br eedi ng )

    return f al se;

    // Get list of neighboring empty locations.Ar r ayLi st emptyNbrs = emptyNei 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. ") ;return 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 am l 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 creat ed: " + chi l d. t oSt r i ng( ) ) ;

    }

    -B6-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    12/29

    Appendix B Fish.java

    /** Moves this fish in its environment.

    **/pr ot ected 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 nextLoc = nextLocat 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( nextLoc) ;

    // 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, nextLoc) ;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, nextLocat i onreturns its current location.* @r et urn the next location for this fish**/

    pr ot ect ed Locat i on nextLocat i on( ){

    // Get list of neighboring empty locations.Ar r ayLi st emptyNbrs = emptyNei ghbor s( ) ;

    // Remove the location behind, since fish do not move backwards.Di r ecti on opposi t eDi r = di r ecti 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 urn l ocat i on( ) ;

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

    }

    -B7-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    13/29

    Appendix B Fish.java

    /** Finds empty locations adjacent to this fish.

    * @r et urn an ArrayList containing neighboring empty locations**/

    pr otect ed Ar r ayLi st empt yNei ghbors ( ){

    // 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 etur n emptyNbr s;}

    /** Modifies this fish's location and notifies the environment.

    * @par am newLoc new location value**/

    pr ot ected 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 otect 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-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    14/29

    Appendix B DarterFish.java

    DarterFish.java

    i mpor t j ava. awt . Col or ;

    /**

    * Marine Biology Simulation:* The Dar t er Fi shclass 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 shobjects inherit instance variables and much* of their behavior from the Fi shclass.** @version 1 July 2002

    **/

    publ i c cl ass Dar t er Fi sh extends 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 ocis valid* for env.)

    * @param env environment in which fish will live* @par am l 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 ocis valid* for env.)* @param env environment in which fish will live* @par am l oc location of the new fish in env* @par am di 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-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    15/29

    Appendix B DarterFish.java

    /** Constructs a darter fish of the specified color at the specified

    * location and direction.

    * (Precondition: parameters are non-null; l ocis valid* for env.)* @param env environment in which fish will live* @par am l oc location of the new fish in env* @par am di r direction the new fish is facing

    * @param col 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 am l 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 creat ed: " + chi l d. t oSt r i ng( ) ) ;}

    /** Moves this fish in its environment.

    * A darter fish darts forward (as specified in nextLocat i on) if* possible, or reverses direction (without moving) if it cannot move

    * forward.

    **/pr ot ected 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 nextLoc = nextLocat i on( ) ;

    // If the next location is different, move there.i f ( ! next Loc. equal s( l ocat i on( ) ) ){

    changeLocat i on( nextLoc) ;Debug. pr i nt l n( " Moves t o " + l ocat i on( ) ) ;

    }el se{

    // Otherwise, reverse direction.changeDi r ecti on( di r ecti on( ) . r ever se( ) ) ;Debug. pr i nt l n( " Now f aci ng " + di r ect i on( ) ) ;

    }}

    -B10-

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    16/29

    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, nextLocat i onreturns the fish's current* location.

    * @r et urn the next location for this fish**/

    pr ot ect ed Locat i on nextLocat i on( ){

    Envi r onment env = envi r onment ( ) ;Locat i on oneI nFront = env. get Nei ghbor ( l ocat i on( ) , di r ect i on( ) ) ;Locat i on t woI nFront = env. get Nei ghbor ( oneI nFront , 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 nFront ) ) ;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 nFront ) ) ;i f ( env. i sEmpt y( oneI nFront ) ){

    i f ( env. i sEmpt y( t woI nFront ) )r et ur n t woI nFront ;

    el ser et ur n oneI nFront ;

    }

    // 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. ") ;retur n l ocat i on( ) ;

    }

    }

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    17/29

    Appendix B SlowFish.java

    -B12-

    SlowFish.java

    i mpor t j ava. awt . Col or ;i mport j ava. ut i l . Arr ayLi st ;i mpor t j ava. ut i l . Random;

    /**

    * Marine Biology Simulation:

    * The Sl owFi shclass 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 shclass.*

    * Sl owFi shobjects inherit instance variables and much of* their behavior from the Fi shclass.*

    * @version 1 July 2002

    **/

    publ i c cl ass Sl owFi sh extends Fi sh{

    // Instance Variables: Encapsulated data for EACH slow fishpr 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 ocis valid

    * for env.)* @param env environment in which fish will live* @par am l 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

    }

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    18/29

    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 ocis valid* for env.)* @param env environment in which fish will live* @par am l oc location of the new fish in env* @par am di 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. red) ;

    // 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 ocis valid* for env.)* @param env environment in which fish will live* @par am l oc location of the new fish in env* @par am di r direction the new fish is facing* @param col 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 am l 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 creat ed: " + chi l d. t oSt r i ng( ) ) ;}

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    19/29

    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 nextLocat i on( ){

    // There's only a small chance that a slow fish will actually

    // move in any given timestep, defined by probOfMoving.Random r andNumGen = RandNumGener at or . get I nst ance( ) ;i f ( r andNumGen. next Doubl e( ) < probOf 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 urn l ocat i on( ) ;}

    }

    }

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    20/29

    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 MBSutility classes covered in Chapters 1 - 4 (Debug, Di rect 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 trueif l ocis 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 ecti on randomDirection( )

    Generates a random direction. The direction returned by r andomDi r ect i onreflects the direction from a cellin the environment to one of its adjacent neighbors.

    publ i c Di r ecti 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 . Arr ayLi st neighborsOf( Locat i on of Loc)

    Returns the adjacent neighboring locations of a specified location. Only neighbors that are valid locations in theenvironment 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 trueif l ocis a valid location in the context of this environment and is empty; f al seotherwise.

    publ i c Locat abl e objectAt( Locat i on l oc)

    Returns the object at location l oc; null if l ocis 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.)

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    21/29

    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 dLocis equal toobj . l ocat i on( ) (there was no movement) or ol dLocis 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 stat i c voi d turnOn( )

    Turns debugging on.

    publ i c stat i c voi d turnOff( )

    Turns debugging off.

    publ i c stat i c voi d restoreState( )

    Restores the previous debugging state. If there is no previous state to restore, restoreStateturns debuggingoff.

    publ i c stat i c voi dprint( 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 stat i c voi dprintln( St r i ng message)Prints debugging message, appending a newline character at the end. If debugging is turned on, messageis

    printed to Syst em. out followed by a newline.

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    22/29

    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 ecti 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 rect i onobject facing North.

    publ i c Direction( i nt degr ees)

    Constructs a Di r ecti onobject - initial compass direction in degrees.

    publ i c Direction( St r i ng s t r )

    Constructs a Di r ecti onobject - 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 )Returnstrue ifother has the same number of degrees as this direction;false otherwise.

    publ i c Di r ecti on toRight( )

    Returns the direction that is a quarter turn to the right of this Di rect i onobject.

    publ i c Di r ecti on toRight( i nt deg)

    Returns the direction that is degdegrees to the right of this Di r ecti onobject.

    publ i c Di r ecti on toLeft( )

    Returns the direction that is a quarter turn to the left of this Di rect i onobject.

    publ i c Di r ecti on toLeft( i nt deg)

    Returns the direction that is degdegrees to the left of this Di rect i onobject.

    publ i c Di r ecti on reverse( )

    Returns the direction that is the reverse of this Di r ecti onobject.

    publ i c St r i ng toString( )

    Returns a string indicating the direction.

    publ i c stat i c Di rect i on randomDirection( )

    Returns a random direction in the range of [0, 360) degrees.

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    23/29

    Appendix C Black Box Classes

    -C4-

    EnvDisplay interface

    publ i c voi d showEnv( )

    Shows the current state of the environment.

    Locatable interfacepubl i c Locat i on location( )

    Returns the location of this object.

    Location class (implements Comparable)

    publ i c Location( i nt row, i nt col )

    Constructs a Locat i onobject.

    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 trueif other is at the same row and column as the current location; f al seotherwise.

    publ i c i nt compareTo( Obj ect ot her )

    Compares this location to other 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 onobject.)

    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 . Random getInstance( )

    Returns a random number generator. Always returns the same Randomobject to provide a better sequence ofrandom numbers.

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    24/29

    Appendix E JMBS 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 ecti on randomDirection( )publ i c Di r ecti 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)

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    25/29

    Appendix E JMBS Quick Reference

    -E2-

    Quick Reference for Fish Class

    Fish Class (implements Locatable)

    publ i cFish( Envi r onment env, Locat i on l oc)publ i cFish( Envi r onment env, Locat i on l oc, Di r ect i on di r )

    publ i cFish( Envi r onment env, Locat i on l oc, Di r ect i on di r , Col or col )private voidinitialize(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 ecti on direction( )publ i c bool ean isInEnv( )publ i c St r i ng toString( )

    publ i c voi d act( )

    protected booleanbreed()

    protected voidgenerateChild(Location loc)protected voidmove()

    protected LocationnextLocation()protected ArrayListemptyNeighbors()

    protected voidchangeLocation(Location newLoc)protected voidchangeDirection(Direction newDir)

    protected voiddie()

    Quick Reference for Specialized Fish SubclassesDarterFish Class (extends Fish)

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

    protected voidgenerateChild(Location loc)protected voidmove()protected LocationnextLocation()

    SlowFish Class (extends Fish)

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

    protected voidgenerateChild(Location loc)

    protected LocationnextLocation()

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    26/29

    Appendix E JMBS 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( )stati c voi d turnOn( )stati c voi d turnOff( )stati c voi d restoreState( )stati c voi dprint( St r i ng message)stati c voi dprintln( St r i ng message)

    EnvDisplay Interface

    voi d showEnv( )

    Locatable Interface

    Locat i on location( )

    -E3-

    Location Class

    (implements Comparable)

    Location( i nt row, i nt col )i nt row( )i nt col( )bool equals( Obj ect ot her )eani 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 s t r )i nt inDegrees( )bool ean equals( Obj ect ot her )Di r ecti on toRight( )Di r ecti on toRight( i nt degr ees)Di r ecti on toLeft( )Di r ecti on toLeft( i nt degr ees)

    Di r ecti reverse( )onSt r i ng toString( )stati c Di rect i on randomDirection( )

    RandNumGenerator Class

    st at i c RandomgetInstance( )

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    27/29

    Appendix E JMBS 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 nObj ect remove( i nt i ndex)bool ean remove( Obj ect obj )Obj e set( i nt i ndex, Obj ect obj )cti 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 ntnextInt )( i nt ndoubl enextDouble( )

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    28/29

    Appendix G Source Code Index

    -G1-

    Appendix G: Index for Source CodeThis appendix provides an index for the Java 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( )B6generateChild( 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

  • 8/12/2019 A Reference Materials Giventostudentsduringexam

    29/29

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