// Input 3 required values and then calculate and display
// the inputted values, surface area and volume of a
// square-based pyramid. To calculate the square-based pyramid's
// surface area, calculate that of a triangle, then that of a square
// and then add these two calculated values together.
import java.awt.*;
import hsa.Console;
public class SquareBasedPyramid
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
double base;
double height;
double side;
double triangle = 0;
double baseSqrd = 0;
double grandTotal = 0;
double volume = 0;
c.print ("Enter the value of the side of the square-based pyramid (in mm): ");
side = c.readDouble ();
c.print ("Enter the value of the base of the square-based pyramid (in mm): ");
base = c.readDouble ();
c.print ("Enter the value of the height of the square-based pyramid (in mm): ");
height = c.readDouble ();
// display the inputted values
c.println ("You just inputted one of two equal sides " + side
+ " the base " + base + " and height " + height
+ "(all in mm)");
// first, calculate the surface area of the square-based pyramid
// using 3 formulas triangle = (base * side) / 2
// baseSqrd = (base * base) and grandTotal = (4 * triangle) + baseSqrd
triangle = (base * side) / 2;
baseSqrd = (base * base);
grandTotal = (4 * triangle) + baseSqrd;
// final output statement # 1
c.println (); // one blank line
c.println ("The surface area of the square-based pyramid with a base "
+ "of " + base);
c.println ("and a side length of " + side
+ " is about: " + Math.round (grandTotal) + " mm squared");
// next, calculate the volume of the square-based pyramid
// using 2 formulas: baseSqrd = (base * base) and
// volume = (baseSqrd * height) / 3
// (however, no need to recalculate baseSqrd here)
volume = (baseSqrd * height) / 3;
// final output statement # 2
c.println ();
c.println ("The volume of the square-based pyramid with a base "
+ "of " + base);
c.println ("and a height of " + height
+ " is about: " + Math.round (volume) + " mm cubed");
} // main method
} // SquareBasedPyramid class
No comments:
Post a Comment