Area and Circumference of a Circle

Study the program below, which uses both variables and constants:
//**********************************************************
//  Circle.java
//
//  Print the area of a circle with two different radii
//**********************************************************

public class Circle
{
    public static void main(String[] args)
    {
     final double PI = 3.14159;
     
     int radius = 10;  // Line A
     double area = PI * radius * radius;

     System.out.println("The area of a circle with radius " + radius +
                        " is " + area);

     radius = 20;  // Line B
     area = PI * radius * radius;

     System.out.println("The area of a circle with radius " + radius +
                        " is " + area);

    }
}
Some things to notice:

Save this program, which is in file Circle.java, into your directory and modify it as follows:

  1. The circumference of a circle is two times the product of Pi and the radius. Add statements to this program so that it computes the circumference in addition to the area for both circles. You will need to do the following: Be sure your results are clearly labeled. Copy the output into the lab document, labelling it Circle1.

  2. When the radius of a circle doubles, what happens to its circumference and area? Do they double as well? You can determine this by dividing the second area by the first area. Unfortunately, as it is now, the program overwrites the result of the first area's calculation with the calculation of the second (same for the circumference). You need to save the first area and circumference calculations that you compute instead of overwriting them with the second set of computations. So you'll need two area variables and two circumference variables, which means they'll have to have different names (e.g., area1 and area2). Remember that each variable will have to be declared. Modify the program as follows: Look at the results. Is this what you expected? If the results were not what you expected, please explain how so in your lab document. Copy the output into the lab document, labelling it Circle 2.

  3. In the program above, you showed what happened to the circumference and area of a circle when the radius went from 10 to 20. Does the same thing happen whenever the radius doubles, or were those answers just for those particular values? To figure this out, you can write a program that reads in values for the radius from the user instead of having it written into the program ("hardcoded"). Modify your program as follows: