Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions