Using Java ---------------------------------------------------------------------
ID: 3858987 • Letter: U
Question
Using Java
----------------------------------------------------------------------------------
III Problem 2: *13.5 (Enable GeometricObject comparable) Modify the GeometricObject class to implement the Comparable interface, and define a static max method in the GeometricObject class for finding the larger of two GeometricObject objects. Implement the new GeometricObject class. Write a test program that uses the max method to find the larger of two circles and the larger of two rectangles. Instructions: Given the following "main" method, complete the "exam4p2.java" so that the following output will be produced: 1. The "main" program: public class exam4p2 // Main method public static void main(Stringl args) // Create two comparable circles Circle1 circle1 new Circle1(5); Circle1 circle2- new Circle1(4); // Display the max circle Circle1 circle (Circle1) GeometricObject1.max(circle1,circle2); System.out.println("The max circle's radius is " +circle.getRadius() System.out.println(circle); Circle1) Geometricobject1.max(circle1, circle2); 2. The output of the complete program: The max circle's radius is 5.0 [Circle] radius = 5.0Explanation / Answer
package sample1;
import java.util.Scanner;
public class test
{
public static void main(String[] args) {
// Create two comparable circles
Circle1 circle1 = new Circle1(5.0);
Circle1 circle2 = new Circle1(4.0);
// Display the max circle
Circle1 circle = (Circle1) GeometricObject1.max(circle1, circle2);
System.out.println("The max circle's radius is " + circle.getRadius());
System.out.println(circle);
}
}
package sample1;
class Circle1 extends GeometricObject1 {
protected double radius;
// Default constructor
public Circle1() {
this(1.0, "white", 1.0);
}
// Construct circle with specified radius
public Circle1(double radius) {
this.radius = radius;
}
// Construct a circle with specified radius, weight, and color
public Circle1(double radius, String color, double weight) {
//You complete this part
}
// Getter method for radius
public double getRadius() {
return radius;
}
// Setter method for radius
public void setRadius(double radius) {
this.radius = radius;
}
// Implement the findArea method defined in GeometricObject
public double getArea() {
return radius * radius * Math.PI;
}
// Implement the findPerimeter method defined in GeometricObject
public double getPerimeter() {
return 2 * radius * Math.PI;
}
// Override the equals() method defined in the Object class
public boolean equals(Circle1 circle) {
return this.radius == circle.getRadius();
}
@Override
public String toString() {
return "[Circle] radius = " + radius;
}
@Override
public int compareTo(GeometricObject1 o) {
if (this.getRadius() > ((Circle1) o).getRadius()){
return 1;
}else if (this.getRadius() == ((Circle1) o).getRadius()){
return 0;
}
else
return -1;
}
}
package sample1;
abstract class GeometricObject1 //You complete this part
{
protected String color;
protected double weight;
// Default construct
protected GeometricObject1() {
color = "white";
weight = 1.0;
}
// Construct a geometric object
protected GeometricObject1(String color, double weight) {
//You complete this part
}
// Getter method for color
public String getColor() {
return color;
}
public void setColor(String color) {
//You complete this part
}
// Getter method for weight
public double getWeight() {
return weight;
}
// Setter method for weight
public void setWeight(double weight) {
//You complete this part
}
// Abstract method
public abstract double getArea();
// Abstract method
public abstract double getPerimeter();
public static GeometricObject1 max(GeometricObject1 o1, GeometricObject1 o2) {
int res = o1.compareTo(o2);
System.out.println(" res :"+res);
if(res==1){
System.out.println(" abc1:"+o1.getPerimeter());
return o1;
}
else{
System.out.println(" abc:"+o2.getPerimeter());
return o2;
}
}
public int compareTo(GeometricObject1 o) {
// TODO Auto-generated method stub
return ((Circle1)this).compareTo(o);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.