



|
|
My Intro to Object-Oriented Programming
|
Class Notes
Monday October 02 , 2000
|
|
|
Class began with definitions of ' command' and ' operator'. See the Glossary for these definitions.
Here is an example of an operator definition:
The operator computes the real average of two integers.
~ ~ ~ ~ ~ double average (int x, int y)
{
int sum = x + y;
double ave = sum / 2.0;
return ave;
}
|
This example shows how we use operators and ``pass'' the parameters:
Read two real numbers. Interpret the first as the radius of a circle and the second as the side of a square. Display the average value of the areas of the corresponding circle and square.
class AverageCS
{
static public void main (~ ~ ~ ~ ~)
{
double r = IO.read_double ();
double s = IO.read_double ();
double acs = averageCS (r, s);
IO.println (``The average area is'' + acs + ``square units.'');
}
static public double averageCS (double radius, double side)
{
Circle c = new Circle (radius);
Square s = new Square (side);
double ave = (c.area () + s.area ()) / 2.0;
return ave;
}
}
|
Here is an example of a command definition:
The command takes one integer and displays both the predecessor and the successor.
~ ~ ~ ~ ~ void printPS (int x)
{
IO.println (x - 1);
IO.println (x + 1);
}
|
|
|