a. Create a class named Circle with fields named radius, diameter, and area. Inc
ID: 667192 • Letter: A
Question
a. Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius()and getRadius(). The setRadius() method not only sets the radius but also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant for this calculation.) Save the class as Circle.java.
b. Create a class named TestCircle whose main() method declares several Circle objects. Using the setRadius() method, assign one Circle a small radius value, and assign another a larger radius value. Do not assign a value to the radius of the third circle; instead, retain the value assigned at construction. Display all the values for all the Circle objects. Save the application as TestCircle.java.
i need this in java thanks in advance.
Explanation / Answer
hi,
Here are the two classes. please check and let me know.
a) Circle.java
***************************************************************************************
/**
* @(#)Circle.java
*
*
* @author
* @version 1.00 2011/11/16
*/
public class Circle {
double radius,diameter,area;
public Circle() {
radius = 1;
diameter = radius * 2;
area = 3.14 * radius * radius;
}
public void setRadius(double r){
this.radius = r;
diameter = radius * 2;
area = 3.14 * radius * radius;
}
public double getRadius(){
return this.radius;
}
}
***************************************************************************************
b) TestCircle.java
***************************************************************************************
/**
* @(#)TestCircle.java
*
*
* @author
* @version 1.00 2011/11/16
*/
public class TestCircle {
public static void main(String[] args){
Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle();
//Set radius of 1st and 2nd circle while radius of 3rd circle is static at 1
c1.setRadius(0.5);
c2.setRadius(5);
//display Diameter of all 3 circle
System.out.println("Diameter of Circle 1: "+c1.diameter);
System.out.println("Diameter of Circle 2: "+c2.diameter);
System.out.println("Diameter of Circle 3: "+c3.diameter);
//display Area of all 3 circle
System.out.println("Area of Circle 1: "+c1.area);
System.out.println("Area of Circle 2: "+c2.area);
System.out.println("Area of Circle 3: "+c3.area);
}
}
***************************************************************************************
Please compile both the files and then run TestCircle.java
You will get Output as :
***********************************************************************************
--------------------Configuration: <Default>--------------------
Diameter of Circle 1: 1.0
Diameter of Circle 2: 10.0
Diameter of Circle 3: 2.0
Area of Circle 1: 0.785
Area of Circle 2: 78.5
Area of Circle 3: 3.14
Process completed.
***********************************************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.