A suggestive ``picture''
Input screen
PROGRAM
Output screen
5
THE COLLATZ PROGRAM
5, 16, 8, 4, 2 1
red white blue
SORT WORDS
blue red white
Remark
We want to write programs in Java that render the computer a special purpose problem solver.
the approach we will adopt
we will write Java programs (text) by means of a ``Template based slot + filler'' scheme.
Remark
A consise way of presenting a program in class is simply to display slots and fillers -- sinse most of the other stuff is invariant technical information.
Remark
In the sole executable instruction of the ``Hello World! '' program... the message println ( ``Hello World! '' ) is sent to the object IO which responds behaviorally by rinting the character string ``Hello World! '' to the standard output file.
Remark
One can interpret the line IO.println ( ``Hello World! '' ); in the following way:
- IO is an object
- println ( ``Hello World! '' ) is a message
Remark
The basic mechanism of computation is object oriented programming is the sending of messages to objects which respond with some form of behavior.
A java program to read two integers and display their sum.
< PROGRAM > -- > / /read the two integers and store
/ /them in memory
int n1 = IO.read_int ();
int n2 = IO.read_int ();
/ /compute the sum
int sum = n1 + n2;
/ /Display the sum, labled
IO.print ( ``the sum is: ``);
IO.print (sum);
IO.println ();
DEMO
> javac SumOfTwoIntsApp.java (RET)
> java SumOfTwoIntsApp (RET)
12 (RET)
7 (RET)
The sum is:19
Enviornment (bindings in memory)
n1 ---- > 12
n2 ---- > 7
sum --- > 19
Remark
In java all variables must be introduced by specifying the type of value they can denote.
Specification of variable Declaration
< TYP > < IDENTIFIER >
ex
int n1 = IO.read_int ();