



|
|
Byron's CSC212 Web Site
|
Class Notes
Monday October 2 , 2000
|
|
|
Notes on Boolean Data Types, Control Loops, and Incremantal Programming.
Class Notes --
Monday October 2 , 2000
CSC212 - October 2, 2000
========================
DEF: A COMMAND is a program that performs an action.
DEF: An OPERATOR is a program that returns a value.
Ex: In the context of the Circle class
shrink() is a command
area() is an operator
Ex: In the context of the .IO class
println(<int>) is a command.
read_int() is an operator
int x = IO.read_int()
Ex: Operator Definition - The operator computes the real a+
verage of two integers.
(stuff) double average(int x,int y);
{
int sum = x + y
double average = sum/2.0;
return average;
}
Using the operator:
Application:
double a = average(2,5);
Execution:
average(2,5);
1) 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
2) the Body of the method is executed in the context of
the "Parametric Bindings"
Sum -> 7 (2+5)
Average -> 3.5 (The value returned)
Calling Agent
Responding Agent
Here is what we use it for:
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 & 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,
doubleside)
{
Circle c = new Circle(radius);
Square r = new Square(side);
double ave = (c.area + s.area)/2.0;
return ave;
}
}
The "Block Structure of this Application:
_______________________________
| |
| AverageCS |
| ___________________ |
| | | |
| | Main() | |
| |___________________| |
| ___________________ |
| | | |
| | Average ( ) | |
| |___________________| |
|_______________________________|
Notes on the Execution:
Suppose 10.0 and 20.0 are the numbers which will
be read.
In main(): r -> 10.0 Bound to
In main(): s -> 20.0 Bound to
In averageCS(-): radius -> 10.0 Parameter passing
In averageCS(-): side -> 20.0 Parameter passing
In averageCS(-): c -> r = 10.0
s -> s = 20.0
ave -> 357.1
The value returned to the "main" method.
In Main: acs -> 357.1 Bound to
Example of a Command Definition:
A command which takes one integer and displays
both the predecessor and the successor.
stuff void print PS (int x)
(void indicates no value will be returned)
{
IO.println (x-1);
IO.println (x+1);
}
Example of the Use:
stuff main() (----)
{
printPS(5);
printPS(10);
}
Output: 4
6
9
11
Simple Interpreter (Lab for 10/4):
indigo> java NumberMachineApp
period "." exits interpreter
static private double current = 0.0;
// Pair Operation Code
Operant
//main method - calls interpreter
static private void intrepreter();
(Note: "void" is a command)
if (opcode == "+")
// <?times dispatcher>
if (opcode == "x")
// Process the times command
// <? times command>
Note: Until next class 10/10/00 you will only be in a
position to do the first (of four) parts of the
next programming assignment.
==========================================================+
123456789012345678921234567893123456789412345678951234567
|
|
|