// CS U213 Spring 2007 // Lecture 7: January 23, 2007 /* +--------+ | LoS |<-------------------+ +--------+ | | | / \ | --- | | | +------+---------------+ | | | | +-----+-----+ | | | MtLoS | +--------+-----+ | +-----------+ | ConsLoS | | +--------------+ | +---------| Song first | | | | Los rest |--+ v +--------------+ +----------------+ | Song | +----------------+ | String title | | String artist | | int size | | double price | +----------------+ */ class Song{ String title; String artist; int size; double price; Song(String title, String artist, int size, double price){ this.title = title; this.artist = artist; this.size = size; this.price = price; } } /* Examples * Song help = new Song("Help", "Beatles", 50, 0.99); Song dsotm = new Song("Dsotm", "Pink Floyd", 100, 0.25); Song hts = new Song("Hts", "Muse", 10, 0.90); LoS mtlos = new MtLoS(); LoS list1 = new ConsLoS(this.help, this.mtlos); LoS list2 = new ConsLoS(this.dsotm, new ConsLoS( this.hts, this.list1)); */ /** Template: we update the ConsLoS constructor to include the 'running' template allong with our new method, 'count' // ... this.first ... --- Song // ... this.first.title ... --- String // ... this.first.artist ... --- String // ... this.first.size ... --- int // ... this.first.price ... --- double // ... this.rest ... --- LoS // ... this.rest.count() ... --- int * Implementation... we update each of the definitions to include the new method... */ interface LoS{ // Count the number of songs in *this* LoS int count(); // Find the total size of all the songs in *this* LoS int totalSize(); // Tell if there is a song by the given artist in *this* LoS boolean contains(String artist); // Create a list of all songs by the given artist in *this* LoS LoS allBy(String artist); } class MtLoS implements LoS{ MtLoS(){} // Count the number of songs in *this* empty LoS int count(){ return 0; } // Find the total size of all the songs in *this* empty LoS int totalSize(){ return 0; } // Tell if there is a song by the given artist in *this* empty LoS boolean contains(String artist){ return false; } // Create a list of all songs by the given artist in *this* empty LoS LoS allBy(String artist){ return this; } } class ConsLoS implements LoS{ Song first; LoS rest; ConsLoS(Song first, LoS rest){ this.first = first; this.rest = rest; } // Count the number of songs in *this* non-empty LoS int count(){ return 1 + this.rest.count(); } // Find the total size of all the songs in *this* non-empty LoS int totalSize(){ return this.first.size + this.rest.totalSize(); } // Tell if there is a song by the given artist in *this* non-empty LoS boolean contains(String artist){ return this.first.artist.equals(artist) || this.rest.contains(artist); } // Create a list of all songs by the given artist in *this* non-empty LoS LoS allBy(String artist){ if(this.first.artist.equals(artist)) return new ConsLoS(this.first, this.rest.allBy(artist)); else return this.rest.allBy(artist); } } class Examples{ Examples(){} Song help = new Song("Help", "Beatles", 50, 0.99); Song dsotm = new Song("Dsotm", "Pink Floyd", 100, 0.25); Song hts = new Song("Hts", "Muse", 10, 0.90); LoS mtlos = new MtLoS(); LoS list1 = new ConsLoS(this.help, this.mtlos); LoS list2 = new ConsLoS(this.dsotm, new ConsLoS( this.hts, this.list1)); // test the method count in classes that implement LoS boolean testCount(){ return (check this.mtlos.count() expect 0) && (check this.list1.count() expect 1) && (check this.list2.count() expect 3);} // test the method totalSize in classes that implement LoS boolean testTotalSize(){ return (check this.mtlos.totalSize() expect 0) && (check this.list1.totalSize() expect 50) && (check this.list2.totalSize() expect 160);} // test the method contains in classes that implement LoS boolean testContains(){ return (check this.mtlos.contains("Beatles") expect false) && (check this.list1.contains("Beatles") expect true) && (check this.list1.contains("Muse") expect false) && (check this.list2.contains("Muse") expect true) && (check this.list2.contains("ABC") expect false);} // test the method allBy in classes that implement LoS boolean testAlllBy(){ return (check this.mtlos.allBy("Pink Floyd") expect this.mtlos) && (check this.list1.allBy("Pink Floyd") expect this.mtlos) && (check this.list1.allBy("Beatles") expect this.list1) && (check this.list2.allBy("Pink Floyd") expect new ConsLoS(this.dsotm, this.mtlos)) && (check this.list2.allBy("Beatles") expect this.list1);} }