Question: Using JAVA Write a class called Rectangle that implements the Comparable interface. The class should contain three fields: height (int), width (int), topCorner (Point). Write

Using JAVA

Write a class called Rectangle that implements the Comparable interface. The class should contain three fields: height (int), width (int), topCorner (Point). Write the necessary constructors, accessors, mutators and other methods. The Point class is given below.

Write a client class that creates an ArrayList of 10 Rectangle objects called list1.

Print the list.

Call Collections.sort(list1), to sort the elements according to the height. If the heights are the equal then sort the list using the width of the rectangle and if the widths are also equal then sort them using the topCorner point (hint: call the Points compareTo method).

Print the sorted list.

public class Point implements Comparable {

private int x;

private int y;

public Point() {

this(0, 0);

}

public Point(int x, int y) {

this.x=x;

this.y=y;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

public String toString() {

return "(" + x + ", " + y + ")";

}

public int compareTo(Point pt){

if(x==pt.x)

return y-pt.y;

else

return x-pt.x;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!