1. Implement the Circle Class. Implement the following class: public class Circl
ID: 3577116 • Letter: 1
Question
1. Implement the Circle Class. Implement the following class: public class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; } } Write a main method that will use the Circle constructors to create a circle object with the default radius (use the no-arg constructor for this) and then create a second circle object with radius 5.5 (use the constructor that lets you specify a radius). Use the Circle objects’ getArea method to compute the area of each circle. Your main method should print these areas.
Explanation / Answer
CircleTest.java
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle();
Circle c2 = new Circle(5.5);
System.out.println("Default constructor circle area is "+c1.getArea());
System.out.println("circle area is "+c2.getArea());
}
}
Circle.java
public class Circle {
/** The radius of this circle */
double radius = 1.0;
/** Construct a circle object */
Circle() {
}
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */
double getArea() {
return radius * radius * 3.14159;
}
}
Output:
Default constructor circle area is 3.14159
circle area is 95.0330975
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.