3.3  Example: Data definitions for a list of files

A File consists of a name (String) and size (int).

A List of Files is one of

// to represent one file in a computer system
class File {
  String name;
  int size;

  File(String name, int size) {
    this.name = name;
    this.size = size;
  }
}

/* Interactions:
Examples of the use of the constructor for the class File

File f1 = new File("MyTrip", 1000);
File f2 = new File("hmwk1", 200);
File f3 = new File("hmwk3", 350);

/*
          +------+              
          | ALoF |<-------------+
          +------+              |
          +------+              |
             / \                |
             ---                |
              |                 |
      ----------------          |
      |              |          |
  +-------+     +----------+    |
  | MtLoF |     | ConsLoF  |    |
  +-------+     +----------+    |    +-------------+
  +-------+     | File fst |----+--->| File        |
                | ALoF rst |----+    +-------------+
                +----------+         | String name |
                                     | int size    |
                                     +-------------+
*/

// to represent a list of files
abstract class ALoF {
}

// to represent an empty list of files
class MtLoF extends ALoF {

  MtLoF() {
  }
}

// to represent a constructed list of files
class ConsLoF extends ALoF {
  File fst;
  ALoF rst;

  ConsLoF(File fst, ALoF rst) {
    this.fst = fst;
    this.rst = rst;
  }
}

/* Interactions:
Examples of the use of the constructors for the subclasses of ALoF

MtLoF mtlof  = new MtLoF();
ConsLoF lof1  = new ConsLoF(f1, mtlof);
ConsLoF lof2  = new ConsLoF(f2, lof1);
ConsLoF lof3  = new ConsLoF(f3, lof2);

Examples of the use of the selectors for the subclasses of ALoF
lof1.fst    -- expected: f1
lof1.rst    -- expected: mtlof
lof2.fst    -- expected: f2
lof2.rst    -- expected: lof1
lof3.fst    -- expected: f3
lof3.rst    -- expected: lof2
*/