Synopsis
This lecture covered more of Triangle class and it's use as well as problems utilizing the Triangle class to solve them. Another groovy class was introduced, the Polygon Class. Enhanced Shapes class functionality was intro'd as well.
|
The Triangle Class - picking up from the previous lecture
We will model a triangle in terms it's three sides.
Constructors
Equilateral triangle - new Triangle (< double >) - > < Triangle >
Isosceles triangle - new Triangle (< double >, < double >) - > < Triangle >
The first number is the length of 2 of the sides.
new Triangle (< double >, < double >, < double >) - > < Triangle >
Operators
< Triangle > .sideA () - > < double > - length of the side
< Triangle > .sideB () - > < double >
< Triangle > .sideC () - > < double >
< Triangle > .area () - > < double >
< Triangle > .perimeter () - > < double >
Command
< Triangle > .display () - > textually
|
Example:
1) 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 ());
2) 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 it's frame.
Triangle roof = new Triangle (s.side ());
double houseArea = s.area () + roof.area ();
A real problem involving triangles.
A pyramid with a square base measures 24.5 feet per side, at the base. It stands 92.4 feet at it's 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 plus four times the area of a side.
Computing the area of the base...is easy!
Computing the area of the side is..more challenging! We can do so using imaginative construction.
Break out one triangle and imagine a rectangle to complete calculations with. The height of the rectangle is the same as the height of the pyramid.
Program > - >
/ /Record given info
double pyramidHeight = 92.4;
double baseSide = 24.5;
/ /Create the base
Square base = new Square (baseSide);
/ /Create a side by means of ``imaginative 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 result
IO.println ( ``Surface Area = ``+ sa + ``square units'');
|
Polygons
A polygon will be modeled in terms of two properties
Degree - number of sides
Side - length of side
Constructor
new Polygon (< int degree >, < double sideLength >) - > < polygon >
Operators
< polygon > .degree (); - > < int >
< polygon > .side (); - > < double >
< polygon > .perimeter (); - > < double >
< polygon > .area (); - > < double >
Command
< polygon > .display ()
|
Example Tasks
1) Display the area of a stop sign of the side 2 feet.
Polygon stopSign = new Polygon (8, 2);
double area = stopSign.area ();
IO.println (area);
|
Lab notes were then discussed for the Wednesday September 27 th lab.
Enhanced ``Shapes World''
Functionality
< Square > .circumscribingCircle () - > < circle > (circle goes outside the square through the vertexes)
< Square > .inscribingCircle () - > < circle > (circle goes inside circle touching each side)
< Circle > .circumscribingSquare () - > < square > {the square outside the circle}
< Circle > .inscribingSquare () - > < square > (the square inside the circle in a diamond fashion)
|