Unix Primer

Dennis Lu will give a lecture on unix and emacs this Wednesday (09/15/99) from 8:30 PM-9:30 PM in Ryon 102. Check Comp212 home page for latest announcement.

Java Syntax and OOP Concepts Review

I will give a review lecture on EVERYTHING we have learned about Java and OOP this Friday (09/17/99) from ?? PM-?? PM in Duncan Hall ???. Check Comp212 home page for latest announcement.

Reading Assignment for this week

Java Programming Language (JPL): 1.6.2, 1.7.3, 1.8, 1.12-1.14, 2.9, 3

Notes on OO Program Design (Sept 7, 99 version), by Dr. Cartwright: 1.1-1.6 (review reading), 1.10, 1.11

Java Tools

DrJava: a tutorial will be given soon. Check Comp212 home page for latest announcement.

JBuilder 3 Univeristy Edition: a FREE Integrated Development Environment (IDE) for Windows platform only from www.inprise.com/programs/education/

NetBeans: a free cross-plaform IDE download version at www.netbeans.com.

StructureBuilder: drawsclass diagrams and generates stub codes, free evaluation version from www.tendril.com.

Feedbacks

We hear you and please keep them coming.

Read, start early, do one thing at a time, ask questions,

Lab 1 Review

  1. Compile an existing Java program: javac Pizza.java
  2. Run the compiled program (i.e. Pizza.class file): java Pizza
  3. Edit an existing Java program.
  4. Learn some debugging on syntax errors (misspell and missing semi-colon).
  5. Translating a Scheme data definition (Comp 210) of an Integer Scheme List (IntSList) into a Java class design (IntSList1.java)

An IntSList is either

abstract class IntSList {

}

class Empty extends IntSList {

}

class Cons extends IntSList {

int first;

IntSList rest;

}

  1. Add "getter" methods to class Cons and a main method to class IntSList to print a list using the inherited toString() (IntSList2.java).
  2. abstract class IntSList {

    // main is a great place to put test cases:

    //

    public static void main( String[] args ) {

    IntSList l0, l1, l2, l3;

    l0 = new Empty();

    l1 = new Cons( 99, l0 );

    l2 = new Cons( 5, new Cons ( 6, new Empty() ));

    l3 = new Cons( 4, l2 );

    System.out.println( "l0 is " + l0.toString() );

    System.out.println( "l1 is " + l1.toString() );

    System.out.println( "l2 is " + l2.toString() );

    System.out.println( "l3 is " + l3.toString() );

    }

    }

    class Empty extends IntSList {

    // We don't actually need the zero-argument constructor,

    // since Java provides a default which initializes all zero of

    // our fields to null. But if we do define this ourselves, it's easy:

    Empty() {}

    }

    class Cons extends IntSList {

    int first;

    IntSList rest;

    Cons( int car, IntSList cdr ) {

    first = car;

    rest = cdr;

    }

    }

  3. Override the toString() method to have more meaningful output (IntSList3.java)
  4. abstract class IntSList {

    public abstract String toString();

    // main is a great place to put test cases:

    public static void main( String[] args ) {

    IntSList l0, l1, l2, l3;

    l0 = new Empty();

    l1 = new Cons( 99, l0 );

    l2 = new Cons( 5, new Cons ( 6, new Empty() ));

    l3 = new Cons( 4, l2 );

    System.out.println( "l0 is " + l0.toString() );

    System.out.println( "l1 is " + l1.toString() );

    System.out.println( "l2 is " + l2.toString() );

    System.out.println( "l3 is " + l3.toString() );

    }

    }

    class Empty extends IntSList {

    // We don't actually need the zero-argument constructor,

    // since Java provides a default which initializes all zero of

    // our fields to null. But if we do define this ourselves, it's easy:

    Empty() {}

    public String toString() {

    // Easy:

    return "()";

    }

    }

    class Cons extends IntSList {

    int first;

    IntSList rest;

    Cons( int car, IntSList cdr ) {

    first = car;

    rest = cdr;

    }

    public String toString() {

    return "(" + first + ", " + rest.toString() + ")";

    }

    }

  5. Modify the toString() method (previously written) to make better output, using a "helper function". The concept of "helper functions" is pervasive in Scheme programming and carries over into OOP (IntSList4.java).

abstract class IntSList {

// toStringHelp: return a string version of the list

// which contains no parens, just comma-separated items:

//

abstract protected String toStringHelp();

public String toString() {

return "(" + toStringHelp() + ")";

}

// main is a great place to put test cases:

public static void main( String[] args ) {

IntSList l0, l1, l2, l3;

l0 = new Empty();

l1 = new Cons( 99, l0 );

l2 = new Cons( 5, new Cons ( 6, new Empty() ));

l3 = new Cons( 4, l2 );

System.out.println( "l0 is " + l0.toString() );

System.out.println( "l1 is " + l1.toString() );

System.out.println( "l2 is " + l2.toString() );

System.out.println( "l3 is " + l3.toString() );

}

}

class Empty extends IntSList {

Empty() {}

protected String toStringHelp() {

return "";

}

}

class Cons extends IntSList {

int first;

IntSList rest;

Cons( int car, IntSList cdr ) {

first = car;

rest = cdr;

}

protected String toStringHelp() {

return " " + first + rest.toStringHelp();

}

}

Lab 2 Review

  1. Java documentation style: add comments to existing files.
  2. /**

    * Mimics functional lists.

    * @author DXN

    */

    public abstract class FunList {

    public abstract int car();

    public abstract FunList cdr();

    /**

    * NOTE: toString () method is NOT abstract. It calls, toStringHelp (), an abstract method.

    * It represents what we call an "invariant" behavior for <code>FunList</code>.

    * It is an example of the "Template Method Pattern". A "template method" is a method that

    * makes calls to at least one abstract method in its own class.

    */

    public String toString() {

    return "(" + toStringHelp() + " )";

    }

    abstract String toStringHelp();

    public abstract FunList append (FunList other);

    /**

    * @param i an integer to be inserted in order into this <code>FunList</code>.

    * <p>Pre-condition: this <code>FunList</code> is sorted in non-descending order.</p>

    */

    public abstract FunList insertInOrder (int i);

    public abstract FunList sort ();

    }

  3. Run the javadoc utility that comes with the Java Development Kit (JDK) to generate html documentation for your commented code files: javadoc *.java. No knowledge of html formatting is needed, but knowing a little bit about it will help make the comments' formats look better. For example, <b>stuffs</b> will be formatted as stuffs.
  4. Run javadoc -private *.java to generate comments for private fields and methods.
  5. Lear to read the UML class diagram for the Singleton pattern and implement it in Java, using private static field and a public method. Apply the Singleton pattern to class Empty.
  6. /**

    * Singleton pattern for the empty list.

    *

    */

    public class Empty extends FunList

    {

    private static Empty _instance; // == null, initially.

    /**

    * Accessible only from within this class. Body is blank because there is nothing to initialize.

    */

    private Empty ()

    {

    }

    public static Empty UniqueInstance ()

    {

    if (null == _instance)

    {

    _instance = new Empty ();

    }

    return _instance; // static methods can access static fields.

    }

    public int car ()

    {

    throw new java.util.NoSuchElementException("car requires a non Empty Funlist");

    }

    public FunList cdr ()

    {

    throw new java.util.NoSuchElementException("cdr requires a non Empty Funlist");

    }

    String toStringHelp ()

    {

    return "";

    }

    /**

    * When a list L is appended to the empty list, the result is just L itself.

    */

    public FunList append (FunList other)

    {

    return other;

    }

    /**

    * When i inserted in order into an empty list, the result is just <code>Cons (i)</code>.

    * <p>Pre-condition: this <code>FunList</code> is sorted in non-descending order.</p>

    */

    public FunList insertInOrder (int i)

    {

    return new Cons (i);

    }

    /**

    * The empty list is sorted by the laws of logic.

    */

    public FunList sort ()

    {

    return this;

    }

    }

  7. Write more code to make use of the singleton Empty and to practice more on recursion.
  8. public class Cons extends FunList

    {

    private int _dat;

    private FunList _cdr;

    public Cons( int carDat, FunList cdr )

    {

    _dat = carDat;

    _cdr = cdr;

    }

    public Cons (int i)

    {

    _dat = i;

    _cdr = Empty.UniqueInstance ();

    }

    public int car()

    {

    return _dat;

    }

    public FunList cdr()

    {

    return _cdr;

    }

    String toStringHelp()

    {

    return " " + _dat + _cdr.toStringHelp();

    }

    public FunList append (FunList other)

    {

    return new Cons (_dat, _cdr.append (other));

    }

    /**

    * Pre-condition: this <code>FunList</code> is sorted in non-descending order.

    */

    public FunList insertInOrder (int i)

    {

    if (i <= _dat)

    {

    return new Cons (i, this);

    }

    else

    {

    return new Cons (_dat, _cdr.insertInOrder (i));

    }

    }

    /**

    * Asks <code>cdr</code> to sort then asks the result to insert <code>_dat</code> in order.

    */

    public FunList sort ()

    {

    return _cdr.sort ().insertInOrder (_dat);

    }

    }

  9. Java-specific implementation of the Singleton pattern.

/**

* Singleton pattern in Java.

*

*/

public class SingletonClass

{

private static SingletonClass _instance = new SingletonClass ();

/**

* Accessible only from within this class. Body is blank because there is nothing to initialize.

*/

private SingletonClass ()

{

}

public static SingletonClass UniqueInstance ()

{

return _instance; // static methods can access static fields.

}

}