Thursday, 15 January 2015

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

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

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

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

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

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

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

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

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

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


        // first, calculate the perimeter of the triangle
        // according to the formula:  perimeter = side A + side C + base
        perimeter = sideA + sideC + base;

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

        // next, calculate the area of the parallelogram
        // acocrding to the formula:  area = base * height
        area = (base * height) / 2;

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

    } // main method
} // Triangle class

No comments:

Post a Comment