igede

Untitled

Nov 22nd, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1.  /**  
  2.   * Represent a location in a rectangular grid.  
  3.   *  
  4.   * @author Gede
  5.   * @version (22/11/2018)
  6.   */  
  7.  public class Location  
  8.  {  
  9.    // Row and column positions.  
  10.    private int row;  
  11.    private int col;  
  12.    /**  
  13.     * Represent a row and column.  
  14.     * @param row The row.  
  15.     * @param col The column.  
  16.     */  
  17.    public Location(int row, int col)  
  18.    {  
  19.      this.row = row;  
  20.      this.col = col;  
  21.    }  
  22.    /**  
  23.     * Implement content equality.  
  24.     */  
  25.    public boolean equals(Object obj)  
  26.    {  
  27.      if(obj instanceof Location) {  
  28.        Location other = (Location) obj;  
  29.        return row == other.getRow() && col == other.getCol();  
  30.      }  
  31.      else {  
  32.        return false;  
  33.      }  
  34.    }  
  35.    /**  
  36.     * Return a string of the form row,column  
  37.     * @return A string representation of the location.  
  38.     */  
  39.    public String toString()  
  40.    {  
  41.      return row + "," + col;  
  42.    }  
  43.    /**  
  44.     * Use the top 16 bits for the row value and the bottom for  
  45.     * the column. Except for very big grids, this should give a  
  46.     * unique hash code for each (row, col) pair.  
  47.     * @return A hashcode for the location.  
  48.     */  
  49.    public int hashCode()  
  50.    {  
  51.      return (row << 16) + col;  
  52.    }  
  53.    /**  
  54.     * @return The row.  
  55.     */  
  56.    public int getRow()  
  57.    {  
  58.      return row;  
  59.    }  
  60.    /**  
  61.     * @return The column.  
  62.     */  
  63.    public int getCol()  
  64.    {  
  65.      return col;  
  66.    }  
  67.  }
Advertisement
Add Comment
Please, Sign In to add comment