Question: Hello, could someone help me with this assginmnet please? We are using Eclipse COSC 1174 Java II Lab Chapter 13 Abstract Classes Define a class
Hello, could someone help me with this assginmnet please? We are using Eclipse
COSC 1174 Java II Lab
Chapter 13 Abstract Classes
Define a class named ComparableCircle that extends Circle and implements Comparable. Implement the compareTo method to compare the circles on the basis of area. A sample test class to find the larger of the two instances of ComparableCircle objects is included below. Note this test class uses a class named Max which has a static method (max) for identifying the largest of two circles. The listing for Max is also included below.
public class TestComparableCircle {
// Main method
public static void main(String[] args) {
// Create two comparable circles
ComparableCircle circle1 = new ComparableCircle(5);
ComparableCircle circle2 = new ComparableCircle(15);
// Display the max circle
ComparableCircle circle3 = (ComparableCircle)Max.max(circle1, circle2);
System.out.println("The max circle's radius is " + circle3.getRadius());
System.out.println(circle3);
}
}
//Max.java: Find a maximum object
class Max {
/** Return the maximum of two objects */
public static ComparableCircle max
(ComparableCircle o1, ComparableCircle o2) {
if (o1.compareTo(o2) > 0)
return o1;
else
return o2;
}
}
Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. The Rectangle objects are equal if their areas are the same. The code for a sample test class is included below.
public class TestRectangle {
public static void main(String[] args) {
Rectangle obj1 = new Rectangle();
Rectangle obj2 = new Rectangle();
System.out.println(obj1.equals(obj2));
System.out.println(obj1.compareTo(obj2));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
