In: Computer Science
(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);
}
}
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.