CIS279-BASFORD-05/31/01

Java Programming
CIS279
Tori Basford
tori@bly.net

Assignment(s):
None

Exercise(s):

  1. Write a program to add numbers given by argument (command line).
  2. Write a program to print a specifed string n times (given by argument/command line).

Variables:
To acess a local variable:

  1. You must be inside the method.
  2. To acess it, just write to the name of the variable (ex. m=119);
To access an object variable:
  1. You must know the object's reference number. (for example, it might be stored in a variable such as CX)
  2. Yo acess the variable you write the variable containing the reference number and a selector telling Java what to select in the object. (ie, CX.r = 72;)
To access a class (static) variable:
  1. You must know the class name. (ex. PimaStudent)
  2. To access the variable you write . (ex. PimaStudent.age = 18)
for example:
The class Math contains a final static variable PI. PI is a final variable which means it is read-only and is called a Symbolic-Constant. Symbolic Constants are usually ALL CAPS.
The class PimaStudent contains a final static variable 'START_DATE'.

Static Variables are all CAPS and are usually contain underscores (_) to break up the different words.
Static Variables are defined by the format: static [final] variable_type

In PimaStudent:
Static Variables: START_DATE
STOP_DATE
howMany
Static Methods: printHowMany()
Object Variables: name
age
Object Methods: pr()
getAge()
getName()
setAge()
setName()

The Cat Class: An Example:
class Cat {
    int r;
    static int m;
    void pr() {
        System.out.println("i am pr");
    static void zx() {
        System.out.println("this is zx");
    Cat() {
        cat.m++;
    }
}

In this example, the "Cat()" is the 'constructor method', and is run when a new Cat object is created. You can tell it is a constructor by the fact that it starts with a capital letter and it's lack of a return type. IF A RETURN TYPE IS SPECIFIED, IT WILL NOT FUNCTION AS A CONSTRUCTOR.

The Pima Class
The class Pima contains a static method sleep which pauses a program for a specified number of milliseconds.
example:
Pima.sleep(3000);

The Pima Class has a parsing function to convert strings to integers or doubles.
Pima.parseInt(x) - Parses x into an Integer
Pima.parseDouble(x) - Parses x into a Double

Exceptions:
When something unusual happens, Java "throws" an exception. An exception is a collection of data on what happened. Normally, the system "catches" the exception and the program quites. If we want to catch the exception ourselves, we construct a try-catch block.
example:
try {
    statements that might cause an exception...
} catch(Exception e) {
    System.out.println(e);
    e.printStackTrace();
}


If no exceptions are thrown, the catch is ignored and passed over. If an exception is thrown, the statements in the catch method are run.

Using the Abstract Windows Toolkit:
To import the AWT, use the import java.awt.*; statement at the beginning of your java code.

The class Toolkit contains a static method getDefaultToolkit() which returns a Toolkit object.
example:
Toolkit tk;
tk = Toolkit.getDefaultToolkit();
tk.beep();
int r=tk.getScreenResolution();
Dimension d=tk.getScreenSize();



[ return ]