How to print the array of points with integers?

advertisements
//imports and class block trimmed
public static void main(String[] args) {

    Point[] xy = new Point[100];

    for (int i = 0; i < xy.length; i++) {

        Point c = new Point((int) (Math.random() * 100),
                (int) (Math.random() * 100));

        xy[i] = c;
    }

    Arrays.sort(xy);

    for (int j = 0; j < xy.length; j++) {
        System.out.println(xy[j]);
    }

}

I get an output of 100 statements like "[email protected]". I need it to display integers. I have two other classes titled Point and CompareY. How do I print the array items in xy as integers?


Override the toString() method in your Point class:

class Point {
    // your code ...
    @Override
    public String toString() {
        return "(" + this.x + ", " + this.y + ")";
    }
}