



|
|
My Intro to Object-Oriented Programming
|
Class Notes
Wednesday September 20 , 2000
|
|
Discussion continued in the ``Shapes World'' with the introduction of the Square Class.
- Constructor: new Square (< double >) -- > < Square >, creates a square using a given side length
The operators for the Square Class include:
- < Square > .side () -- > < double >, computes the side length of the square
- < Square > .area () -- > < double >, computes the area of the square
- < Square > .perimeter () -- > < double >, computes the perimeter of the square
- < Square > .diagonal () -- > < double >, computes the diagonal of the square
Commands for the Square Class include:
- < Square > .describe (), describes the square
- < Square > .expand (< double >), increases the side length of the square by the given number
- < Square > .shrink (< double >), decreases the side length of the square by the given number
Using the Square Class, CG then demonstrated their use by coding this interesting Java examples.
The design of a square flag is as follows:
- the background is blue
- the middleground is a white disk of maximal size, minus the foreground
- the foreground is a blue square which inscribes the disk
If the flag measures 3 feet on one side, what is the blue area of the flag?
class BlueAndWhiteFlagApp
import blue.shapes. *;
/ /Model the flag.
double flagSide = 3.0;
Square flag = new Square (flagSide);
/ /Model the disk.
double diskDiameter = flagSide;
double diskRadius = flagSide / 2.0;
Circle disk = new Circle (diskRadius);
/ /Model the interior square.
double helperSquareSide = flagSide / 2.0;
Square helper = new Square (helperSquareSide);
double smallSquareSide = helper.diagonal ();
Square smallSquare = new Square (smallSquareSide);
/ /Compute the blue area.
double blueArea = flag.area () - disk.area () + smallSquare.area ();
/ /Display result.
IO.println (``The blue area is ``+ blueArea + ``square units.'');
|
The concept of Imaginative Construction was presented.
CG ended the lecture with a quick introduction to the Triangle Class. Because of the nature of triangles, three different constructors were presented:
- new Triangle (< double >) -- > < Triangle >, produces a triangle for all sides of equal length
- new Triangle (< double >, < double >) -- > < Triangle >, the first number represents the length of two of the sides of the triangle
- new Triangle (< double >, < double >, < double >) -- > < Triangle >, use this operator if all the sides are of different length
|
|