View the step-by-step solution to:

Question

I am trying to define a class named ComparableCircle that extends Circle and implements Comparable. The

Circle class extends the abstract class Geometric Object, both of which are defined below:


//---------------GEOMETRIC OBJECT-----------------

abstract class GeometricObject {

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;


/** Construct a default geometric object */

protected GeometricObject() {

dateCreated = new java.util.Date();

}


/** Construct a geometric object with color and filled value */

protected GeometricObject(String color, boolean filled) {

dateCreated = new java.util.Date();

this.color = color;

this.filled = filled;

}


/** Return color */

public String getColor() {

return color;

}


/** Set a new color */

public void setColor(String color) {

this.color = color;

}


/** Return filled. Since filled is boolean,

* the get method is named isFilled */

public boolean isFilled() {

return filled;

}


/** Set a new filled */

public void setFilled(boolean filled) {

this.filled = filled;

}


/** Get dateCreated */

public java.util.Date getDateCreated() {

return dateCreated;

}


@Override

public String toString() {

return "created on " + dateCreated + "ncolor: " + color +

" and filled: " + filled;

}


/** Abstract method getArea */

public abstract double getArea();


/** Abstract method getPerimeter */

public abstract double getPerimeter();

}



//-----------------------CIRCLE-----------------------

class Circle extends GeometricObject {

private double radius;


public Circle() {

}


public Circle(double radius) {

this.radius = radius;

}


/** Return radius */

public double getRadius() {

return radius;

}


/** Set a new radius */

public void setRadius(double radius) {

this.radius = radius;

}


@Override /** Return area */

public double getArea() {

return radius * radius * Math.PI;

}


/** Return diameter */

public double getDiameter() {

return 2 * radius;

}


@Override /** Return perimeter */

public double getPerimeter() {

return 2 * radius * Math.PI;

}


/* Print the circle info */

public void printCircle() {

System.out.println("The circle is created " + getDateCreated() +

" and the radius is " + radius);

}

}


Implement the compareTo method in the ComparableCircle class, comparing the circles on the basis of area. If the original circle has a larger area, the compareTo function should return 1. If the original circle has a smaller area, the function should return -1, and if the two circles have the same area, the function should return 0.


Test your ComparableCircle class in a Driver class that asks the user to input the radii of two circles. Your program should then print the result of the first circle's compareTo method (using the second circle as an argument). 


SAMPLE RUN #1: java Driver

Enter radius of first circle:33.5↵

Enter·radius of second circle:17.2↵

1↵

SAMPLE RUN #2: java Driver

Enter radius of first circle:10.9↵

Enter radius of second circle:22.34↵

-1↵

SAMPLE RUN #3: java Driver

Enter radius of first circle:5.5·↵

Enter radius of second circle:5.5↵

0↵


This is what I have so far:


public class ComparableCircle{


  public static void main(String[] args) {

    ComparableCircle c1 = new ComparableCircle(0,0,5);

    ComparableCircle c2 = new ComparableCircle(0,0,10);

    ComparableCircle c3 = (ComparableCircle) GeometricObject.max(c1, c2);

    System.out.println(c1);

    System.out.println(c2);


    System.out.println("Max circle = " + c3.getRadius());

    System.out.println(c3);

  }

}



class ComparableCircle extends Circle { // GeometricObject Implements compareTo


  ComparableCircle() {

  }


  ComparableCircle(double x, double y, double radius) {

    super(x, y, radius);

  }


}



abstract class GeometricObject {

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;


/** Construct a default geometric object */

protected GeometricObject() {

dateCreated = new java.util.Date();

}


/** Construct a geometric object with color and filled value */

protected GeometricObject(String color, boolean filled) {

dateCreated = new java.util.Date();

this.color = color;

this.filled = filled;

}


/** Return color */

public String getColor() {

return color;

}


/** Set a new color */

public void setColor(String color) {

this.color = color;

}


/** Return filled. Since filled is boolean,

* the get method is named isFilled */

public boolean isFilled() {

return filled;

}


/** Set a new filled */

public void setFilled(boolean filled) {

this.filled = filled;

}


/** Get dateCreated */

public java.util.Date getDateCreated() {

return dateCreated;

}


@Override

public String toString() {

return "created on " + dateCreated + "ncolor: " + color +

" and filled: " + filled;

}


/** Abstract method getArea */

public abstract double getArea();


/** Abstract method getPerimeter */

public abstract double getPerimeter();

}


class Circle extends GeometricObject {

private double radius;


public Circle() {

}


public Circle(double radius) {

this.radius = radius;

}


/** Return radius */

public double getRadius() {

return radius;

}


/** Set a new radius */

public void setRadius(double radius) {

this.radius = radius;

}


@Override /** Return area */

public double getArea() {

return radius * radius * Math.PI;

}


/** Return diameter */

public double getDiameter() {

return 2 * radius;

}


@Override /** Return perimeter */

public double getPerimeter() {

return 2 * radius * Math.PI;

}


/* Print the circle info */

public void printCircle() {

System.out.println("The circle is created " + getDateCreated() +

" and the radius is " + radius);

}

}



I can not get my ComparableCircle class to work. Please help!

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 Java Programming tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors