Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. public class JavaApplication {
  2.     static class Point{
  3.         private int x;
  4.         private int y;
  5.        
  6.         Point(int x, int y){
  7.             this.x = x;
  8.             this.y = y;
  9.         }
  10.        
  11.         public void setX(int x){ this.x = x;}
  12.         public void setY(int y){ this.y = y;}
  13.         public int getX(){ return this.x;}
  14.         public int getY(){ return this.y;}
  15.     }
  16.  
  17.     static class Shape{
  18.         Point point;
  19.        
  20.         Shape(Point point){
  21.              this.point = point;
  22.         }
  23.        
  24.         public void move(int x, int y){
  25.             point.setX(x);
  26.             point.setY(y);
  27.         }
  28.        
  29.         @Override
  30.         public int hashCode() {
  31.             int hash = 57;
  32.             hash = hash * 17 + point.x;
  33.             hash = hash * 17 + point.y;
  34.             return hash;
  35.         }
  36.        
  37.         @Override
  38.         public boolean equals(Object obj) {
  39.             Shape c = (Shape) obj;
  40.             return (this.point.getX() == c.point.getX() && this.point.getY() == c.point.getY());
  41.         }
  42.     }
  43.     public static void main(String[] args) {
  44.         Set hs = new HashSet();
  45.         Shape c1 = new Shape(new Point(1,1));
  46.         hs.add(c1);
  47.         c1.move(2, 2);
  48.         System.out.println(hs.contains(c1));
  49.         Shape c2 = new Shape(new Point(3,3));
  50.         System.out.println(hs.contains(c2));
  51.         hs.add(c2);
  52.         Shape c3 = new Shape(new Point(3,3));
  53.         System.out.println(hs.contains(c3));
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement