ALSO IN ADDITION For Circle add an equals method to the curr

***ALSO IN ADDITION*** For Circle, add an equals method to the current code and implement the Comparable interface.

Provide test code to test the compareTo and equals methods please.

..

I have the GeometricObject and Circle classes included here.

..

GeometricObject class:

public 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();
}

/** 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,
so, the get method name is 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;
}

/** Return a string representation of this object */
public String toString() {
return \"created on \" + dateCreated + \"\ color: \" + color +
\" and filled: \" + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

..

Circle class:

public 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;
}

/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}

/** Return diameter */
public double getDiameter() {
return 2 * radius;
}

/** 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 3.9 (Enable Circle comparable) Rewrite the Circle class in Listing 13.2 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Circle objects are equal if their radii are the same. Draw the UML diagram that involves Circle, Geometricobject, armeil Comparable.

Solution

Hi, Please find my code.

I have implemented Comparable interface.

Please let me know in case of any issue:

############# Circle.java ############

public class Circle extends GeometricObject implements Comparable<Circle> {

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;

   }

/** Return area */

   public double getArea() {

       return radius * radius * Math.PI;

   }

/** Return diameter */

   public double getDiameter() {

       return 2 * radius;

   }

/** 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);

   }

  

// overriding compareTo Method

@Override

   public int compareTo(Circle o) {

       if(radius == o.radius)

           return 0;

       else if(radius > o.radius)

           return 1;

       else

           return -1;

   }

  

// overriding equal method

@Override

   public boolean equals(Object obj) {

      

       if(obj instanceof Circle){

           // type casting obj in Circle

           Circle other = (Circle)obj;

           if(radius == other.radius)

               return true;

       }

      

       return false;

   }

}

############# CircleTest.java #################

public class CircleTest {

  

   public static void main(String[] args) {

      

       // Creating Two Circle Objects

      

       Circle c1 = new Circle(9.5);

      

       Circle c2 = new Circle();

       c2.setRadius(12.3);

      

       System.out.println(c1.toString());

       System.out.println(c2.toString());

      

       System.out.println();

      

       // calling equals method

       System.out.println(\"is C1 == C2 ? \"+ c1.equals(c2));

      

       int com = c1.compareTo(c2);

       System.out.print(\"\ Compare: \");

       if(com == 0){

           System.out.println(\"C1 == C2\");

       }

       else if(com > 1){

           System.out.println(\"C1 > C2\");

       }

       else{

           System.out.println(\"C1 < C2\");

       }

      

   }

}

/*

Sample Run:

created on Thu Oct 20 06:03:26 IST 2016

color: white and filled: false

created on Thu Oct 20 06:03:26 IST 2016

color: white and filled: false

is C1 == C2 ? false

Compare: C1 < C2

*/

***ALSO IN ADDITION*** For Circle, add an equals method to the current code and implement the Comparable interface. Provide test code to test the compareTo and
***ALSO IN ADDITION*** For Circle, add an equals method to the current code and implement the Comparable interface. Provide test code to test the compareTo and
***ALSO IN ADDITION*** For Circle, add an equals method to the current code and implement the Comparable interface. Provide test code to test the compareTo and
***ALSO IN ADDITION*** For Circle, add an equals method to the current code and implement the Comparable interface. Provide test code to test the compareTo and
***ALSO IN ADDITION*** For Circle, add an equals method to the current code and implement the Comparable interface. Provide test code to test the compareTo and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site