Thursday, 15 January 2015

// The "Trapezoid" class.
// Input 5 required values and then calculate the perimeter and area
// and output the inputted and calculated values.


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

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

    public static void main (String[] args)
    {
        c = new Console ();

        int sideA, sideC, sideD, base, height;
        int perimeter = 0;
        int area = 0;

        // prompt for and input the values required to calculate
        // both the perimeter and area of a trapezoid

        c.print ("Enter the value of side A of the trapezoid (in cm): ");
        sideA = c.readInt ();

        c.print ("Enter the value of side C of the trapezoid (in cm): ");
        sideC = c.readInt ();

        c.print ("Enter the value of side D of the trapezoid (in cm): ");
        sideD = c.readInt ();

        c.print ("Enter the value of the height of the trapezoid (in cm): ");
        height = c.readInt ();

        c.print ("Enter the value of the base of the trapezoid (in cm): ");
        base = c.readInt ();

        c.println ();       // one blank line
        c.println ("The 3 required values you've inputted are: ");
        c.println ("side A: " + sideA + " side C: " + sideC
                + " side D: " + sideD + " height: " + height
                + " and base: " + base);

        // first, calculate the perimeter of the trapezoid
        // according to the formula:  perimeter = sideA + sideC + sideD + base
        perimeter = sideA + sideC + sideD + base;

        // final output statement # 1
        c.println ();
        c.println ("The perimeter of the trapezoid is: " + perimeter + " cm");

        // next, calculate the area of the trapezoid
        // acocrding to the formula:  area = ((sideA + sideB) * height) / 2
        area = ((sideA + base) * height) / 2;

        // final output statement # 2
        c.println ();
        c.println ("The area of the trapezoid is: " + area + " cm");

    } // main method
} // Trapezoid class

No comments:

Post a Comment