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

Write a class encapsulating the concept of a circle, assuming a circle has the f

ID: 3646217 • Letter: W

Question

Write a class encapsulating the concept of a circle, assuming a circle has the following attributes:
two doubles representing the x and y coordinates of the center of the circle
a double representing the radius of the circle

Include the following methods:
two constructors
accessor methods
mutator methods
toString and equals
methods returning the perimeter (2 * B * radius) and area (B * radius2) of the circle

Write a client class to test all the methods in your class ( See sample output from the test program on the
next page.).

UML diagram for the Circle class:
Circle
- centerX: double
- centerY: double
- radius: double
+ Circle( )
+ Circle( initCenterX: double, initCenterY: double, initRadius: double )
+ getCenterX( ): double
+ getCenterY( ): double
+ getRadius( ): double
+ getPerimeter( ): double
+ getArea( ): double
+ setCenterX( newCenterX: double )
+ setCenterY( newCenterY : double )
+ setRadius( newRadius: double )
+ equals( otherCircle: Circle ): boolean
+ toString( ): String

The no-argument constructor should initialize the circle to be a unit circle with center at the origin.

The explicit-value constructor ( the second one in the UML diagram ) should use the setRadius
method to initialize the radius.

The setRadius method will validate the newRadius and set the radius of the circle to newRadius if
newRadius >= 0, othersies it will set the radius of the circle to 0.

The toString method should return a String that is in the format shown in the example below.

EXAMPLE: For a circle with center at (5, 10) and a radius of 3 to String would return:
Center: (5, 10) Radius: 3
Output from the test program should look something like the following,
(You may use different values for your circles if you like.):

TESTING CONSTRUCTORS:
No-Argument circle: Center: (0, 0) Radius: 1
Circle instantiated using center at (15, 25) and a radius of 50 is: Center: (15, 25) Radius: 50
Circle instantiated using center at (15, 25) and a radius of -5 is: Center: (15, 25) Radius: 0

TESTING ACCESSOR METHODS:
x-coordinate of the center is: 15
y-coordinate of the center is: 25
radius is: 50
perimeter is: ___________
area is: _____________

TESTING MUTATOR METHODS:
After using the set methods to change the center to: (1, 2) and the radius to 10:
The circle is: Center: (1, 2) Radius: 10
After changing the radius to -15:
The circle is: Center: (1, 2) Radius: 0

TESTING EQUALS METHOD:
circle1 is: Center: (1, 1) Radius: 1
circle2 is: Center: (2, 2) Radius: 2
circle3 is: Center: (1, 2) Radius: 1
circle4 is: Center: ( 2, 2) Radius: 2
circle1 does not equal circle 2
circle 1 does not equal circle 3
circle 1 equals circle4

Of course your output would have the perimeter and area blanks filled in.

Make sure you use all the method of the Circle class appropriately to produce this output.

Explanation / Answer

import java.io.*; class Circle { double centerX,centerY,radius; Circle() { centerX=centerY=0; } Circle(double centerX,double centerY,double radius) { this.centerX=centerX; this.centerY=centerY; this.radius=radius; } double getCenterX() { return centerX; } double getCenterY() { return centerY; } double getRadius() { return radius; } double getCenterX() { return centerX; } double getPerimeter() { return 2*3.14*radius; } double getArea() { return 3.14*radius*radius; } void setCenterX(double centerX) { this.centerX=centerX; } void setCenterY(double centerY) { this.centerY=centerY; } void setRadius(double radius) { this.radius=radius; } boolean equals(Circle c) { if((this.c).equals(c)) return true; else return false; } String toString() { String s="Center("+this.centerX+","+this.centerY+")Radius:"+this.radius; return s; } } class CircleTest { public static void main(String args[]) { Circle c=new Circle(15,25,50); Circle c1=new Circle(15,25,0); //c & c1 are free to use any of the above declared methods in the class Circle } }

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