AP® Computer Science Marine Biology Simulation

Class Direction

java.lang.Object
  |
  +--Direction

public class Direction
extends java.lang.Object

AP® Computer Science Marine Biology Simulation:
The Direction class encapsulates the notion of a compass direction such as North, East, South, West.

The Direction class is copyright© 2002 College Entrance Examination Board (www.collegeboard.com).

Version:
1 July 2002

Field Summary
static Direction EAST
           
static int FULL_CIRCLE
          Number of degrees in compass (will not be tested on the Advanced Placement exam).
static Direction NORTH
           
static Direction NORTHEAST
           
static Direction NORTHWEST
           
static Direction SOUTH
           
static Direction SOUTHEAST
           
static Direction SOUTHWEST
           
static Direction WEST
           
 
Constructor Summary
Direction()
          Constructs a default Direction object facing North.
Direction(int degrees)
          Constructs a Direction object.
Direction(java.lang.String str)
          Constructs a Direction object.
 
Method Summary
 boolean equals(java.lang.Object other)
          Indicates whether some other Direction object is "equal to" this one.
 int hashCode()
          Generates a hash code for this direction (will not be tested on the Advanced Placement exam).
 int inDegrees()
          Returns this direction value in degrees.
static Direction randomDirection()
          Returns a random direction.
 Direction reverse()
          Returns the direction that is the reverse of this Direction object.
 Direction roundedDir(int numDirections, Direction startingDir)
          Rounds this direction to the nearest "cardinal" direction (will not be tested on the Advanced Placement exam).
The choice of possible cardinal directions depends on the number of cardinal directions and the starting direction.
 Direction toLeft()
          Returns the direction that is a quarter turn to the left of this Direction object.
 Direction toLeft(int deg)
          Returns the direction that is deg degrees to the left of this Direction object.
 Direction toRight()
          Returns the direction that is a quarter turn to the right of this Direction object.
 Direction toRight(int deg)
          Returns the direction that is deg degrees to the right of this Direction object.
 java.lang.String toString()
          Represents this direction as a string.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NORTH

public static final Direction NORTH

NORTHEAST

public static final Direction NORTHEAST

EAST

public static final Direction EAST

SOUTHEAST

public static final Direction SOUTHEAST

SOUTH

public static final Direction SOUTH

SOUTHWEST

public static final Direction SOUTHWEST

WEST

public static final Direction WEST

NORTHWEST

public static final Direction NORTHWEST

FULL_CIRCLE

public static final int FULL_CIRCLE
Number of degrees in compass (will not be tested on the Advanced Placement exam).
Constructor Detail

Direction

public Direction()
Constructs a default Direction object facing North.

Direction

public Direction(int degrees)
Constructs a Direction object.
Parameters:
degrees - initial compass direction in degrees

Direction

public Direction(java.lang.String str)
Constructs a Direction object.
Parameters:
str - compass direction specified as a string, e.g. "North"
Throws:
java.lang.IllegalArgumentException - if string doesn't match a known direction name
Method Detail

inDegrees

public int inDegrees()
Returns this direction value in degrees.
Returns:
the value of this Direction object in degrees

equals

public boolean equals(java.lang.Object other)
Indicates whether some other Direction object is "equal to" this one.
Overrides:
equals in class java.lang.Object
Parameters:
other - the other position to test
Returns:
true if other represents the same direction; false otherwise

hashCode

public int hashCode()
Generates a hash code for this direction (will not be tested on the Advanced Placement exam).
Overrides:
hashCode in class java.lang.Object
Returns:
a hash code for a Direction object

toRight

public Direction toRight()
Returns the direction that is a quarter turn to the right of this Direction object.
Returns:
the new direction

toRight

public Direction toRight(int deg)
Returns the direction that is deg degrees to the right of this Direction object.
Parameters:
deg - the number of degrees to turn
Returns:
the new direction

toLeft

public Direction toLeft()
Returns the direction that is a quarter turn to the left of this Direction object.
Returns:
the new direction

toLeft

public Direction toLeft(int deg)
Returns the direction that is deg degrees to the left of this Direction object.
Parameters:
deg - the number of degrees to turn
Returns:
the new direction

reverse

public Direction reverse()
Returns the direction that is the reverse of this Direction object.
Returns:
the reverse direction

toString

public java.lang.String toString()
Represents this direction as a string.
Overrides:
toString in class java.lang.Object
Returns:
a string indicating the direction

roundedDir

public Direction roundedDir(int numDirections,
                            Direction startingDir)
Rounds this direction to the nearest "cardinal" direction (will not be tested on the Advanced Placement exam).
The choice of possible cardinal directions depends on the number of cardinal directions and the starting direction. For example, the two cardinal directions starting at NORTH are NORTH and SOUTH. The two cardinal directions starting at EAST are EAST and WEST. The four cardinal directions starting at NORTH are NORTH, EAST, SOUTH, and WEST. The four cardinal directions starting from NORTHEAST are NORTHEAST, SOUTHEAST, SOUTHWEST, and NORTHWEST. (Precondition: 0 < numDirections <= 360)
Parameters:
numDirections - the number of "cardinal" directions
startingDir - the starting cardinal direction
Returns:
the current direction rounded to a "cardinal" direction

randomDirection

public static Direction randomDirection()
Returns a random direction.
Returns:
a direction

AP® Computer Science Marine Biology Simulation

Copyright© 2002 College Entrance Examination Board