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

Update the Circle class definition. You need to: Add a copy constructor to the c

ID: 3827117 • Letter: U

Question

Update the Circle class definition. You need to: Add a copy constructor to the class Circle Add a public setRadius (double r) Add a public method double getRadius() Create circle2 as a clone of circle1 in the main(). Display its circumference and area. Use setRadius(..) to change radius of circle 1. Does it affect circle2? Prove your answer by displaying relevant values. Declare another object reference variable Circle circle3 = circle2; Display the radius of circle2 and circle 3. Are these values the same? Change the radius of circle3 to a different value. Display the radius of circle2 and circle 3 again. As the radius of circle2 has not been changed, it should be different from the radius of circle3 that has been changed. Are they different? Explain the result.

Explanation / Answer

Hi, Please find my answer.

Please let me know in case of any issue.

/**

* @author pravesh

*

*/

public class Circle {

   // instance variables

   private double radius;

   private static final double PI = 3.14159;

   // constructors

   public Circle() {

       radius = 0;

   }

   // copy constructors

   public Circle(Circle other) {

       radius = other.radius;

   }

   /**

   * @param radius

   */

   public Circle(double radius) {

       this.radius = radius;

   }

   // getters and setters

   /**

   * @return radius

   */

   public double getRadius() {

       return radius;

   }

   /**

   * @param radius

   */

   public void setRadius(double radius) {

       this.radius = radius;

   }

   /**

   * @return area

   */

   public double getArea(){

       return PI*radius*radius;

   }

   /**

   * @return diameter

   */

   public double getDiameter(){

       return 2*radius;

   }

   /**

   * @return perimeter

   */

   public double getCircumference(){

       return 2*PI*radius;

   }

   /* (non-Javadoc)

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

   */

   @Override

   public String toString() {

       return "Radius: "+radius;

   }

   public static void main(String[] args) {

       Circle circle1 = new Circle();

       circle1.setRadius(4.5);

       Circle circle2 = new Circle(circle1);

      

       System.out.println("Area: "+circle2.getArea());

       System.out.println("Area: "+circle2.getCircumference());

      

      

       Circle circle3 = circle2;

       // here circle2 and circle3 pointing to same object

      

       System.out.println("Radius of c2: "+circle2.getRadius());

       System.out.println("Radius of c3: "+circle3.getRadius());

      

       circle3.setRadius(6);

      

       System.out.println("Radius of c2: "+circle2.getRadius());

       System.out.println("Radius of c3: "+circle3.getRadius());

   }

}

/*

Sample run:

Area: 63.6171975

Area: 28.27431

Radius of c2: 4.5

Radius of c3: 4.5

Radius of c2: 6.0

Radius of c3: 6.0

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote