Using java language
Assume you have a graphical user interface with a bunch of Rectangles that you added
randomly all over the screen, and saved in an ArrayList<Rectangle> called
rectangles. The built-in Rectangle shape class is not implemented to be Comparable,
but you want to be able to sort the rectangles in order of how they appear in the interface from
left to right. If two Rectangles align on the left, the Rectangle up higher in the interface should
come first. Note: the built-in Rectangle class has methods getX(), getY(),
getWidth(), and getHeight(), all of which return a double. Write your own comparator
so that you can use the following line to sort your Rectangles:
Collections.sort(rectangles, new MyRectangleComparator());
----------------------------------------------------------------
import java.util.Comparator;
public class MyRectangleComparator
import java.util.Comparator;
public class MyRectangleComparator implements Comparator<Rectangle> {
@Override
public int compare(Rectangle rectangle1, Rectangle rectangle2) {
if (rectangle1.getX() == rectangle2.getX()) {
return Double.compare(rectangle1.getY(), rectangle2.getY());
}
return Double.compare(rectangle1.getX(), rectangle2.getX());
}
}
Using java language Assume you have a graphical user interface with a bunch of Rectangles that...