Advertisement
Guest User

SHMIFFLEBIGGLEBLUIMP

a guest
Mar 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1.  
  2. //Header comment
  3.  
  4. class Position{
  5. // this is the class that represent one cell position in a 2D grid
  6.  
  7. // row and column
  8. private int row;
  9. private int col;
  10.  
  11. public Position(int row, int col){
  12. // constructor to initialize your attributes
  13. this.col = col;
  14. this.row = row;
  15.  
  16. }
  17.  
  18. // accessors of row and column
  19. public int getRow(){ return this.row;}
  20. public int getCol(){ return this.col;}
  21.  
  22. public String toString(){
  23. // return string representation of a position
  24. // row R and col C must be represented as <R,C> with no spaces
  25. String positionString = "<"+this.row+","+this.col+">";
  26. return positionString;
  27. }
  28.  
  29. @Override
  30. public boolean equals(Object obj){
  31. // check whether two positions are the same
  32. // return true if they are of the same row and the same column
  33. // return false otherwise
  34.  
  35.  
  36. boolean positionCheck = false;
  37. if(obj instanceof Position)
  38. {
  39. Position holdObject = (Position) obj;
  40. if(this.row == holdObject.getRow() && this.col == holdObject.getCol())
  41. positionCheck = true;
  42. }
  43. return positionCheck;
  44. }
  45.  
  46. @Override
  47. public int hashCode(){
  48. // compute an integer hash code for this object
  49. // must follow hash contract and distribute well
  50.  
  51. int theHash = 1;//This will hold the hashcode and will be returned at the end
  52. theHash = 31 * theHash + ((Integer)this.row).hashCode();
  53. theHash = 31 * theHash + ((Integer)this.col).hashCode();
  54. return theHash;
  55. }
  56.  
  57.  
  58.  
  59. //----------------------------------------------------
  60. // example testing code... make sure you pass all ...
  61. // and edit this as much as you want!
  62.  
  63.  
  64. public static void main(String[] args){
  65. Position p1 = new Position(3,5);
  66. Position p2 = new Position(3,6);
  67. Position p3 = new Position(2,6);
  68.  
  69. if (p1.getRow()==3 && p1.getCol()==5 && p1.toString().equals("<3,5>")){
  70. System.out.println("Yay 1");
  71. }
  72.  
  73. if (!p1.equals(p2) && !p2.equals(p3) && p1.equals(new Position(3,5))){
  74. System.out.println("Yay 2");
  75. }
  76.  
  77. if (p1.hashCode()!=p3.hashCode() && p1.hashCode()!=(new Position(5,3)).hashCode() &&
  78. p1.hashCode() == (new Position(3,5)).hashCode()){
  79. System.out.println("Yay 3");
  80. }
  81.  
  82.  
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement