 |
Starting Java
|
Modeling the program
-
Our first program is to make an announcement on the screen, that says:
This is my first Java program
but it won't be my last.
-
What are we modeling?
-
a human being making an announcement
-
What are the objects?
-
the human being
-
the monitor
-
What are the behaviors?
-
human being sends message to monitor
-
monitor has character printing behavior
-
In Java, the initiating object is the main program
How to write the program
-
We know that there's a predefined object called System.out,
which can put characters onto the screen
-
The way to get the behavior is to send the object a message
-
The first part of the message is the behavior we want, i.e., println
-
The second part of the message contains the arguments, which are the added
detail or extra information needed to do the job
Two lines = two messages to System.out
System.out.println ("This is my first
Java program");
System.out.println ("but it won't
be my last.");
Here, we used the System.out object,
but we haven't modeled the human, or main program.
A valid Java main program must have:
-
A name (called an identifier)
-
Extra notation (which has meaning, but we won't discuss it yet)
The program:
import java.io.*;
class Program1 {
public static void main (String[] arg) {
System.out.println ("This is my first Java program");
System.out.println ("but it won't be my last.");
}
}
The first three lines and the last two are going to be the same for this
week
-
Treat them as rules - any discipline has rules
Summary
-
We have modeled a real world example and, using our knowledge of predefined
objects, written a simple program that displays two lines on the screen.
Java stuff
Modeling the program as we've done is transferable to other languages.
-
There are, however, specific details in Java that we need to know about.
Java rules
-
Java has rules that dictate what an acceptable program looks like
-
e.g., can't say
System.out("This is my first java
program").println
-
violating these rules can be the source of many errors in beginners' programs
Identifiers
-
Classes, behaviors and objects must have a name - an identifier.
-
Rules for identifiers:
-
must contain only letters, digits and underscores
-
must begin with a letter
-
upper and lowercase letters are different
-
identifiers should be meaningful
-
call a spade a spade, and not x.
Keywords
-
Are special words that have special meaning in the Java language, e.g.,
import, class,
public, static,
void
-
Do not use keywords as identifiers!
Order of statements
System.out.println ("One, two, three");
System.out.println ("four, five, six");
is different to
System.out.println ("four, five, six");
System.out.println ("one, two, three");
Program format and comments
Format
-
Java is very flexible about format - the chief format rule is to put spaces
around two adjacent identifiers or keywords, e.g., class
Program1, not classProgram1
-
There is a readability issue though,
-
you looking at your code later on,
-
someone else is maintaining your code
-
Some general rules:
-
Write one statement per line
-
Use consistent spacing when indenting (e.g., use the TAB key)
-
This can be difficult to pick up, but you should use examples as your guide
Comments
-
Comments are notes that are ignored by the computer
-
help the human reader to interpret the code
-
In Java, anything that starts with /*
and ends with */ is a comment
-
Don't have to be on the same line
-
Also, line comments are lines that begin with //
-
Comments enhance the readability of a program, but should not be over used
-
Some rules
-
Each file containing a program should have the following header comment:
/* Filename: Program1.java
Author: Bradley Schmerl
Date: 8/24/98
Version: 1.0
Description: This program announces my first Java
program experience, and signals my
intent to continue.
*/
Note: All programs your write in this course must contain this header.
If more than one class per file, place comments just before the class
indicating the purpose and behavior of code
Don't explain how Java works (assume the reader knows this)
-
use comments to give insights that aren't readily apparent
Don't put comments in the middle of statements
Last modified: 1/10/99