Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Compare areas of different geometric shapes (filename: CompareShapeArea

ID: 3738580 • Letter: P

Question

Problem: Compare areas of different geometric shapes (filename: CompareShapeAreas.java) Define a class named ComparableCircle that extends the Circle class and implements the Comparable interface as shown below. The Circle class itself extends the GeometricObject class class ComparableCircle extends Circle implements Comparable Implement the CompareTo method defined inside the Comparable interface to compare the areas between a Circle object and a GeometricObject object. It returns 1 if the circle has a larger area, else if the circle has a smaller area, it returns -1, else it returns 0 For implementing the Geometric class and the Circle class, you may use the code from the book or the slides Similarly, based on the code from the book or the slides, define a class named ComparableRectangle that extends the Rectangle class and implements the Comparable interface. The Rectangle class itself extends the GeometricObject class. Implement the CompareTo method defined inside the Comparable interface to compare the areas between a Rectangle object and a GeometricObject object. It returns 1 if the rectangle has a larger area, else if the rectangle has a smaller area, it returns -1, else it returns 0 Write a test class to find the larger of two instances of ComparableCircle objects by invoking the CompareTo method, and the larger between an instance of a ComparableCircle and an instance of ComparableRectangle also by invoking the CompareTo method.

Explanation / Answer

GeometricObejct.java

public abstract class GeometricObejct {

//Declaring instance variables

private String color;

//Zero argumented constructor

public GeometricObejct() {

}

//Parameterized constructor

public GeometricObejct(String color) {

this.color = color;

}

//getters and setters

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

return " Color=" + color;

}

public abstract double getArea();

}

_________________

Circle.java

public class Circle extends GeometricObejct {

//Declaring instance variables

private double radius;

//Parameterized constructor

public Circle(double radius) {

super();

this.radius = radius;

}

// getters and setters

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

//This method will calculate the area of the Circle

public double getArea() {

return Math.PI * radius * radius;

}

@Override

public String toString() {

return "Cirlce [radius=" + radius + "]";

}

}

____________________

Rectangle.java

public class Rectangle extends GeometricObejct {

//Declaring instance variables

private double length;

private double width;

//Parameterized constructor

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

}

// getters and setters

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

//This method calculate the area of the Rectangle

public double getArea()

{

return length*width;

}

@Override

public String toString() {

return "Rectangle [length=" + length + ", width=" + width + "]";

}

}

________________________

ComparableCircle.java

public class ComparableCircle extends Circle implements

Comparable<GeometricObejct> {

public ComparableCircle(double radius) {

super(radius);

}

@Override

public int compareTo(GeometricObejct o) {

int val = 0;

double area = o.getArea();

if (getArea() > area)

val = 1;

else if (getArea() < area)

val = -1;

else if (getArea() == area)

val = 0;

return val;

}

}

_______________________

ComparableRectangle.java

public class ComparableRectangle extends Rectangle implements

Comparable<GeometricObejct> {

public ComparableRectangle(double length, double width) {

super(length, width);

}

@Override

public int compareTo(GeometricObejct o) {

int val = 0;

double area = o.getArea();

if (getArea() > area)

val = 1;

else if (getArea() < area)

val = -1;

else if (getArea() == area)

val = 0;

return val;

}

}

____________________

CompareShapeAreas.java

public class CompareShapeAreas {

public static void main(String[] args) {

ComparableCircle cc1=new ComparableCircle(5.5);

ComparableCircle cc2=new ComparableCircle(4.5);

ComparableRectangle cr=new ComparableRectangle(4,5);

if(cc1.compareTo(cc2)==1)

{

System.out.println("Circle#1 area is greater than Circle#2 Area");

}

else if(cc1.compareTo(cc2)==-1)

{

System.out.println("Circle#2 area is greater than Circle#1 Area");

}

else

{

System.out.println("Circle#1 and Circle#2 are having same area");

}

if(cc1.compareTo(cr)==1)

{

System.out.println("Circle#1 area is greater than Rectangle Area");

}

else if(cc1.compareTo(cr)==-1)

{

System.out.println("Rectangle area is greater than Circle#1 Area");

}

else

{

System.out.println("Circle#1 and Rectangle are having same area");

}

}

}

____________________

Output:

Circle#1 area is greater than Circle#2 Area
Circle#1 area is greater than Rectangle Area

________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote