CS111

Location Contract

Instances of the Location class represent points with integer coordinates (x, y) on the Cartesian plane. Location instances are immutable; their coordinates can never change. This distinguishes them from instances of the java.awt.Point class, which are mutable.

Constructor Methods

Location (int x, int y)
Returns a location with coordinates (x, y).

Instance Variables

public final int x
The x-coordinate of this location. The final keyword indicates it cannot be changed by assignment.

public final int y
The y-coordinate of this location. The final keyword indicates it cannot be changed by assignment.

Instance Methods

public Location add (Location loc)
Returns a new location that is the componentwise sum of this location with loc. That is, the x coordinate of the resulting location is the sum of this.x and loc.x, and similarly for the y coordinate.

public boolean equals (Object obj)
Returns true if obj is a Location instance with the same x and y coordinates as this point, and false otherwise.

public Point toPoint ()
Returns a Point instance with the same coordinates as this location.

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

Class Methods

public static Location fromPoint (Point p)
Returns a Location instance with the same coordinates as p.

public static Point toPoint (Location loc)
Returns a Point instance with the same coordinates as loc..