1 Sample Starting programs for PP 8.11

Sample Starting programs for PP 8.11
/* ShapeTester.java
Preparer: Your Name
* Start-up Solution to Programming Project 8.11
*/
import java.io.*;
import java.util.*;
public class ShapeTester
{
//--------------------------------------------------------------------------// Read the dimensions of various 3-D shapes from an input file, then
// displays pertinent information about each shape.
//--------------------------------------------------------------------------public static void main (String[] args)
{
try
{
Scanner scan = new Scanner (new File("shapes.dat"));
double width, length, height, side, radius;
// Read the data from the input file
while (scan.hasNext())
{
String shapeType = scan.next();
if (shapeType.equalsIgnoreCase("tetrahedron"))
{
side = scan.nextDouble();
height = scan.nextDouble();
System.out.println (new Tetrahedron (side, height));
}
}
}
catch (Exception except)
{
System.err.println(except);
}
}
}
/* Shape.java
Preparer: Your Name
* Start-up Solution to Programming Project 8.11
*/
public abstract class Shape
{
abstract public double computeArea();
abstract public double computePerimeter();
}
Current contents of fileshapes.dat:
Tetrahedron 4 5
Sample Run:
Triangle Pyramid: Height is 5
perimeter of base is 12, area is 37.72
volume is 11.55
1
/* Triangle.java 0 Preparer: Your Name
* Start-up Solution to Programming Project 8.11 (
*/
import java.text.*;
public class Triangle extends Shape
{
protected double side;
protected static DecimalFormat form = new DecimalFormat("0.##");
//--------------------------------------------------------------------------// Sets up the triangle by entering the length of a side
//--------------------------------------------------------------------------public Triangle (double sid)
{
side = sid;
}
//--------------------------------------------------------------------------// Returns the double value of the side
//--------------------------------------------------------------------------public double getSide()
{
return side;
}
//--------------------------------------------------------------------------// Returns the double value of the height of the triangle
//--------------------------------------------------------------------------public double getHeight()
{
return Math.sqrt(Math.pow(side, 2) - Math.pow(side / 2, 2));
}
//--------------------------------------------------------------------------// Returns the calculated value of the area
//--------------------------------------------------------------------------public double computeArea()
{
return side * getHeight() / 2;
}
//--------------------------------------------------------------------------// Returns the calculated value of the perimeter
//--------------------------------------------------------------------------public double computePerimeter()
{
return side * 3;
}
//--------------------------------------------------------------------------// Returns pertinent information about the triangle
//--------------------------------------------------------------------------public String toString()
{
return "Triangle: side length is " + form.format(side) +
"\nperimeter is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea());
}
}
2
/*Tetrahedron.java
Preparer: Your Name
* Start-up Solution to Programming Project 8.11
*/
public class Tetrahedron extends Triangle
{
private double height;
// Sets up the pyramid by entering its base side length and height
//--------------------------------------------------------------------------public Tetrahedron (double sid, double hei)
{
super(sid);
height = hei;
}
// Returns the double value of the height
//--------------------------------------------------------------------------public double getPyramidHeight()
{
return height;
}
// Returns the calculated value of a face height
//--------------------------------------------------------------------------public double faceHeight()
{
return Math.sqrt(Math.pow(height, 2) + Math.pow(getHeight() / 2, 2));
}
// Returns the calculated value of a face area
//--------------------------------------------------------------------------public double faceArea()
{
return faceHeight() * side / 2;
}
// Returns the calculated value of the surface area
//--------------------------------------------------------------------------public double computeArea()
{
return 3 * faceArea() + super.computeArea();
}
// Returns the calculated value of the volume
//--------------------------------------------------------------------------public double computeVolume()
{
return super.computeArea() * height / 3;
}
// Returns pertinent information about the pyramid
//--------------------------------------------------------------------------public String toString()
{
return "Triangle Pyramid: Height is " + form.format(height) +
"\nperimeter of base is " + form.format(computePerimeter()) +
", area is " + form.format(computeArea()) +
"\nvolume is " + form.format(computeVolume()) + "\n";
}
}
3
Relate calculations
Given: Triangle side = s = 4 and pyramid height = h = 5.
s
s 2 ( )2
2
The height of the base is calculated by: hbase
The area of the base (equilateral triangle) is:
1
( ) s.hbase
2
Abase
To find the area of the other 3 faces, the height of the face is needed:
The face area then is calculated:
Then the total area is:
And the volume is:
hbase 2
)
3
h face
h2 (
Aface
1
( ) s.h face
2
Atotal
Abase 3. Aface
1
( ) Abase .h
3
V
Here is the summary of calculations:
4