Lab Assignment 05
Course Code: CSE111
Course Title: Programming Language II
Topic: Multi-class Design
Number of Tasks: 10
[You are not allowed to change the driver codes of any of the tasks]
After YouTube Music, Spotify has decided to redesign their Playlist system. However, they
decided to not use arrays to store their music, instead, they will use OOP concepts to create
the new Playlist system. You have been assigned to build the system by using 3 classes (Song,
Playlist, and SpotifyTester).
Each song will have the name of the song, artist name, length of the song in minutes and the
next song. Each playlist will have a name and it can contain multiple songs. Both classes will
have some features which will be demonstrated in each task.
[You are not allowed to use Array or any built-in libraries for this assignment]
Task 1
Design the Song class with constructor and songInfo() method along with necessary attributes
in such a way that it produces the following output.
Driver Code Output
public class SpotifyTester { 1===========
public static void main(String[] args) { Title: Song-A
Song s1 = new Song("Song-A", "Artist-A", 3); Artist: Artist-A
Length: 3 minutes
System.out.println("1===========");
2===========
s1.songInfo();
System.out.println("2===========");
// More lines will be added in this Tester class
Task 2
Design the Playlist class constructor along with necessary attributes in such a way that it
produces the following output.
Driver Code Output
System.out.println("2==========="); 2===========
// Continuation from Task 1 First Playlist created.
Playlist p1 = new Playlist("First Playlist"); 3===========
System.out.println("3===========");
Task 3 & 4
Create addSong() method and info() method inside the Playlist class to produce the following
output.
Driver Code Output
System.out.println("3==========="); 3===========
// Continuation from Task 2 First Playlist has the following
p1.info(); songs:
No songs in First Playlist.
System.out.println("4===========");
4===========
p1.addSong(s1); Song-A added to First Playlist.
System.out.println("5==========="); 5===========
p1.info(); First Playlist has the following
System.out.println("6==========="); songs:
Song-1
Song s2 = new Song("Song-B", "Artist-B", 4);
Title: Song-A
Song s3 = new Song("Song-C", "Artist-C", 2); Artist: Artist-A
p1.addSong(s2); Length: 3 minutes
p1.addSong(s3); 6===========
System.out.println("7==========="); Song-B added to First Playlist.
p1.info(); Song-C added to First Playlist.
7===========
System.out.println("8===========");
First Playlist has the following
songs:
Song-1
Title: Song-A
Artist: Artist-A
Length: 3 minutes
Song-2
Title: Song-B
Artist: Artist-B
Length: 4 minutes
Song-3
Title: Song-C
Artist: Artist-C
Length: 2 minutes
8===========
Task 5
Create addSong() [overloaded] method inside the Playlist class to produce the following output.
Driver Code Output
System.out.println("8==========="); 8===========
// Continuation from Task 3&4 Song-D added to First Playlist.
Song s4 = new Song("Song-D", "Artist-D", 3); Song-E added to First Playlist.
Song-F added to First Playlist.
Song s5 = new Song("Song-E", "Artist-E", 4);
Cannot add song to Index 10.
Song s6 = new Song("Song-F", "Artist-F", 2); 9===========
Song s7 = new Song("Song-G", "Artist-G", 2); First Playlist has the following
p1.addSong(s4, 0); songs:
p1.addSong(s5, 2); Song-1
Title: Song-D
p1.addSong(s6, 5);
Artist: Artist-D
p1.addSong(s7, 10); Length: 3 minutes
System.out.println("9==========="); Song-2
p1.info(); Title: Song-A
System.out.println("10=========="); Artist: Artist-A
Length: 3 minutes
Song-3
Title: Song-E
Artist: Artist-E
Length: 4 minutes
Song-4
Title: Song-B
Artist: Artist-B
Length: 4 minutes
Song-5
Title: Song-C
Artist: Artist-C
Length: 2 minutes
Song-6
Title: Song-F
Artist: Artist-F
Length: 2 minutes
10==========
Task 6
Create playSong() method inside the Playlist class to produce the following output.
Driver Code Output
System.out.println("10=========="); 10==========
// Continuation from Task 5 Playing Song-F by Artist-F.
p1.playSong("Song-F"); Song-G not found in playlist First
Playlist.
p1.playSong("Song-G");
Playing Song-B by Artist-B.
p1.playSong("Song-B"); 11==========
System.out.println("11==========");
Task 7
Create the playSong() [overloaded] method inside the Playlist class to produce the following
output.
Driver Code Output
System.out.println("11=========="); 11==========
// Continuation from Task 6 Playing Song-D by Artist-D.
p1.playSong(0); Playing Song-C by Artist-C.
Song at Index 8 not found in First
p1.playSong(4);
Playlist.
p1.playSong(8); 12==========
System.out.println("12==========");
Task 8
Create the deleteSong() method inside the Playlist class to produce the following output.
Driver Code Output
System.out.println("12=========="); 12==========
// Continuation from Task 7 Song-D deleted from First Playlist.
p1.deleteSong("Song-D"); Song-B deleted from First Playlist.
Song-F deleted from First Playlist.
p1.deleteSong("Song-B");
Song-K not found in First Playlist.
p1.deleteSong("Song-F"); 13==========
p1.deleteSong("Song-K"); First Playlist has the following
System.out.println("13=========="); songs:
p1.info(); Song-1
Title: Song-A
System.out.println("14==========");
Artist: Artist-A
Length: 3 minutes
Song-2
Title: Song-E
Artist: Artist-E
Length: 4 minutes
Song-3
Title: Song-C
Artist: Artist-C
Length: 2 minutes
14==========
Task 9
Create the totalSong() method inside the Playlist class to produce the following output.
Driver Code Output
System.out.println("14=========="); 14==========
// Continuation from Task 8 First Playlist has 3 songs
System.out.println(p1.name +" has "+p1.totalSong() 15==========
+" songs");
System.out.println("15==========");
Task 10
Create the merge() method inside the Playlist class to produce the following output.
Driver Code Output
System.out.println("15=========="); 15==========
// Continuation from Task 9 16==========
Song ns1 = new Song("Song-Z", "Artist-Z", 3); Second Playlist created.
Song-Z added to Second Playlist.
Song ns2 = new Song("Song-Y", "Artist-Y", 4);
Song-Y added to Second Playlist.
Song ns3 = new Song("Song-X", "Artist-X", 2); Song-X added to Second Playlist.
System.out.println("16=========="); 17==========
Playlist p2 = new Playlist("Second Playlist"); First Playlist has the following
p2.addSong(ns1); songs:
Song-1
p2.addSong(ns2);
Title: Song-A
p2.addSong(ns3); Artist: Artist-A
System.out.println("17=========="); Length: 3 minutes
p1.info(); Song-2
System.out.println("18=========="); Title: Song-E
p2.info(); Artist: Artist-E
Length: 4 minutes
System.out.println("19==========");
Song-3
p1.merge(p2); Title: Song-C
System.out.println("20=========="); Artist: Artist-C
p1.info(); Length: 2 minutes
System.out.println("21=========="); 18==========
Second Playlist has the following
songs:
Song-1
Title: Song-Z
Artist: Artist-Z
Length: 3 minutes
Song-2
Title: Song-Y
Artist: Artist-Y
Length: 4 minutes
Song-3
Title: Song-X
Artist: Artist-X
Length: 2 minutes
19==========
Merge Completed!
20==========
First Playlist has the following
songs:
Song-1
Title: Song-A
Artist: Artist-A
Length: 3 minutes
Song-2
Title: Song-E
Artist: Artist-E
Length: 4 minutes
Song-3
Title: Song-C
Artist: Artist-C
Length: 2 minutes
Song-4
Title: Song-Z
Artist: Artist-Z
Length: 3 minutes
Song-5
Title: Song-Y
Artist: Artist-Y
Length: 4 minutes
Song-6
Title: Song-X
Artist: Artist-X
Length: 2 minutes
21==========
Task 11 [Ungraded]
Create the showHistory() method inside the Playlist class to produce the following output.
[Hint: showHistory() only shows the songs which were played from the playlist. So you might
need to update the method which is used to play Songs.]
Driver Code Output
System.out.println("21=========="); 21==========
// Continuation from Task 10 History of First Playlist:
p1.showHistory(); Title: Song-F
Artist: Artist-F
System.out.println("22==========");
Length: 2 minutes
p2.showHistory(); Title: Song-B
System.out.println("23=========="); Artist: Artist-B
} Length: 4 minutes
} Title: Song-D
Artist: Artist-D
Length: 3 minutes
Title: Song-C
Artist: Artist-C
Length: 2 minutes
22==========
History of Second Playlist:
No songs were played from Second
Playlist.
23==========
Complete driver code and expected output:
Driver Code Output
public class SpotifyTester { 1===========
public static void main(String[] args) { Title: Song-A
Song s1 = new Song("Song-A", "Artist-A", 3); Artist: Artist-A
System.out.println("1==========="); Length: 3 minutes
s1.songInfo();
2===========
System.out.println("2===========");
Playlist p1 = new Playlist("First Playlist");
First Playlist created.
System.out.println("3==========="); 3===========
p1.info(); First Playlist has the following songs:
System.out.println("4==========="); No songs in First Playlist.
p1.addSong(s1); 4===========
System.out.println("5==========="); Song-A added to First Playlist.
p1.info(); 5===========
System.out.println("6===========");
First Playlist has the following songs:
Song s2 = new Song("Song-B", "Artist-B", 4);
Song-1
Song s3 = new Song("Song-C", "Artist-C", 2);
p1.addSong(s2); Title: Song-A
p1.addSong(s3); Artist: Artist-A
System.out.println("7==========="); Length: 3 minutes
p1.info(); 6===========
System.out.println("8==========="); Song-B added to First Playlist.
Song s4 = new Song("Song-D", "Artist-D", 3); Song-C added to First Playlist.
Song s5 = new Song("Song-E", "Artist-E", 4);
7===========
Song s6 = new Song("Song-F", "Artist-F", 2);
First Playlist has the following songs:
Song s7 = new Song("Song-G", "Artist-G", 2);
p1.addSong(s4, 0); Song-1
p1.addSong(s5, 2); Title: Song-A
p1.addSong(s6, 5); Artist: Artist-A
p1.addSong(s7, 10); Length: 3 minutes
System.out.println("9==========="); Song-2
p1.info(); Title: Song-B
System.out.println("10==========");
Artist: Artist-B
p1.playSong("Song-F");
Length: 4 minutes
p1.playSong("Song-G");
p1.playSong("Song-B"); Song-3
System.out.println("11=========="); Title: Song-C
p1.playSong(0); Artist: Artist-C
p1.playSong(4); Length: 2 minutes
p1.playSong(8); 8===========
System.out.println("12=========="); Song-D added to First Playlist.
p1.deleteSong("Song-D");
Song-E added to First Playlist.
p1.deleteSong("Song-B");
Song-F added to First Playlist.
p1.deleteSong("Song-F");
p1.deleteSong("Song-K"); Cannot add song to Index 10.
System.out.println("13=========="); 9===========
p1.info(); First Playlist has the following songs:
System.out.println("14=========="); Song-1
System.out.println(p1.name +" has "+ Title: Song-D
p1.totalSong() + " songs");
Artist: Artist-D
System.out.println("15==========");
Song ns1 = new Song("Song-Z", "Artist-Z", 3);
Length: 3 minutes
Song ns2 = new Song("Song-Y", "Artist-Y", 4); Song-2
Song ns3 = new Song("Song-X", "Artist-X", 2); Title: Song-A
System.out.println("16=========="); Artist: Artist-A
Playlist p2 = new Playlist("Second Playlist"); Length: 3 minutes
p2.addSong(ns1); Song-3
p2.addSong(ns2); Title: Song-E
p2.addSong(ns3);
Artist: Artist-E
System.out.println("17==========");
Length: 4 minutes
p1.info();
System.out.println("18=========="); Song-4
p2.info(); Title: Song-B
System.out.println("19=========="); Artist: Artist-B
p1.merge(p2); Length: 4 minutes
System.out.println("20=========="); Song-5
p1.info(); Title: Song-C
System.out.println("21==========");
Artist: Artist-C
Length: 2 minutes
//Ungraded Task
p1.showHistory(); Song-6
System.out.println("22=========="); Title: Song-F
p2.showHistory(); Artist: Artist-F
System.out.println("23=========="); Length: 2 minutes
} 10==========
} Playing Song-F by Artist-F.
Song-G not found in playlist First
Playlist.
Playing Song-B by Artist-B.
11==========
Playing Song-D by Artist-D.
Playing Song-C by Artist-C.
Song at Index 8 not found in First
Playlist.
12==========
Song-D deleted from First Playlist.
Song-B deleted from First Playlist.
Song-F deleted from First Playlist.
Song-K not found in First Playlist.
13==========
First Playlist has the following songs:
Song-1
Title: Song-A
Artist: Artist-A
Length: 3 minutes
Song-2
Title: Song-E
Artist: Artist-E
Length: 4 minutes
Song-3
Title: Song-C
Artist: Artist-C
Length: 2 minutes
14==========
First Playlist has 3 songs
15==========
16==========
Second Playlist created.
Song-Z added to Second Playlist.
Song-Y added to Second Playlist.
Song-X added to Second Playlist.
17==========
First Playlist has the following songs:
Song-1
Title: Song-A
Artist: Artist-A
Length: 3 minutes
Song-2
Title: Song-E
Artist: Artist-E
Length: 4 minutes
Song-3
Title: Song-C
Artist: Artist-C
Length: 2 minutes
18==========
Second Playlist has the following
songs:
Song-1
Title: Song-Z
Artist: Artist-Z
Length: 3 minutes
Song-2
Title: Song-Y
Artist: Artist-Y
Length: 4 minutes
Song-3
Title: Song-X
Artist: Artist-X
Length: 2 minutes
19==========
Merge Completed!
20==========
First Playlist has the following songs:
Song-1
Title: Song-A
Artist: Artist-A
Length: 3 minutes
Song-2
Title: Song-E
Artist: Artist-E
Length: 4 minutes
Song-3
Title: Song-C
Artist: Artist-C
Length: 2 minutes
Song-4
Title: Song-Z
Artist: Artist-Z
Length: 3 minutes
Song-5
Title: Song-Y
Artist: Artist-Y
Length: 4 minutes
Song-6
Title: Song-X
Artist: Artist-X
Length: 2 minutes
21==========
History of First Playlist:
Title: Song-F
Artist: Artist-F
Length: 2 minutes
Title: Song-B
Artist: Artist-B
Length: 4 minutes
Title: Song-D
Artist: Artist-D
Length: 3 minutes
Title: Song-C
Artist: Artist-C
Length: 2 minutes
22==========
History of Second Playlist:
No songs were played from Second
Playlist.
23==========