// This program defines and exercises a simple subclass of List, // in order to demonstrate some of the subtleties of working with // collections in general, and Lists in particular. The only thing // this class really provides to clients other than what it inherits from // List is... // // o printNumbered, a message that prints the items in a list // from first to last, numbering them as it does so. The // value with which to start numbering is the parameter to // this message. This message has no return value. // // However, this class, like all subclasses of List, needs to override // the makeNewList method so that extended lists grow by making new // extended lists. This class also provides constructors parallel to // those provided by List // History: // March 2004 -- Created by Doug Baldwin. import geneseo.cs.sc.List; public class ExtendedList extends List { // The constructors. These simply pass their parameters (if any) // on to the corresponding constructor for List. public ExtendedList() { super(); } public ExtendedList( Object item ) { super( item ); } // The printNumbered method. This is based on the recursive insight // that printing an empty list doesn't require doing anything, while // printing a non-empty list requires printing its head (with number n), // followed by printing the tail numbered from n+1. public void printNumbered( int n ) { if ( ! this.isEmpty() ) { System.out.println( n + ": " + this.getFirst() ); ((ExtendedList)this.getRest()).printNumbered( n + 1 ); } } // The makeNewList method. public List makeNewList() { return new ExtendedList(); } // The main method. This simply creates an extended list and tries // to print it. public static void main(String args[]) { ExtendedList testList = new ExtendedList(); testList.addItem( "World" ); testList.addItem( "Hello" ); /* testList.printNumbered( 1 ); */ System.out.println( (int)32.7 ); } }