



|
|
CS1 Course Site
|
Wait , WAIT !!What's that word ? ( Class Notes ( without definitions ) )
Monday October 2 , 2000
|
|
Operators and Commands
area () is an operator.
shrink () is a command.
in the context of the IO class...
println (< int >) is a command
read_int () is an operator
Example operator definition
The operator computes the real average of 2 integers
---- double average (int x, int y)
{
int sum = x + y;
double avg = sum / 2.0;
return avg;
{
Using the operator...
double a = average (2, 5); = 3.5
The Execution of
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 x - > 2 y - > 5
- The body of the method is executed in context of the parametric Bindings. sum - > 7 avg - > 3.5 the value returned.
Here is what we use it for
Problem
Read two real numbers. Interpret the first as the radius of the circle, and the second as the side of a square.
Display the average value of the areas of the corrisponding 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 private double averageCS (double radius, double side);
{
Circle c = new Circle (radius);
Square s = new Square (side);
double avg = ((c.area () + s.area ()) / 2.0);
return avg;
}
}
Note on the execution....
Suppose 10.0 and 20.0 are the numbers which will be read:
In main (): r -- > 10.0
In main (): s -- > 20.0
``In'' averageCS (--):
radius -- > 10.0
side -- > 20.0
In averageCS (--): c -- > 10.0
In averageCS (--): s -- > 20.0
In averageCS (--): avg -- > 357
In main (): avg -- > 357
Example of a command definition
A command which takes ONE integer and displays both the predecessor and the successor.
----- void printPS (int x)
{
IO.println (x-1)
IO.println (x + 1)
{
Example use
----- main ()
{
printPS (5);
printPS (10);
}
output screen... 4, 6, 9, 11
Note
Until wednesday ou will only be in a position to do the first (of four) parts of the next programming assignment.
|
|