Write a class encapsulating the concept of a circle, a circle has the following
ID: 3896446 • Letter: W
Question
Write a class encapsulating the concept of a circle, a circle has the following attributes: a Point representing the center of the Circle, and a none negative number-the radius of the circle. Include constructor, the accessors and mutators, perimeter, area methods, and overwrite toString methods. Here are the outline of the classes you need to define: Circle (the class name must be named as Circle.java): data member: center (Point type), radius (double) constructor: takes two parameter (Point center, double radius) and the no parameter constructor which sets its center to (1.0, 1.0) and radius to 1.0 a circle radius must be within the range (0, 25], otherwise set to its default radius (1.0) methods: all getter and setter methods perimeter returns the perimeter of the circle area return the area of the circle toString returns the circle information as follows:Radius: 1.00 Center[1.00, 1.00] Perimeter: 6.28 Area: 3.14
Point (must be named as Point.java): data member: x (double for x-axis) and y (double for y-axis)
All getter and setter methods constructor: two parameters to set x and y, and a no parameter constructor with default value: x=0 and y=0;
Overwrite toString method which returns the point information as follows:
[1.00, 1.00]
CircleTester(driver class to test your classes)
1. Create a java file named CircleTester.java 2. Open the file named CircleTester.txt, copy and paste the content to your CircleTester.java 3. Compile and run the CircleTester.java to test your Point and Circle classes
If your Point and Circle classes are defined properly, the tester program should generate the following output:
Radius: 2.00 Center[0.00, 0.00] Perimeter: 12.57 Area: 12.57 Radius: 10.00 Center[-1.00, -10.00] Perimeter: 62.83 Area: 314.16 Radius: 1.00 Center[2.00, -1.00] Perimeter: 6.28 Area: 3.14 Radius: 1.00 Center[-1.00, -10.00] Perimeter: 6.28 Area: 3.14 Radius: 25.00 Center[0.00, 0.00] Perimeter: 157.08 Area: 1963.50 Radius: 1.00 Center[0.00, 0.00] Perimeter: 6.28 Area: 3.14 A À Circle Tester.txt E i public class CircleTester 3 public static void main (Stringt] args) t 5 Circle cl- new Circle (new Point (0, 0), 2) Circle c2 new Circle (new Point (-1,-10), 10); Circle c3new Circle (new Point (2, 1), -5) system.out.printin (cl); System.out.printin (c2) System.out printin(c3) c2.setRadius (0) System.out.printin(c2); c1.setRadius (25) System.out.printin (cl) c2.setRadius (25.05)
Explanation / Answer
Here is the completed code for this problem. (All three classes are defined in separate .java files) Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks.
// Point.java
public class Point {
// attributes
private double x;
private double y;
// constructor with no arguments
public Point() {
x = 0;
y = 0;
}
// constructor with arguments
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// getters and setters
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public String toString() {
// returning a properly formatted string
return String.format("[%.2f, %.2f]", x,y);
}
}
// Circle.java
public class Circle {
// attributes
private Point center;
private double radius;
// constructor with arguments
public Circle(Point center, double radius) {
this.center = center;
setRadius(radius);
}
// constructor with no arguments
public Circle() {
// default values
this.center = new Point(1, 1);
this.radius = 1;
}
// getters and setters
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
/**
* validating the radius before assigning
*/
if (radius > 0 && radius <= 25) {
this.radius = radius;
} else {
// invalid radius, resetting to default value
this.radius = 1;
}
}
/**
* @return the perimeter of the circle (2*PI*radius)
*/
public double perimeter() {
return 2 * Math.PI * radius;
}
/**
* @return the area of the circle (PI*radius*radius)
*/
public double area() {
return Math.PI * radius * radius;
}
@Override
public String toString() {
// returning a properly labeled string
return String.format(
"Radius: %.2f Center%s Perimeter: %.2f Area: %.2f", radius,
center, perimeter(), area());
}
}
// CircleTester.java
public class CircleTester {
public static void main(String[] args) {
/**
* Creating and testing Circle objects
*/
Circle c1=new Circle(new Point(0,0),2);
Circle c2=new Circle(new Point(-1,-10),10);
Circle c3=new Circle(new Point(2,-1),-5);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
c2.setRadius(0);
System.out.println(c2);
c1.setRadius(25);
System.out.println(c1);
c1.setRadius(25.05);
System.out.println(c1);
}
}
/*OUTPUT*/
Radius: 2.00 Center[0.00, 0.00] Perimeter: 12.57 Area: 12.57
Radius: 10.00 Center[-1.00, -10.00] Perimeter: 62.83 Area: 314.16
Radius: 1.00 Center[2.00, -1.00] Perimeter: 6.28 Area: 3.14
Radius: 1.00 Center[-1.00, -10.00] Perimeter: 6.28 Area: 3.14
Radius: 25.00 Center[0.00, 0.00] Perimeter: 157.08 Area: 1963.50
Radius: 1.00 Center[0.00, 0.00] Perimeter: 6.28 Area: 3.14
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.