



|
|
My Intro to Object-Oriented Programming
|
Class Notes
Monday September 25 , 2000
|
|
|
CG continued where we left off last Wednesday with the Triangle Class.
The operators for the Triangle Class are as follows:
- < Triangle > .sideA () -- > < double >, returns the length of side A
- < Triangle > .sideB () -- > < double >, returns the length of side B
- < Triangle > .sideC () -- > < double >, returns the length of side C
- < Triangle > .area () -- > < double >, returns the area of the triangle
- < Triangle > .perimeter () -- > < double >, returns the perimeter of the triangle
We also have the following command for the Triangle Class:
- < Triangle > .display (), returns a textual representation of the triangle
CG then demonstrated the use of the Triangle Class with a couple of example problems. One of the problems in particular was interesting. Given the height and the side base length for a pyramid (with a square base), what is the surface area? The Java solution for this problem utilized problem decomposition, imaginative construction, and the square, rectangle and triangle classes of the ``Shapes World''. In order to find out the two unknown sides of the triangle faces of the pyramid, CG demonstrated that this length could be determined by using imaginative construction. By using the height of the pyramid and half the diagonal of the pyramid base, a rectangle was constructed. The unknown sides of the triangle face = the diagonal of the ``imagined'' rectangle (very cool). Here is the code:
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?
class PyramidApp
/ /Record given information.
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.'');
|
Following the above example, we were introduced to the Polygon Class.
- Constructor: new Polygon (< int >, < double >) -- > < Polygon >, creates a polygon of < int > sides of length < double >
The operators for the Polygon Class include:
- < Polygon > .side () -- > < double >, computes the side length of the polygon
- < Polygon > .degree () -- > < double >, computes the number of sides of the polygon
- < Polygon > .perimeter () -- > < double >, computes the perimeter of the polygon
- < Polygon > .area () -- > < double >, computes the area of the polygon
We also have the following command for the Polygon Class:
- < Polygon > .display (), returns a textual representation of the polygon
We also learned some ``Shapes World'' functionality:
- < Square > .circumscibingCircle () -- > < Circle >, creates the circle circumscribing the given square
- < Square > .inscibingCircle () -- > < Circle >, creates the circle inscribing the given square
- < Circle > .circumscibingSquare () -- > < Square >, creates the square circumscribing the given circle
- < Circle > .inscibingSquare () -- > < Square >, creates the square inscribing the given circle
|
|