Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JavaApplication {
- static class Point{
- private int x;
- private int y;
- Point(int x, int y){
- this.x = x;
- this.y = y;
- }
- public void setX(int x){ this.x = x;}
- public void setY(int y){ this.y = y;}
- public int getX(){ return this.x;}
- public int getY(){ return this.y;}
- }
- static class Shape{
- Point point;
- Shape(Point point){
- this.point = point;
- }
- public void move(int x, int y){
- point.setX(x);
- point.setY(y);
- }
- @Override
- public int hashCode() {
- int hash = 57;
- hash = hash * 17 + point.x;
- hash = hash * 17 + point.y;
- return hash;
- }
- @Override
- public boolean equals(Object obj) {
- Shape c = (Shape) obj;
- return (this.point.getX() == c.point.getX() && this.point.getY() == c.point.getY());
- }
- }
- public static void main(String[] args) {
- Set hs = new HashSet();
- Shape c1 = new Shape(new Point(1,1));
- hs.add(c1);
- c1.move(2, 2);
- System.out.println(hs.contains(c1));
- Shape c2 = new Shape(new Point(3,3));
- System.out.println(hs.contains(c2));
- hs.add(c2);
- Shape c3 = new Shape(new Point(3,3));
- System.out.println(hs.contains(c3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement