Java Lecture 2

[Previous Lecture] [Lecture Index] [Next Lecture]

Documentation & Naming Convention

Java API Documentation Java naming convention:

How Javac Finds Classes

Abc.java                 Xyz.java
class Abc {              class Xyz {
  public Abc() {           public Xyz(int i) {
    // ...                   // ...
    aVar = new Xyz(3);     }
  }                        // ...
  // ...                 }
  private Xyz aVar;
}
When Abc.java is compiled, Xyz is assumed to be a class. To find class Xyz, javac:
  1. looks inside Abc.java
  2. looks in . for Xyz.java or Xyz.class (compiles Xyz.java if newer than Xyz.class)
  3. looks in system directories for Xyz.class
Above is an approximation; really does:
  1. search each package for Xyz (including the unnamed package)
  2. for each package, looks for the file packageName/ClassName.[class|java] (dots in packageName are changed to /)
  3. CLASSPATH environment variable lists directories to search (by default, CLASSPATH contains . and system directories)

Notes on Exceptions

try {
    statements
} catch (exception-type1  variable-name) {
    statements
} catch (exception-type2  variable-name) {
    statements
} finally {
    statements
}

Sum.java

class Sum {
   public static void main(String av[]) {
      int i;
      int total = 0;

      for (i = 0; i < av.length; i++) {
          try { total += Integer.parseInt(av[i]);
          } catch (NumberFormatException e) {
              System.out.println(av[i] + " is not an integer");
              return;
          }
      }
      System.out.println("Sum of arguments: " + total);
   }
}

Classes for Scalar Types

All nn-object types have a Object counterpart

Numbers:
    Float, Double, Integer, Byte, Short, Long

Character:


Vector class


VectorTest.java

import java.util.Vector;

class VectorTest {
  public static void main(String args[]) {
      Vector v = new Vector();

      v.addElement(new MyPoint(1, 2));
      v.addElement(new MyPoint(2, 4));
      v.addElement(new MyPoint(-2, 3));

      for (int i = 0; i < v.size(); i++) {
	      MyPoint p = (MyPoint) v.elementAt(i);
	      p.moveBy(4, 4);
      }

      for (int i = 0; i < v.size(); i++)
	      System.out.println(v.elementAt(i));
  }
}

Intro to Interfaces

[ public ] [ abstract ]
interface name [ extends type [,...]] {
    methods...
    constants...
}

ArrayEnumeration.java

import java.util.Enumeration;
import java.util.NoSuchElementException;

class ArrayEnumeration implements Enumeration {
    public ArrayEnumeration(Object a[]) {
        array = a;
    }
    public boolean hasMoreElements() {
        return pos < array.length;
    }
    public Object nextElement() 
        throws NoSuchElementException
    {
        if (pos >= array.length)
            throw new NoSuchElementException();
        return array[pos++];
    }
    private Object array[];
    private int pos = 0;
}

EnumTest.java

import java.util.*;

class EnumTest {
    public static void main(String args[]) {
        System.out.println("Arguments:");
        printEnum(new ArrayEnumeration(args));

        System.out.println("Integers:");
        Integer iArray[] = new Integer[10];
        for (int i = 0; i < iArray.length; i++)
            iArray[i] = new Integer(i * 10);
        printEnum(new ArrayEnumeration(iArray));

        System.out.println("Doubles:");
        Vector v = new Vector();
        for (int i = 1; i < 20; i++)
            v.addElement(new Double(1.0 / i));
        printEnum(v.elements());
    }
    private static void printEnum(Enumeration e)
    {
        int i = 0;
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            System.out.println("\t" + i + ": " + o);
            i++;
        }
    }
}

[Previous Lecture] [Lecture Index] [Next Lecture]