Thursday, 18 December 2014

// The "Calculate" class.
// Shows how to use variable and constant names to do calculations,
// in this case, the circumference and the area of a circle.

import java.awt.*;
import hsa.Console;

public class CalculateV2
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();
        // declare a constant for radius with value = 3
        final double radius = 3;
        // declare a constant for PI with value = 3.14159
        final double PI = 3.14159;
        // declare variables for circumference and area of a circle
        double circumf, area;
        // output the calculated values --> INCORRECT CALCULATION:
        // c.println ("circumference of a circle with diameter of 6 = "
        //            + PI / (radius*2));
        // CORRECT CALCULATION (for circumference):
        c.println ("circumference of a circle with diameter of "
                + (radius * 2) + " = " + 2 * PI * radius);
        // output the calculated values ---> INCORRECT CALCULATION:
        // c.println ("area of a circle with radius of 3 = "
        //            + PI * (radius * radius)
        // CORRECT CALCULATION (for area):
        c.println ("area of a circle with radius of " + radius + " = "
                + PI * (radius * radius));

    } // main method
} // Calculate class

No comments:

Post a Comment