Java Programming
CIS279
Tori Basford
tori@bly.net
Assignment(s):
None
Exercise(s):
Variables:
To acess a local variable:
| 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();