The problem
Write program that displays the area (size of the surface) and perimeter (the distance around) of a circle that has a radius of 5.5:
perimeter = 2 * radius * p area = radius * radius * p
Breaking it down
To make the program more reusable and modular we will define two methods named calculateArea
and calculatePerimeter
. If the program was capturing user input we could pass a parameter to the method to calculate the values. Next from the main
method we will call the method and set it to a local variable. Finally we will print the calculations to the console.
private static final double radius = 5.5;
public static void main(String[] args) {
double perimeter = calculatePerimeter();
double area = calculateArea();
System.out.println("Perimeter = " + perimeter);
System.out.println("Area = " + area);
}
private static double calculateArea() {
double area = radius * radius * Math.PI;
return area;
}
private static double calculatePerimeter() {
double perimeter = 2 * radius * Math.PI;
return perimeter;
}
Output
Perimeter = 34.55751918948772
Area = 95.03317777109125