5.5  Example: Computing the total size of all files in a list

1. Data Analysis and Problem Analysis

The only data we need is the list of files, because each file know its size.

2. Purpose and Contract/Header:

  // PURPOSE AND CONTRACT:
  // compute the total size of all files in this list
  abstract int totalSize();

We develop the methods in the two classes concurrently, starting with examples.

3. Examples:

  File f1 = new File("MyTrip", 1000);
  File f2 = new File("hmwk1", 200);
  File f3 = new File("hmwk3", 350);
  ALoF mtlof = new MtLoF();
  ALoF lof1  = new ConsLoF(f1, mtlof);
  ALoF lof2  = new ConsLoF(f2, lof1);
  ALoF lof3  = new ConsLoF(f3, lof2);
  ...
  mtlof.totalSize()  -- expected: 0
  lof1.totalSize()   -- expected: 1000
  lof2.totalSize()   -- expected: 1200
  lof3.totalSize()   -- expected: 1550

4. Template:

There is a separate template for each of the two subclasses.

  /* TEMPLATE for the MtLoF class:
  int totalSize() {
    ...  }  
  */

 /* TEMPLATE for the ConsLoF class:
  int totalSize() {
    ... this.fst ...
        ... this.fst.name ...
        ... this.fst.size ...
    ... this.rst.totalSize() ...
  }  
  */

5. Program:

Again, we have two different methods, one for each class.

  // PROGRAM for the class MtLoF: 
  int totalSize() {
    return 0; }  

  // PROGRAM for the class ConsLoF: 
  int totalSize() {
    return this.fst.size + this.rst.totalSize(); }  

6. Tests:

  mtlof.totalSize()  == 0
  lof1.totalSize()   == 1000
  lof2.totalSize()   == 1200
  lof3.totalSize()   == 1550