Cicles Class and Tester in Java: Problem Specification the Circle class represen
ID: 3670422 • Letter: C
Question
Cicles Class and Tester in Java:
Problem Specification
the Circle class represents circles. A circle has a color ("red", "yellow", "blue"), and a radius. separate circle methods must retrun the color, calculate and return the circumference, and calculate and retrun the area.
the CircleTester class uses the Circle class, gets user input to create a new circle and does calculations on it.
The Circle Class
Paratial pseudocode outline of Circle:
constants: set PI to 3.1415926
instance variables:
as specified above
methods:
constructor(radius parameter, color parameter)
initialize all instance variables
-design other methods, parameters and return types as specified above. NONE OF THE CIRCLE METHODS PRINT OUT ANTHING. Instead, all of them return a value back to the method classa in CirlceTester.
algorithms:
circumference is 2PIr
area is PIrr
The CircleTester Class
put all CircleTester code into the main() method. Declare in main() any local variables that are needed. Here is pseudocode outline.
instance variables:
none
methods: main()
prompt user for and read from the keyboard a radius
prompt user for and read from the keyboard a color (use next())
create a new Circle object with this radius and color
print the color of the circle. Call a Circle method as you do this.
print the circumference of the circle (to 2 decimal places) call a Circle method as you do this.
print the area of the circle (to 2 decimal places) call a Circle method as you do this
Explanation / Answer
Please find the required solution:
import java.text.DecimalFormat;
import java.util.Scanner;
//declaration of class circle
class Circle {
public static final double PI = 3.1415926;
String color;
double radius;
// two argument constructor for circle
public Circle(String color, double radius) {
this.color = color;
this.radius = radius;
}
// getter and setter for properties
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
// calculate and return circumference
public double getCircumference() {
return 2 * PI * this.radius;
}
// calculate and return area
public double getArea() {
return PI * this.radius * this.radius;
}
}
public class CircleTester {
public static void main(String[] args) {
// read input from keyboard(resource)
Scanner input = new Scanner(System.in);
System.out.println("enter the radius:");
double radius = input.nextDouble();
System.out.println("enter the color:");
String color = input.next();
// create object of circle
Circle circle = new Circle(color, radius);
// formatter used to limit decimal place to 2
DecimalFormat formatter = new DecimalFormat(".00");
// print the result
System.out.println("Color of circle:" + circle.getColor());
System.out.println("Circumference of circle:" + formatter.format(circle.getCircumference()));
System.out.println("Area of circle:" + formatter.format(circle.getArea()));
// close the resource
input.close();
}
}
Sample output:
enter the radius:15
enter the color:
red
Color of circle:red
Circumference of circle:94.25
Area of circle:706.86