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

JAVA PROGRAM HELP: I need the following code. Write a class named Circle which r

ID: 3885680 • Letter: J

Question

JAVA PROGRAM HELP: I need the following code.

Write a class named Circle which represents a circle shape as described below: 1. Two attributes, color and radius. 2. A default constructor which initializes the attributes to red and 5, respectively. 3. A copy constructor. 4. A compareTo() method to determine if two Circle objects are the equal. Equality should be defined as the same color and the same radius. 5. A toString() method that returns the color and radius as a string. 6. A set function that allows the color of an object to be changed. 7. A set function that allows the radius of an object to be changed. Write a second class with a main function that declares two Circle objects: C1 and C2. Create a copy of C2 in a third object, C3 Compare the C1 and C2 and print the results. Change the color of C1. Compare C1 and C3 and print the results. Set the radius of C2 to 20. Print the results of toString() for C2.

Explanation / Answer

public class Circle implements Comparable<Circle> {

// members of circle

String color;

int radius;

/**

* default constructor

*/

public Circle() {

// TODO Auto-generated constructor stub

this.color = "Red";

this.radius = 5;

}

/**

* copy constructor to copy color and radius

*

* @param c

*/

public Circle(Circle c) {

// TODO Auto-generated constructor stub

this.color = c.color;

this.radius = c.radius;

}

/*

* method to compare the objects (non-Javadoc)

*

* @see java.lang.Comparable#compareTo(java.lang.Object)

*/

@Override

public int compareTo(Circle o) {

// TODO Auto-generated method stub

if (o.color.equals(this.color) && o.radius == this.radius)

return 1;

else

return 0;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "Circle [color=" + color + ", radius=" + radius + "]";

}

/**

* @param color

* the color to set

*/

public void setColor(String color) {

this.color = color;

}

/**

* @param radius

* the radius to set

*/

public void setRadius(int radius) {

this.radius = radius;

}

}

public class TestCircle {

/**

* main method to test

*

* @param args

*/

public static void main(String[] args) {

// objects creation

Circle C1 = new Circle();

Circle C2 = new Circle();

Circle C3 = new Circle(C2);

// comparing objects

if (C1.compareTo(C2) == 1)

System.out.println("C1 and C2 are equal");

else

System.out.println("C1 and C2 are not equal");

// set color to green

C1.setColor("Green");

// comparing objects

if (C1.compareTo(C3) == 1)

System.out.println("C1 and C3 are equal");

else

System.out.println("C1 and C3 are not equal");

// set radius to 20

C2.setRadius(20);

// print the object C2

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

}

}

OUTPUT:

C1 and C2 are equal
C1 and C3 are not equal
Circle [color=Red, radius=20]