Sample Class Contracts

Wellesley College -- CS111 Computer Programming

Contract for the Buggle Class

Constructor Method

Buggle ()
Returns a new buggle at position (1,1) whose heading is Direction.EAST, whose color is Color.red, and whose brush is down.

Instance Methods

public void forward ()
Moves this buggle forward one step (in the direction of its current heading). Complains if the buggle is facing a wall.

public void forward (int n)
Moves this buggle forward n steps. If the buggle encounters a wall along the way, it will stop and complain.

public void backward ()
Moves this buggle backward one step (in the direction opposite to its current heading). Complains if the buggle is facing a wall.

public void backward (int n)
Moves this backward n steps. If the buggle encounters a wall along the way, it will stop and complain.

public void left ()
Turns this buggle left by 90 degrees.

public void right ()
Turns this buggle right by 90 degrees.

public void brushDown ()
Lowers this buggle's brush. When the brush is lowered, the buggle leaves a trail when it moves.

public void brushUp ()
Raises this buggle's brush. When the brush is raised, the buggle leaves no trail when it moves.

public void dropBagel ()
Drops a bagel in the cell currently occupied by this buggle. If there is already a bagel in the cell, tbe buggle complains.

public void pickUpBagel ()
Picks up the bagel in the cell currently occupied by this buggle. If there is no bagel in the cell, the buggle complains.

public Point getPosition ()
Returns a point that indicates the current position of this buggle in the grid.

public void setPosition (Point p)
Changes the position of this buggle to be the point p. Complains if p is not in the grid.

public Direction getHeading ()
Returns the heading of this buggle.

public void setHeading (Direction d)
Changes the heading of this buggle to be the direction d.

public Color getColor ()
Returns the color of this buggle.

public void setColor (Color c)
Changes the color of this buggle to be the color c.

public String toString ()
Returns a string representation of this buggle.

Contract for the BuggleWorld Class

Instance Method

public void run ()
Execute specified buggle actions in this buggle world.

Contract for the Point Class

Constructor Method

public Point (int x, int y)
Returns a point with x-coordinate x and y-coordinate y.

Instance Methods

public boolean equals (Point p)
Returns true if this point has the same coordinates as p, and false otherwise.

public String toString ()
Returns a string representation of this point.

Contract for the Color Class

Constants

The following constant colors are available
public final static Color black
public final static Color blue
public final static Color cyan
public final static Color darkGray
public final static Color gray
public final static Color green
public final static Color lightGray
public final static Color magenta
public final static Color orange
public final static Color pink
public final static Color red
public final static Color white
public final static Color yellow

Constructor Methods

public Color(int r, int g, int b)
Creates a color with the specified red, green, and blue components. The three arguments must each be in the range 0-255.

public Color(float r, float g, float b)
Creates a color with the specified red, green, and blue values, where each of the values is in the range 0.0-1.0. The value 0.0 indicates no contribution from the primary color component. The value 1.0 indicates the maximum intensity of the primary color component.

Instance Methods

public Color brighter()
Returns a brighter version of this color.

public Color darker()
Returns a darker version of this color.

public boolean equals(Color c)
Returns true if this color is equal to c and false otherwise.

public int getRed()
Returns the red component of this color as an integer between 0 and 255.

public int getGreen()
Returns the green component of this color as an integer between 0 and 255.

public int getBlue()
Returns the blue component of this color as an integer between 0 and 255.

public String toString()
Returns a string representation of this color.

Contract for the Direction Class

Constants

The following constant directions specify the four compass points:
public static final Direction NORTH
public static final Direction EAST
public static final Direction SOUTH
public static final Direction WEST

Instance Methods

public Direction left ()
Returns the compass point to the left of this direction.

public Direction right ()
Returns the compass point to the right of this direction.

public Direction opposite ()
Returns the compass point opposite to this direction.

public boolean equals (Direction d)
Returns true if this direction equals d, and false otherwise.

public String toString ()
Returns a string representation of this direction.