Can someone please help me with this JAVA problem about Abstract classes and Int
ID: 3769831 • Letter: C
Question
Can someone please help me with this JAVA problem about Abstract classes and Interfaces? They are asking me to explain how the following class SortRectangle works. What is the output of the problem?
The program is:
public class SortRectangles {
public static void main(String[] args) {
new ComparableRectangle(3.4, 5.4);
new ComparableRectangle(13.24, 55.4);
new ComparableRectangle(7.4, 35.4);
new ComparableRectangle(1.4, 25.4);
java.util.Arrays.sort(rectangles);
for (Rectangle rectangle: rectangles){
System.out.print(rectangle + " ");
System.out.println();
}
}
}
=============================================================================
public class ComparableRectangle extends Rectangle
implements Comparable<ComparableRectangle>{
public ComparableRectangle(double width, double height){
super(width, height);
}
@Override // Implement the compareTo method defined in Comparable
public int compareTo(ComparableRectangle o){
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}
@Override //Implement the toString method in GeometricObject
public String toString(){
return "Width: " + getWidth() + "Height: " + getHeight() + "Area: " + getArea();
}
}
public interface comparable
public int compareTo(object o){
}
}
I'm really lost here, when I write the program and try to execute it, it has many errors. I would really appreciate the help! Thanks!
Explanation / Answer
package current.sortect;
public class SortRectangles {
public static void main(String[] args) {
ComparableRectangle[] rectangles = new ComparableRectangle[4];
rectangles[0] = new ComparableRectangle(3.4, 5.4);
rectangles[1] = new ComparableRectangle(13.24, 55.4);
rectangles[2] = new ComparableRectangle(7.4, 35.4);
rectangles[3] =new ComparableRectangle(1.4, 25.4);
java.util.Arrays.sort(rectangles);
for (Rectangle rectangle: rectangles){
System.out.print(rectangle + " ");
System.out.println();
}
}
}
//=============================================================================
class Rectangle {
private double width;
private double height;
public Rectangle(double width,double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
class ComparableRectangle extends Rectangle
implements Comparable<ComparableRectangle>{
public ComparableRectangle(double width, double height){
super(width, height);
}
@Override // Implement the compareTo method defined in Comparable
public int compareTo(ComparableRectangle o){
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}
@Override //Implement the toString method in GeometricObject
public String toString(){
return "Width: " + getWidth() + "Height: " + getHeight() + "Area: " + getArea();
}
}
interface comparable {
public int compareTo(Object o);
}
--------------output---------------
Width: 3.4Height: 5.4Area: 18.36
Width: 1.4Height: 25.4Area: 35.559999999999995
Width: 7.4Height: 35.4Area: 261.96
Width: 13.24Height: 55.4Area: 733.496