Run the following file that has two classes in it: Understand how objects circle
ID: 3762563 • Letter: R
Question
Run the following file that has two classes in it:
Understand how objects circle1, circle2, and circle3 are created and manipulated.
Now remove the SimpleCircle class from the TestSimpleCircle.java file, and then compile the following file that is made up of one class:
Again, run your modified TestSimpleCircle class, and understand how objects circle1, circle2, and circle3 are created and manipulated.
Next, make some changes to SimpleCircle.java to create 2 more objects, called circle4 and circle5. Print out the radius and area of each circle object similar to other circle objects.
Turn in your modified SimpleCircle.java
Explanation / Answer
public class TestSimpleCircle {
/**
* Main method
*/
public static void main(String[] args) {
// Create a circle with radius 1
SimpleCircle circle1 = new SimpleCircle();
System.out.println("The area of the circle of radius "
+ circle1.radius + " is " + circle1.getArea());
// Create a circle with radius 25
SimpleCircle circle2 = new SimpleCircle(25);
System.out.println("The area of the circle of radius "
+ circle2.radius + " is " + circle2.getArea());
// Create a circle with radius 125
SimpleCircle circle3 = new SimpleCircle(125);
System.out.println("The area of the circle of radius "
+ circle3.radius + " is " + circle3.getArea());
// Create a circle with radius 50
SimpleCircle circle4 = new SimpleCircle(50);
System.out.println("The area of the circle of radius "
+ circle4.getRadius() + " is " + circle4.getArea());
// Modify circle radius
circle2.setRadius(100);
System.out.println("The area of the circle of radius "
+ circle2.radius + " is " + circle2.getArea());
// Create a circle with radius 50
SimpleCircle circle5 = new SimpleCircle(500);
System.out.println("The area of the circle of radius "
+ circle5.getRadius() + " is " + circle5.getArea());
}
// Define the circle class with two constructors
static class SimpleCircle {
double radius;
private double area;
/**
* Construct a circle with radius 1
*/
SimpleCircle() {
radius = 1;
}
/**
* Construct a circle with a specified radius
*/
SimpleCircle(double newRadius) {
radius = newRadius;
area = radius * radius * Math.PI;
}
/**
* Return the area of this circle
*/
double getArea() {
return area;
}
/**
* Return the perimeter of this circle
*/
double getPerimeter() {
return 2 * radius * Math.PI;
}
/**
* Set a new radius for this circle
*/
void setRadius(double newRadius) {
radius = newRadius;
area = radius * radius * Math.PI;
}
double getRadius() {
return radius; //To change body of generated methods, choose Tools | Templates.
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.