/* --- CSU213 Fall 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 7: September 20, 2006 Lecture 8: September 21, 2006 Methods for Unions with Containment: Self-Referential Data Managing ITunes --------------- */ /* +--------------------------------+ | ISongs |<-+ +--------------------------------+ | | int count() | | | boolean contains(String title) | | | ISongs allBy(String artist) | | | ISongs sort() | | | ISongs insert(Song song) | | +--------------------------------+ | | | / \ | --- | | | ------------------ | | | | +---------+ +-------------+ | | MTSongs | | ConsSongs | | +---------+ +-------------+ | +---------+ +-| Song first | | | | ISongs rest |-+ | | +-------------+ | | | | | v +--=---+ +---------------+ | Song | +---------------+ | String title | | String artist | | int size | | double price | +---------------+ */ // to represent a list of songs in an ITunes collection interface ISongs { // count the number of songs in this list int count(); // does this list contain a song with the given title boolean contains(String title); // produce a list of all songs in this list that are by the given artist ISongs allBy(String artist); // produce from this list a list sorted by the size of the files ISongs sort(); // insert into this sorted list the given song (by the size of the files) ISongs insert(Song song); } // to represent an empty list of songs class MTSongs implements ISongs { MTSongs() {} // count the number of songs in this list int count() { return 0; } // does this list contain a song with the given title boolean contains(String title){ return false; } // produce a list of all songs in this list that are by the given artist ISongs allBy(String artist){ return this; } // produce from this list a list sorted by the size of the files ISongs sort(){ return this; } // insert into this sorted list the given song (by the size of the files) ISongs insert(Song song){ return new ConsSongs(song, this); } } // to represent a nonempty list of songs class ConsSongs implements ISongs { Song first; ISongs rest; ConsSongs(Song first, ISongs rest) { this.first = first; this.rest = rest; } /* TEMPLATE: ... this.first ... -- Song ... this.first.title ... -- String ... this.first.artist ... -- String ... this.first.size ... -- int ... this.first.price ... -- double ... this.rest ... -- ISongs ... this.rest.count() ... -- int ... this.rest.contains(Song title) ... -- boolean ... this.rest.allBy(String artist) ... -- ISongs ... this.rest.sort() ... -- ISongs (sorted) ... this.rest.insert(Song song) ... -- ISongs (sorted) */ // count the number of songs in this list int count() { return 1 + this.rest.count(); } // does this list contain a song with the given title boolean contains(String title){ if (this.first.title.equals(title)) return true; else return this.rest.contains(title); } // produce a list of all songs in this list that are by the given artist ISongs allBy(String artist){ if (this.first.artist.equals(artist)) return new ConsSongs(this.first, this.rest.allBy(artist)); else return this.rest.allBy(artist); } // produce from this list a list sorted by the size of the files ISongs sort(){ return this.rest.sort().insert(this.first); } // insert into this sorted list the given song (by the size of the files) ISongs insert(Song song){ if (song.size <= this.first.size) return new ConsSongs(song, this); else return new ConsSongs(this.first, this.rest.insert(song)); } } /* ;; A Song is (make-song String String Number Number) (define-struct song (Title Artist size price)) ;; Examples: (define help (make-song "Help" "Beatles" 3178852 0.99)) (define hc (make-song "Hotel California" "Eagles" 4405515 0.99)) */ /* +---------------+ | Song | +---------------+ | String title | | String artist | | int size | | double price | +---------------+ */ // to represent an ITunes song recording 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; } } class ExamplesSongs { ExamplesSongs(){} Song help = new Song("Help", "Beatles", 3178852, 0.99); Song hcal = new Song("Hotel California", "Eagles", 4405515, 0.99); Song mbnz = new Song("Mercedes Benz", "Janis", 4551534, 0.99); Song bmcg = new Song("Bobby McGee", "Janis", 4234975, 0.99); Song midf = new Song("Midnight Fire", "Eagles", 3426515, 0.99); ISongs mtsongs = new MTSongs(); ISongs songs1 = new ConsSongs(this.help, this.mtsongs); ISongs songs2 = new ConsSongs(this.hcal, this.songs1); ISongs songs3 = new ConsSongs(this.mbnz, this.songs2); ISongs songs4 = new ConsSongs(this.bmcg, this.songs3); ISongs songs5 = new ConsSongs(this.midf, this.songs4); ISongs songs5janis = new ConsSongs(this.bmcg, new ConsSongs(this.mbnz, this.mtsongs)); ISongs songs2sorted = new ConsSongs(this.help, new ConsSongs(this.hcal, this.mtsongs)); ISongs songs3sorted = new ConsSongs(this.help, new ConsSongs(this.hcal, new ConsSongs(this.mbnz, this.mtsongs))); ISongs songs5sorted = new ConsSongs(this.help, new ConsSongs(this.midf, new ConsSongs(this.bmcg, new ConsSongs(this.hcal, new ConsSongs(this.mbnz, this.mtsongs))))); ISongs songs4sorted = new ConsSongs(this.help, new ConsSongs(this.bmcg, new ConsSongs(this.hcal, new ConsSongs(this.mbnz, this.mtsongs)))); // test for the method count boolean testCount = (check this.mtsongs.count() expect 0) && (check this.songs1.count() expect 1) && (check this.songs5.count() expect 5); // test for the method contains boolean testContains = (check this.mtsongs.contains("Help") expect false) && (check this.songs1.contains("Help") expect true) && (check this.songs1.contains("Bobby McGee") expect false) && (check this.songs4.contains("Midnight Fire") expect false) && (check this.songs5.contains("Bobby McGee") expect true); // test for the method allBy boolean testAllBy = (check this.mtsongs.allBy("Janis") expect this.mtsongs) && (check this.songs2.allBy("Janis") expect this.mtsongs) && (check this.songs5.allBy("Janis") expect this.songs5janis); // test for the method sort boolean testSort = (check this.mtsongs.sort() expect this.mtsongs) && (check this.songs2.sort() expect this.songs2sorted) && (check this.songs5.sort() expect this.songs5sorted); // test for the method insert boolean testInsert = (check this.mtsongs.insert(this.help) expect this.songs1) && (check this.songs2sorted.insert(this.mbnz) expect this.songs3sorted) && (check this.songs4sorted.insert(this.midf) expect this.songs5sorted); }