[code="java"]//TPoint.java
/*
This is just a trivial "struct" type class --
it simply holds an int x/y point for use by Tetris,
and supports equals() and toString().
We'll allow public access to x/y, so this
is not an object really.
*/
public class TPoint {
public int x;
public int y;
// Creates a TPoint based in int x,y
public TPoint(int x, int y) {
// questionable style but convenient --
// params with same name as ivars
this.x = x;
this.y = y;
}
// Creates a TPoint, copied from an existing TPoint
public TPoint(TPoint point) {
this.x = point.x;
this.y = point.y;
}
// Standard equals() override
public boolean equals(Object other) {
// standard two checks for equals()
if (this == other) return true;
if (!(other instanceof TPoint)) return false;
// check if other point same as us
TPoint pt = (TPoint)other;
return(x==pt.x && y==pt.y);
}
// Standard toString() override, produce
// human-readable String from object
public String toString() {
return "(" + x + "," + y + ")";
}
}
[/code]
[code="java"]
import java.util.HashSet;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
TPoint a =new TPoint(0,1);
TPoint b =new TPoint(0,1);
HashSet<TPoint> setA = new HashSet<TPoint>();
HashSet<TPoint> setB = new HashSet<TPoint>();
setA.add(a);
setB.add(b);
boolean res = setA.equals(setB);
System.out.println(res);
}
}
[/code]