public class Direction
extends java.lang.Object
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).
Modifier and Type | Field and Description |
---|---|
static Direction |
EAST |
static int |
FULL_CIRCLE
Number of degrees in compass.
|
static Direction |
NORTH |
static Direction |
NORTHEAST |
static Direction |
NORTHWEST |
static Direction |
SOUTH |
static Direction |
SOUTHEAST |
static Direction |
SOUTHWEST |
static Direction |
WEST |
Constructor and Description |
---|
Direction()
Constructs a default
Direction object facing North. |
Direction(int degrees)
Constructs a
Direction object. |
Direction(java.lang.String str)
Constructs a
Direction object. |
Modifier and Type | Method and Description |
---|---|
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.
|
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.
|
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.
|
public static final Direction NORTH
public static final Direction NORTHEAST
public static final Direction EAST
public static final Direction SOUTHEAST
public static final Direction SOUTH
public static final Direction SOUTHWEST
public static final Direction WEST
public static final Direction NORTHWEST
public static final int FULL_CIRCLE
public Direction()
Direction
object facing North.public Direction(int degrees)
Direction
object.degrees
- initial compass direction in degreespublic Direction(java.lang.String str)
Direction
object.str
- compass direction specified as a string, e.g. "North"java.lang.IllegalArgumentException
- if string doesn't match a known direction namepublic int inDegrees()
Direction
object in degreespublic boolean equals(java.lang.Object other)
Direction
object
is "equal to" this one.equals
in class java.lang.Object
other
- the other position to testtrue
if other
represents the same direction;
false
otherwisepublic int hashCode()
hashCode
in class java.lang.Object
Direction
objectpublic Direction toRight()
Direction
object.public Direction toRight(int deg)
deg
degrees
to the right of this Direction
object.deg
- the number of degrees to turnpublic Direction toLeft()
Direction
object.public Direction toLeft(int deg)
deg
degrees
to the left of this Direction
object.deg
- the number of degrees to turnpublic Direction reverse()
Direction
object.public java.lang.String toString()
toString
in class java.lang.Object
public Direction roundedDir(int numDirections, Direction startingDir)
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)
numDirections
- the number of "cardinal" directionsstartingDir
- the starting cardinal directionpublic static Direction randomDirection()