Starting from:
$30

$24

CS Algorithms & Programming Lab 9 Solution


The objective of this lab is to learn how to use ArrayLists. When you need an array-like data structure that offers dynamic resizing, you would usually use an ArrayList. An ArrayList is an array that resizes itself as needed. Remember that analyzing your problems and designing them on a piece of paper before starting implementation/coding is always a best practice.

0. Setup Workspace

Start VSC and open the previously created workspace named labs_ws. Now, under the labs folder, create a new folder named lab9.

In this lab, you are to have three Java classes/files (under labs/lab9 folder) as described below. A fourth Java file containing the revision should go under this folder as well. We expect you to submit a total of 4 files including the revision. Do not upload other/previous lab solutions in your submission. Outputs of sample runs are shown as brown.

In this lab you are going to implement a playlist creator along with Song and Playlist classes.

Every year music streaming applications like Spotify create a playlist out of a user’s favorite songs played over the year. This year is no different!

You are going to create this year’s playlist using ArrayList, Song and Playlist objects. After you create the playlist, you are going to add the necessary methods to make it sortable by song’s duration.

1. Song Class

Create a new/empty file to define a class named Song. This class defines the details of a song in the application.

Instance Data Members:

    • title: The name of the song (String)

    • artist: Artist of the song (String)

    • album: The name of the album (String)

    • duration: Duration in seconds of the song (int)

    • timesPlayed: The number of times the song has been played (int)
Methods:

    • Constructor:

        ◦ Registers the song to the system.

        ◦ Takes title, artist, album, duration, and timesPlayed.

        ◦ Initializes the data members title, artist, album, duration, and timesPlayed.
    • Accessor methods:

        ◦ You need to implement methods to access the data variables title, artist, album, duration,timesPlayed of the song

    • Setter methods:

        ◦ setTitle: Sets the name of the song

        ◦ setArtist: Sets the Artist object of the song

        ◦ setAlbum: Changes the album name of the song

        ◦ setDuration: Changes the duration of the song

        ◦ setTimesPlayed: Changes the times the song has been played

    • compareTo method:

        ◦ should compare the two songs relative to their durations

        ◦ Song class must implement the Comparable interface (for this you need to learn about how a class implements an interface and how this will help us to sort objects easily). Learn more about the Comparable interface and compareTo method and how it compares two objects here

    • toString method

        ◦ Override the toString method to get an understandable string representation of a song object, where it shows song name, artist name, album name, duration, and times played together.

Sample run:


Song song = new Song("Test", "artist1", "Red", 347, 10000000); System.out.println(song);
*************************************************

Song Name: Test

Artist Name: artist1

Album Name: Red

Duration: 5:47

Times Played: 10000000




2. Playlist Class

Create a new/empty file to define a class named Playlist. This class defines the details of a playlist in the application.

Instance Data Members:

    • name: The name of the playlist (String)

    • creator: Creator of the playlist (String)

    • genre: Genre of the playlist (String)

    • songs: ArrayList of Song objects (ArrayList)

Methods:

    • An empty constructor:

        ◦ An empty constructor to be able to define a playlist object without parameters in the main method
    • Constructor:

        ◦ Registers the playlist to the system.

        ◦ Takes name, creator, genre,and songs.

        ◦ Initializes the data members name, creator, genre, and songs.

    • Accessor methods:

        ◦ You need to implement methods to access the data variables name, creator, genre, songs of the playlist

    • Setter methods:

        ◦ setName: Sets the name of the playlist

        ◦ setCreator: Sets the creator of the playlist

        ◦ setGenre: Changes the genre of the playlist

        ◦ setSongs: Changes the songs inside the playlist

    • Playlist methods:

        ◦ songExists : returns true if the given song exists in the playlist

        ◦ addSong : adds song to the playlist if the song doesn’t exist in the playlist

        ◦ removeSong : removes the song from the playlist

        ◦ sortByDurationDesc : After using the comparable interface in our Song class, we can now sort the songs according to the rules we defined. Sort the songs using Collections.
        ◦ toString: Override toString method to return an informative representation of playlist including song name, artist name, genre and song object.

Sample output for a Playlist object:


Song song1 = new Song("Test", "artist1", "Red", 600, 10000000); Song song2 = new Song("Test2", "artist2", "Red2", 106, 23555); System.out.println(song);
ArrayList<Song> songs = new ArrayList<Song>();

songs.add(song1);

songs.add(song2);

Playlist playlist = new Playlist("Red Season","Taylor Swift","Fall Songs",songs); System.out.println(playlist);
*************************************************

--------------------------------------------------------

Playlist Name: Red Season

Creator Name: Taylor Swift

Genre: Fall Songs

--------------------------------------------------------

Song Name: Test

Artist Name: artist1

Album Name: Red

Duration: 10:0

Times Played: 10000000

--------------------------------------------------------

Song Name: Test2

Artist Name: artist2

Album Name: Red2

Duration: 1:46

Times Played: 23555

--------------------------------------------------------



3. Main Class

Create a new/empty file to define a class named MusicApp. This class defines the menu where we will create our music app and interact with Song and Playlist objects to create our playlist. Create the music app where user will be able to:
    • create a new playlist,

    • display the recently created playlist,

    • sort the current playlist according to song durations in descending order,

    • remove a song based on their index from the recently created playlist,

    • display all playlists that are created

    • exit the application

You also will define an arraylist of genres, where users will be able to select the playlist genre in its respective part.

Sample run (Create a new Playlist):


--------------------------------------------------------

Welcome to Music App! Get Ready to create your playlist!

--------------------------------------------------------

1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

***************************

1

How many songs do you want to add to playlist?:

2

Enter artist name for Song 1:

Tarkan

Enter album name for Song 1:

Karma

Enter song name for Song 1:

Kuzu Kuzu

Enter song duration in seconds for Song 1:

233

Enter the times song has been played for Song 1:

29144161


Enter artist name for Song 2:

Depeche Mode

Enter album name for Song 2:

Violator

Enter song name for Song 2:

Personal Jesus

Enter song duration in seconds for Song 2:

294

Enter the times song has been played for Song 2:

28646612

Enter playlist name:

Mix

Enter creator of playlist:

John Doe

0-Country

1-Electronic

2-Pop Music

3-Rock Music

4-Jazz

5-Classical

Select genre of playlist :

2




Sample run (Display the playlist, after creating the playlist):


1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

***************************

2

Displaying the playlist: Mix

--------------------------------------------------------

Playlist Name: Mix

Creator Name: John Doe

Genre: Pop Music

--------------------------------------------------------

Song Name: Kuzu Kuzu

Artist Name: Tarkan

Album Name: Karma


Duration: 3:53

Times Played: 29144161

--------------------------------------------------------

Song Name: Personal Jesus

Artist Name: Depeche Mode

Album Name: Violator

Duration: 4:54

Times Played: 28646612

--------------------------------------------------------



Sample run (Sort the playlist):


1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

***************************

3

Sorted playlists:

--------------------------------------------------------

Playlist Name: Mix

Creator Name: John Doe

Genre: Pop Music

--------------------------------------------------------

Song Name: Personal Jesus

Artist Name: Depeche Mode

Album Name: Violator

Duration: 4:54

Times Played: 28646612

--------------------------------------------------------

Song Name: Kuzu Kuzu

Artist Name: Tarkan

Album Name: Karma

Duration: 3:53

Times Played: 29144161

--------------------------------------------------------



Sample run (Remove a song, then display the playlist):


1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

***************************

4

Which song do you want to remove? (Remove by index)


0

1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

2

Displaying the playlist: Mix

--------------------------------------------------------

Playlist Name: Mix

Creator Name: John Doe

Genre: Pop Music

--------------------------------------------------------

Song Name: Kuzu Kuzu

Artist Name: Tarkan

Album Name: Karma

Duration: 3:53

Times Played: 29144161

--------------------------------------------------------



Sample run (Create another playlist with one song then display all playlists):


1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

***************************

1

How many songs do you want to add to playlist?:

1

Enter artist name for Song 1:

Natasha Bedingfield

Enter album name for Song 1:

Unwritten

Enter song name for Song 1:

Unwritten

Enter song duration in seconds for Song 1:

259

Enter the times song has been played for Song 1:

384325136

Enter playlist name:

BedingfieldSongs


Enter creator of playlist:

Johanna Doe

0-Country

1-Electronic

2-Pop Music

3-Rock Music

4-Jazz

5-Classical

Select genre of playlist :

2

1 - Create a new Playlist

2 - Display the Playlist

3 - Sort the Playlist

4 - Remove song by index from the current Playlist

5 - Display all Playlists

6 - Exit

5

All playlists:

--------------------------------------------------------

Playlist Name: Mix

Creator Name: John Doe

Genre: Pop Music

--------------------------------------------------------

Song Name: Kuzu Kuzu

Artist Name: Tarkan

Album Name: Karma

Duration: 3:53

Times Played: 29144161

--------------------------------------------------------

--------------------------------------------------------

Playlist Name: BedingfieldSongs

Creator Name: Johanna Doe

Genre: Pop Music

--------------------------------------------------------

Song Name: Unwritten

Artist Name: Natasha Bedingfield

Album Name: Unwritten

Duration: 4:19

Times Played: 384325136

--------------------------------------------------------

More products