Introduction to Java Templates
Definition
A COMPUTER is a general purpose machine which can be made to act like a special purpose machine by means of a program.
We want to write programs in Java to render the computer a special purpose problem solver. The approach that we will adopt will be to write Java programs (texts) by means of a ``template based slot and filler'' scheme.
The following is an example of a simple program written in a template, which performs the simple task of printing ``hello world! '' to the screen.
/ /File: HelloApp.java
/ /Type: java application file
/ /Date: August 30, 2000
/ /Name: Kara Becker
/ /Line: Classical first program
------------------------
class HelloApp
IO.println (``Hello world! '');
Demo
------------------------
java HelloApp.java
HelloApp
Hello world!
|
Definition
A JAVA APPLICATION TEMPLATE is a text which is a Java program except for ``slots'' which must be ``filled.'' These templates contain five textual sections:
- general information
- application description
- required packages
- application class
- demo
A concise way of presenting a program in class is simply to display slots and fillers - since most of the other stuff is invariant technical information.
This is a presentation of ``Hello world'' using slots and fillers.
Class ---------- HelloApp
Author --------- Kara Becker
Line ----------- classical first program
Description ---- (Type in description of program)
Needs ---------- (Type in files that need to be
imported)
Program -------- IO.println (``Hello world! '');
|
One can interpret the line IO.println (``Hello world! '') in the following way:
- IO is an object
- println (``Hello world! '') is a message
The basic mechanism of computation in object oriented programming is the sending of messages to objects which respond with some form of behavior. 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 printing the string ``Hello world! '' to the standard output file.
Definition
OBJECT-ORIENTATED PROGRAMMING is a style of programming which features the modeling of objects in a hierarchical fashion and the sending of messages to appropriately behaving objects.
Definition
A MESSAGE SEND is a construct of the form < object >. < message > which tells the computer to ``send the message to the object (which knows how to process it) .''
Hypothetical message sends:
- hand.display ()
- dime.area ()
This is an example of a Java program that will read two integers and display their sum.
/ /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 labeled.
IO.print (``The sum is: ``);
IO.print (sum);
IO.println ();
--------------------------------
Demo
Javac SumOfTwoIntsApp.java
Java SumOfTwoIntsApp
12
7
The sum is:19
|
Environment (bindings in memory)
Definition
A VARIABLE is a name (identifier) which represents some concept and which may denote a value.
In Java all variables must be introduced by specifying the type of value they can denote.
Definition
A VARIABLE DECLARATION is the introduction of a variable into the program which specifies the type of value which the variable can denote.
Specification of variable declaration:
Example:
(t variable declaration)