Write a program that creates a new circle of radius 2.0 and then shows the area
ID: 3869190 • Letter: W
Question
Write a program that creates a new circle of radius 2.0 and then shows the area of that circle. Your solution should be two lines of code:
Use the constructor to create a circle with radius 2.0 as the parameter, and store this new circle into a variable. The name of this variable will be used as the name of the object in the next step.
Use the showArea method to show the circle's area. Since this is a non-static method, you should put the name of the object before the dot, rather than the name of the class
public class Exercise2 {
public static void main(String[] args) {
// INSERT YOUR CODE HERE
}
}
Explanation / Answer
Exercise2.java
public class Exercise2 {
private double circle;
public Exercise2(double radius) {
circle=radius;
}
public void showArea() {
double area = Math.PI * circle * circle;
System.out.println("Circle area: "+area);
}
public static void main(String[] args) {
// INSERT YOUR CODE HERE
Exercise2 circle = new Exercise2(2.0);
circle.showArea();
}
}
Output:
Circle area: 12.566370614359172
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.