Subject: Code from Class, 2005-02-02 and 2005-02-03
From: John B. Clements <[email-masked]>
Date: Thu, 3 Feb 2005 22:18:10 +0000 (UTC)
Newsgroups: ccs.courses.csu213

2005-02-02 code:

/*
         +-------------+             
         | IntegerList |<-----------+
         +-------------+            |
         +-------------+            |
               / \                  |
               ---                  |
                |                   |
    ------------------              |
    |                |              |
  +----+    +------------------+    |
  | MT |    | ConsList         |    |
  +----+    +------------------+    |
  +----+    | MyInteger first  |    |
            | IntegerList rest |-+  |
            +------------------+ |  |
                                 |  |
                                 +--+

*/

// represent a list of integers
abstract class IntegerList {

// represent a list of integers
abstract class IntegerList {
  abstract boolean same(IntegerList other);
  boolean sameMT(MT other) { return false; }
  boolean sameConsList(ConsList other) { return false; }

  abstract IntegerList insert(MyInteger x);
}

// represents an empty list of integers
class MT extends IntegerList {

  MT() {
  }
  boolean same(IntegerList other) { return other.sameMT(this); }
  boolean sameMT(MT other) {
    return true;
  }

  IntegerList insert(MyInteger x){
    return new ConsList(x,new MT());
  }
}

// represents a nonempty list of integers
class ConsList extends IntegerList {
  MyInteger first;
  IntegerList rest;

  ConsList(MyInteger first, IntegerList rest) {
    this.first = first;

  ConsList(MyInteger first, IntegerList rest) {
    this.first = first;
    this.rest = rest;
  }
  boolean same(IntegerList other) { return other.sameConsList(this); }
  boolean sameConsList(ConsList other) {
    return (this.first.same(other.first)) &&
           (this.rest.same(other.rest)) &&
           true;
  }

  IntegerList insert(MyInteger x){
    if (this.first.n <= x.n) 
      return new ConsList(this.first,this.rest.insert(x));
    else
      return new ConsList(x,this);
  }
}

class MyInteger{
  int n;
  MyInteger(int n){
    this.n = n;
  }
  boolean same(MyInteger other){
    return this.n == other.n;
  }
}

}

class Examples{
  Examples(){}

  MyInteger i3 = new MyInteger(3);
  MyInteger i6 = new MyInteger(6);
  MyInteger i8 = new MyInteger(8);
  MyInteger i4 = new MyInteger(4);
  MyInteger i2 = new MyInteger(2);

  IntegerList l0 = new MT();
  IntegerList l2 = new ConsList(this.i8,new ConsList (this.i6,
                        new ConsList(this.i3,new MT())));
  IntegerList l1_with_extra_six = new ConsList(this.i3,
                new ConsList (this.i6, new ConsList(this.i6,
                        new ConsList (this.i8, new MT()))));
  IntegerList l1 = new ConsList(this.i3,
                new ConsList(this.i6,
                        new ConsList (this.i8, new MT())));

  //boolean test1 = (this.l2.sort().same(this.l1));

  boolean test2 = (this.l1.insert(this.i6).same(this.l1_with_extra_six));

  
}