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
An IntSList is either
abstract
class IntSList {}
class Empty extends IntSList {
}
class Cons extends IntSList {
int first;
IntSList rest;
}
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;
}
}
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() + ")";
}
}
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
/**
* 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 ();
}
/**
* 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;
}
}
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);
}
}
/**
* 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.
}
}