// AP(r) Computer Science Marine Biology Simulation: // The Location class is copyright(c) 2002 College Entrance // Examination Board (www.collegeboard.com). // // This class is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation. // // This class is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. package edu.kzoo.grid; /** * AP® Computer Science Marine Biology Simulation:
* A Location object represents the row and column of a * location in a two-dimensional grid. * *

* The Location class is * copyright© 2002 College Entrance Examination Board * (www.collegeboard.com). * * @author Alyce Brady * @author Chris Nevison * @author APCS Development Committee * @version 1 July 2002 **/ public class Location implements Comparable { // Instance Variables: Encapsulated data for each Location object private int myRow; // row location in grid private int myCol; // column location in grid /** Constructs a Location object. * @param row location's row * @param col location's column **/ public Location(int row, int col) { myRow = row; myCol = col; } // accessor methods /** Returns the row coordinate of this location. * @return row of this location **/ public int row() { return myRow; } /** Returns the column coordinate of this location. * @return column of this location **/ public int col() { return myCol; } /** Indicates whether some other Location object is * "equal to" this one. * @param other the other location to test * @return true if other is at the * same row and column as the current location; * false otherwise **/ public boolean equals(Object other) { if ( ! (other instanceof Location) ) return false; Location otherLoc = (Location) other; return row() == otherLoc.row() && col() == otherLoc.col(); } /** Generates a hash code for this location * (will not be tested on the Advanced Placement exam). * @return a hash code for a Location object **/ public int hashCode() { return row() * 3737 + col(); } /** 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 other. * Locations are ordered in row-major order. * (Precondition: other is a Location object.) * @param other the other location to test * @return a negative integer if this location is less than * other, zero if the two locations are equal, * or a positive integer if this location is greater than * other **/ public int compareTo(Object other) { Location otherLoc = (Location) other; if ( equals(other) ) return 0; if ( row() == otherLoc.row() ) return col() - otherLoc.col(); return row() - otherLoc.row(); } /** Represents this location as a string. * @return a string indicating the row and column of the * location in (row, col) format **/ public String toString() { return "(" + row() + ", " + col() + ")"; } }