The Triangle Class
CONSTRUCTORS
new Triangle (< double >) -- < triangle > (equilateral)
new Triangle < double >, < double >) -- < triangle > (isosceles)
(The first number is the length of two of the sides. )
new Triangle (< double >, < double >, < double >) -- < triangle >
OPERATORS
< Triangle > .sideA () -- < double >
< Triangle > .sideB () -- < double >
< Triangle > .side () () -- < double >
< Triangle > .area () -- < double >
< Triangle > .perimeter () -- < double >
COMMANDS
< Triangle > .display ()
EXAMPLES
/ /Display the area of a triangle of sides 5,
6.2, and 9.58
Triangle t = new Triangle (5, 6.2, 9.58);
IO.println (t.area ());
/ /Compute the area of the ``house'' which is
the icon on your website for going to the root
page of the site - assuming that a variable
named s is bound to the square which is its frame.
Triangle roof = new Triangle (s.side ());
double houseArea = s.area () + roof.area ());
|
PROBLEM
A pyramid with a square base measures 24.5 feet per side, at the base. It stands 92.4 feet at its high point. What is the surface area of the pyramid?
To solve this problem we can use problem decomposition. The surface area equals the area of the base + 4 x the area of a side.
Computing the area of the base is easy! Computing the area of a side is more challenging. We can do so using imaginitive construction.
SOLUTION
/ /Record the given information.
double pyramidHeight = 92.4;
double baseSide = 24.5;
/ /Create the base.
Square base = new Square (baseSide);
/ /Create the side by means of an
``imaginitive construction.''
double helperHeight = pyramidHeight;
double helperWidth = base.diagonal () / 2.0;
Rectangle helper = new Rectangle
(helperHeight, helperWidth);
double triangleBaseSide = baseSide;
double triangleOtherSide = helper.diagonal ();
Triangle side = new Triangle (baseSide,
triangleOtherSide, triangleOtherSide);
/ /Compute the total surface area.
double sa = base.area () + (4 * side.area ());
/ /Display the results.
IO.println (``Surface area = ``+ sa +
``square units.'');
|
The Polygon Class
A polygon will be modeled in two ways:
- degree: number of sides
- side: length of side
CONSTRUCTOR
new Polygon (< int >, < double >) -- < polygon >
OPERATORS
< polygon > .degree () -- < int >
< polygon > .side () -- < double >
< polygon > .perimeter () -- < double >
< polygon > .area () -- < double >
COMMAND
< polygon > .display ()
EXAMPLE:
/ /Display the area of a stop sign of side two feet.
Polygon stopSign = new Polygon (8, 2);
IO.println (stopSign.area ());
|
ENHANCED ``SHAPES WORLD'' FUNCTIONALITY
< Square > .circumscribingCircle () -- < Circle >
< Square > .inscribingCircle () -- < Circle >
< Circle > .circumscribingSquare () -- < Square >
< Circle > .inscribingSquare () -- < Square >