The only data we need is the list of files, because each file know its size.
// 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.
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
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() ... } */
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(); }
mtlof.totalSize() == 0 lof1.totalSize() == 1000 lof2.totalSize() == 1200 lof3.totalSize() == 1550