Cicles Class and Tester in Java: Problem Specification the Circle class represen
ID: 3670328 • 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
Circle.java
import java.util.Scanner;
import java.lang.Math;
public class Circle {
double radius;
String color;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Circle(double d,String color) {
this.radius=d;
this.color=color;
}
public static double getCircumference(double radius) {
return 2 * Math.PI * radius;
}
public static double getArea(double radius) {
return Math.PI * radius * radius;
}
}
TesterCircle.java
import java.util.Scanner;
public class TesterCircle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double rad=sc.nextDouble();
String color=sc.next();
Circle c=new Circle(rad,color);
System.out.println("Circumference: ");
System.out.println(c.getCircumference(rad));
System.out.println("Area: ");
System.out.println(c.getArea(rad));
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.