Definition
A COMMAND is a program that performs an action (other than returning a value).
Definition
An OPERATOR is a program that returns a value.
EXAMPLES:
In the context of the Circle class....
- area () is an operator
- shrink (_) is a command
In the context of the IO class....
- print (< int >) is a command
- read_int is an operator
Example 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;
|
Using the operator...
double a = average (2, 5);
|
The Execution....
average (2, 5)
- parameters are ``passed.'' This means bindings are established from the ``formal parameters'' in the definition to the ``actual parameters'' in the application.
- The body of the method is executed in the context of the parametric bindings.
PROBLEM
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 (String args [])
{
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 private 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;
}
}
|
Example of a command definition
Command which takes one integer and displays both the predecessors and the successor.
static private void printPS (int x)
{
IO.println (x - 1);
IO.println (x + 1);
}
|
Example use...
static public void main (String args [])
{
printPS (5);
printPS (10);
}
|