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

Java: Write a class encapsulating the concept of a circle, assuming a circle has

ID: 3593840 • Letter: J

Question

Java:

Write a class encapsulating the concept of a circle, assuming a circle has the following:

two fields: a point which is an object of type Point (from java.awt package) representing the center of the circle, and the radius (int) of the circle

secondary constructor (default constructor should not be implemented)

accessor methods for each field

mutator methods for each field

toString method

equals method

a "business" method calculating and returning the primeter of the circle (2 * * radius)

a "business" method calculating and returning the area of the circle ( * radius2)

write a client class to test all the methods in your class

Explanation / Answer

import java.awt.Point;

/**

* @author pravesh

*

*/

public class Circle {

   // instance variables

   private double radius;

   private Point centre;

   private static final double PI = 3.14159;

   // constructors

   public Circle() {

       radius = 0;

   }

   /**

   * @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;

   }

   public Point getCentre() {

       return centre;

   }

   public void setCentre(Point centre) {

       this.centre = centre;

   }

   /**

   * @return perimeter

   */

   public double getCircumference(){

       return 2*PI*radius;

   }

   /* (non-Javadoc)

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

   */

   @Override

   public String toString() {

       return "Radius: "+radius+", Center: "+centre;

   }

  

   @Override

   public boolean equals(Object obj) {

       if(obj instanceof Circle) {

           Circle c = (Circle)obj;

           if(c.centre.equals(centre) && c.radius == radius)

               return true;

       }

      

       return false;

   }

}

##########

import java.awt.Point;

public class CircleDriver {

   public static void main(String[] args) {

      

       Circle c = new Circle();

      

       c.setCentre(new Point(3, 2));

       c.setRadius(6);

      

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

       System.out.println("Circumference: "+c.getCircumference());

       System.out.println(c);

   }

}

/*

Sample run:

Area: 113.09723999999999

Circumference: 37.699079999999995

Radius: 6.0, Center: java.awt.Point[x=3,y=2]

*/

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