Question

In: Computer Science

(Java 8th Person edition) Exercise 13.9, p. 536 (Enable Circle comparable). Override the following methods: compareTo...

(Java 8th Person edition) Exercise 13.9, p. 536 (Enable Circle comparable). Override the following methods: compareTo (interface Comparable), toString (class GeometricObject) and equals (class Object).Create an array of comparable circles, sort them and display the result of a sorting procedure. Then create one more circle, which is equal to one of the existing circles and demonstrate the work of the method equals().

I added the code that the question references, but I'm not even quite too sure what it's asking me

public class GeometricObject implements Comparable<GeometricObject>
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
  
public GeometricObject(){
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled){
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
  
@Override
public String toString(){
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
@Override
public int compareTo(GeometricObject Circle){
  
}
}

public class Circle extends GeometricObject//11.2 listing code
{
public double radius;
  
public Circle(){
}
public Circle(double radius){
this.radius = radius;
}
public Circle(double radius, String color, boolean filled){
this.radius = radius;
setColor(color);
setFilled(filled);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius){
this.radius=radius;
}
public double getArea(){
return radius*radius*Math.PI;
}
public double getDiameter(){
return 2*radius;
}
public double getPerimeter(){
return 2*radius*Math.PI;
}
public void printCircle(){
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}

Solutions

Expert Solution

Create files with given names and paste the below code in it.

run demo.java file

>> javac demo.java

>> java demo

GeometricObject.java:

public class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

public GeometricObject () //empty constructor
{
dateCreated = new java.util.Date ();
}
public GeometricObject (String color, boolean filled) //parametrized constructor
{
dateCreated = new java.util.Date ();
this.color = color;
this.filled = filled;
}
//getter setter methods
public String getColor ()
{
return color;
}
public void setColor (String color)
{
this.color = color;
}
public boolean isFilled ()
{
return filled;
}
public void setFilled (boolean filled)
{
this.filled = filled;
}
public java.util.Date getDateCreated ()
{
return dateCreated;
}

  

}

Circle.java:

public class Circle extends GeometricObject   implements Comparable < Circle >
{
public double radius;

public Circle () //costructor
{
}
public Circle (double radius)
{
this.radius = radius;
}
public Circle (double radius, String color, boolean filled)
{
this.radius = radius;
setColor (color);
setFilled (filled);
}
public double getRadius ()
{
return radius;
}
public void setRadius (double radius)
{
this.radius = radius;
}
public double getArea ()
{
return radius * radius * Math.PI;
}
public double getDiameter ()
{
return 2 * radius;
}
public double getPerimeter ()
{
return 2 * radius * Math.PI;
}
public void printCircle ()
{
System.out.println ("The circle is created " + getDateCreated () +
           " and the radius is " + radius);
}
@Override
public int compareTo (Circle obj) //compareTo method which is used int sorting object
{
   //if current object radius is greater than given object radius then return -1
   if (obj.getRadius() > this.getRadius()) return -1;
  
   //if current object radius is less than given object radius then return 1
   if (obj.getRadius() < this.getRadius()) return 1;
return 0; //if both radius are equal return 0
     
}
@Override
public String toString () //toString() method which display
{
return "created on " + this.getDateCreated() + "\ncolor: " + this.getColor() +
" and filled: " + this.isFilled();
}
public boolean equals(Circle obj) //equals method which comapres two Circle class objects
{
   if(this.radius==obj.radius)
       return true;
   return false;
}
  
}

demo.java:

import java.util.Arrays;


public class demo {
   public static void main(String[] args)
   {
       Circle[] arr=new Circle[5]; //creating Class Circle array type
       arr[0]=new Circle(5.4); //adding objects
   arr[1]=new Circle(3.2);
   arr[2]=new Circle(6.9);
   arr[3]=new Circle(5.5);
   arr[4]=new Circle(9.7);
  
   System.out.println("Before: ");
   for(int i=0;i<5;i++) //printing
   {
       System.out.println(arr[i].getRadius());
   }
  
   Arrays.sort(arr); //sorting array using collections
   System.out.println("After: ");
   for(int i=0;i<5;i++) //printing
   {
       System.out.println(arr[i].getRadius());
   }
  
   Circle ncir=new Circle(9.7); //new circle object
   System.out.println("Cirlce arr[4] is equals tp Circle ncir: "+arr[4].equals(ncir)); //calling equals method
      
   }
  
}

OUTPUT:

If you have any doubts please ask in the comments section

Please rate my answer if you liked it.


Related Solutions

IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable radius = -999 declare character choice create do while loop inside of do loop write: System.out.println(); System.out.println("*** CIRCLE CALCULATIONS ***"); System.out.println(); System.out.println("1. Enter the radius of the circle"); System.out.println("2. Display the area of the circle"); System.out.println("3. Display the circumference of the circle"); System.out.println("4. Quit"); System.out.println(); System.out.println("Enter a number from 1 - 4"); System.out.println(); Declare choice character and relate to scanner declare switch (choice) case...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT