9.14 Ch 8 Program: Playlist (Java) You will be building a linked list. Make sure
ID: 3916810 • Letter: 9
Question
9.14 Ch 8 Program: Playlist (Java)
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(1) Create two files to submit.
SongEntry.java - Class declaration
Playlist.java - Contains main() method
Build the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.
Private fields
String uniqueID - Initialized to "none" in default constructor
string songName - Initialized to "none" in default constructor
string artistName - Initialized to "none" in default constructor
int songLength - Initialized to 0 in default constructor
SongEntry nextNode - Initialized to null in default constructor
Default constructor (1 pt)
Parameterized constructor (1 pt)
Public member methods
void insertAfter(SongEntry currNode) (1 pt)
void setNext(SongEntry nextNode) - Mutator (1 pt)
String getID()- Accessor
String getSongName() - Accessor
String getArtistName() - Accessor
int getSongLength() - Accessor
SongEntry getNext() - Accessor
void printPlaylistSongs()
Ex. of printPlaylistSongs output:
(2) In main(), prompt the user for the title of the playlist. (1 pt)
Ex:
(3) Implement the printMenu() method. printMenu() takes the playlist title as a parameter and a Scanner object, outputs a menu of options to manipulate the playlist, and reads the user menu selection. Each option is represented by a single character. Build and output the menu within the method.
If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to execute the menu until the user enters q to Quit. (3 pts)
Ex:
(4) Implement "Output full playlist" menu option. If the list is empty, output: Playlist is empty (3 pts)
Ex:
Ex (empty playlist):
(5) Implement the "Add song" menu item. New additions are added to the end of the list. (2 pts)
Ex:
(6) Implement the "Remove song" method. Prompt the user for the unique ID of the song to be removed.(4 pts)
Ex:
(7) Implement the "Change position of song" menu option. Prompt the user for the current position of the song and the desired new position. Valid new positions are 1 - n (the number of nodes). If the user enters a new position that is less than 1, move the node to the position 1 (the head). If the user enters a new position greater than n, move the node to position n (the tail). 6 cases will be tested:
Moving the head node (1 pt)
Moving the tail node (1 pt)
Moving a node to the head (1 pt)
Moving a node to the tail (1 pt)
Moving a node up the list (1 pt)
Moving a node down the list (1 pt)
Ex:
(8) Implement the "Output songs by specific artist" menu option. Prompt the user for the artist's name, and output the node's information, starting with the node's current position. (2 pt)
Ex:
(9) Implement the "Output total time of playlist" menu option. Output the sum of the time of the playlist's songs (in seconds). (2 pts)
Ex:
Explanation / Answer
Hello there, I have answered this question before, so I’m referring my own solution here. Below is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
EDIT: I’m getting troubles submitting the answer without losing the format. Showing character limit exceeded error. So I have to paste it as a plain text, which will cause the loss of code formatting and indentations. Sorry for the trouble. If you are using eclipse ,copy the code and press ctrl+shift+F to format the code
// SongEntry.java
public class SongEntry {
private String uniqueID;
private String songName;
private String artistName;
private int songLength;
private SongEntry nextNode;
// Default constructor.
public SongEntry() {
uniqueID = "none";
songName = "none";
artistName = "none";
songLength = 0;
nextNode = null;
}
// constructor to initialize all fields
public SongEntry(String uniqueID, String songName, String artistName,
int songLength) {
this.uniqueID = uniqueID;
this.songName = songName;
this.artistName = artistName;
this.songLength = songLength;
}
/**
* method to insert a new node between this node and current next node
*
* @param currNode - node to be added
*/
public void insertAfter(SongEntry currNode) {
if (currNode == null) {
return;
}
currNode.setNext(nextNode);
nextNode = currNode;
}
public void setNext(SongEntry nextNode) {
this.nextNode = nextNode;
}
public SongEntry getNext() {
return this.nextNode;
}
/**
* Mutator methods for uniqueID, songName, artistName, and songLength. These
* methods are used to set a value to the private fields I have initialized.
*/
public void setUniqueID(String uniqueID) {
this.uniqueID = uniqueID;
}
public void setSongName(String songName) {
this.songName = songName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public void setSongLength(int songLength) {
this.songLength = songLength;
}
// Accessor methods that are used to return the value of the private fields.
public String getID() {
return this.uniqueID;
}
public String getSongName() {
return this.songName;
}
public String getArtistName() {
return this.artistName;
}
public int getSongLength() {
return this.songLength;
}
/**
* method to print song details
*/
public void printPlaylistSongs() {
System.out.println("Unique ID: " + getID());
System.out.println("Song Name: " + getSongName());
System.out.println("Artist Name: " + getArtistName());
System.out.println("Song Length (in seconds): " + getSongLength());
System.out.println("");
}
}
// Playlist.java
import java.util.Scanner;
public class Playlist {
//attributes
private SongEntry headNode;
private SongEntry tailNode;
private int count;
//default constructor
public Playlist() {
this.headNode = null;
this.tailNode = null;
this.count = 0;
}
/**
* @return true if the list is empty, else false
*/
boolean isEmpty() {
return (headNode == null);
}
/**
* method to print the entire play list
* @param title - title of the play list
*/
void outputPlaylist(String title) {
SongEntry song = this.headNode;
int i = 1;
System.out.println(title + " - OUTPUT FULL PLAYLIST");
while (song != null) {
System.out.println(i + ".");
song.printPlaylistSongs();
song = song.getNext();
i += 1;
}
}
/**
* method to check if a song with given id exists in the list
*/
boolean songExists(String songID) {
SongEntry song = this.headNode;
while (song != null) {
if (song.getID().equalsIgnoreCase(songID)) {
return true;
}
song = song.getNext();
}
return false;
}
/**
* method to add a song with given attributes to the list
*/
void addSong(String uniqueID, String songName, String artistName,
int songLength) {
SongEntry newSong = new SongEntry(uniqueID, songName, artistName,
songLength);
if (headNode == null) {
headNode = newSong;
tailNode = headNode;
count += 1;
} else {
if (!songExists(uniqueID)) {
tailNode.setNext(newSong);
tailNode = newSong;
count += 1;
} else
System.out.println("Song ID: " + uniqueID
+ " is already in the playlist.");
}
}
/**
* method to remove a song with given id from the list
*/
String removeSong(String uniqueID) {
SongEntry song = this.headNode;
String title = null;
if (headNode.getID().equalsIgnoreCase(uniqueID)) {
title = headNode.getSongName();
headNode = headNode.getNext();
count -= 1;
} else {
while (song.getNext() != null) {
if (song.getNext().getID().equalsIgnoreCase(uniqueID)) {
title = song.getNext().getSongName();
if (song.getNext().getNext() == null)
tailNode = song;
song.setNext(song.getNext().getNext());
count -= 1;
break;
}
song = song.getNext();
}
}
return title;
}
/**
* method to get a song(name) with given id from in the list
*/
String getSong(int index) {
SongEntry song = this.headNode;
String title = null;
int i = 1;
while (i != index) {
song = song.getNext();
i += 1;
}
return song.getSongName();
}
/**
* method to change the index of a song from currPos to newPos
*/
int changeSongPos(int currPos, int newPos) {
int actualPos = -1;
if (currPos != newPos) {
int pos = 1;
SongEntry song = this.headNode;
SongEntry target = null;
if (currPos == 1) {
target = headNode;
headNode = headNode.getNext();
} else {
while (pos != (currPos - 1)) {
song = song.getNext();
pos += 1;
}
target = song.getNext();
song.setNext(target.getNext());
if (currPos == count)
tailNode = song;
}
if (newPos <= 1) {
target.setNext(headNode);
headNode = target;
actualPos = 1;
} else if (newPos >= count) {
tailNode.setNext(target);
tailNode = target;
target.setNext(null);
actualPos = count;
} else {
song = this.headNode;
pos = 1;
while (pos != (newPos - 1)) {
song = song.getNext();
pos += 1;
}
target.setNext(song.getNext());
song.setNext(target);
actualPos = newPos;
}
}
return actualPos;
}
/**
* method to print songs by a given artist
*/
void songByArtist(String artistName) {
SongEntry song = this.headNode;
while (song != null) {
if (song.getArtistName().equalsIgnoreCase(artistName))
song.printPlaylistSongs();
song = song.getNext();
}
}
/**
* method to return the total play list time of all songs in the list
*/
int totalPlaylistTime() {
int time = 0;
SongEntry song = this.headNode;
while (song != null) {
time += song.getSongLength();
song = song.getNext();
}
return time;
}
/**
* method to print the menu
*/
private static void printMenu(String title, Scanner scnr) {
Playlist list = new Playlist();
String menuOption;
do {
System.out.println(title + " PLAYLIST MENU" + " a - Add song"
+ " d - Remove song" + " c - Change position of song"
+ " s - Output songs by specific artist"
+ " t - Output total time of playlist (in seconds)"
+ " o - Output full playlist" + " q - Quit");
System.out.println();
System.out.print("Choose an option: ");
menuOption = scnr.nextLine();
/**
* converting to lower case characters and stripping first
* character, so that we can minimise comparisons and use character
* to switch cases
*/
char c = menuOption.toLowerCase().charAt(0);
switch (c) {
case 'q':
break;
case 'a':
System.out.println("ADD SONG");
System.out.print("Enter song's unique ID: ");
String songID = scnr.nextLine();
System.out.print("Enter song's name: ");
String songName = scnr.nextLine();
System.out.print("Enter artist's name: ");
String artistName = scnr.nextLine();
System.out.print("Enter song's length (in seconds): ");
int songLength = Integer.parseInt(scnr.nextLine());
list.addSong(songID, songName, artistName, songLength);
System.out.println();
break;
case 'd':
System.out.println("REMOVE SONG");
if (!list.isEmpty()) {
System.out.print("Enter song's unique ID: ");
songID = scnr.nextLine();
String removedSong = list.removeSong(songID);
System.out
.println((removedSong == null) ? ("Cannot find the song with id " + songID)
: (""" + removedSong + """ + " " + "removed."));
} else
System.out.println("Playlist is empty");
System.out.println();
break;
case 'c':
if (!list.isEmpty()) {
System.out.println("CHANGE POSITION OF SONG");
int currPos = -1;
do {
System.out.print("Enter song's current position: ");
currPos = Integer.parseInt(scnr.nextLine());
if ((currPos < 1) || (currPos > list.count))
System.out
.println("Invalid current position. Please try again.");
} while ((currPos < 1 || currPos > list.count));
System.out.print("Enter new position for song: ");
int newPos = Integer.parseInt(scnr.nextLine());
newPos = (newPos < 1) ? 1 : ((newPos > list.count) ? list.count
: newPos);
newPos = list.changeSongPos(currPos, newPos);
if (newPos != -1)
System.out.println(""" + list.getSong(newPos) + """
+ " moved to position " + newPos);
else
System.out.println("No change");
} else
System.out.println("Playlist is empty");
System.out.println();
break;
case 's':
if (!list.isEmpty()) {
System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
System.out.print("Enter artist's name: ");
artistName = scnr.nextLine();
list.songByArtist(artistName);
} else
System.out.println(" Playlist is empty");
break;
case 't':
if (!list.isEmpty()) {
System.out
.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");
System.out.println("Total time: "
+ list.totalPlaylistTime() + " seconds ");
} else {
System.out.println(title);
System.out.println(" Playlist is empty");
}
break;
case 'o':
if (!list.isEmpty()) {
list.outputPlaylist(title);
} else {
System.out.println(title + " - OUTPUT FULL PLAYLIST"
+ " Playlist is empty");
System.out.println();
}
break;
}
} while (!menuOption.equalsIgnoreCase("q"));
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter playlist's title:");
String title = scnr.nextLine();
System.out.println();
printMenu(title, scnr);
}
}
/*OUTPUT*/
Enter playlist's title:Collection
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: a
ADD SONG
Enter song's unique ID: 1234
Enter song's name: Hello
Enter artist's name: Adele
Enter song's length (in seconds): 320
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: o
Collection - OUTPUT FULL PLAYLIST
1.
Unique ID: 1234
Song Name: Hello
Artist Name: Adele
Song Length (in seconds): 320
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: a
ADD SONG
Enter song's unique ID: 1122
Enter song's name: Shape of You
Enter artist's name: Ed Sheeran
Enter song's length (in seconds): 420
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: o
Collection - OUTPUT FULL PLAYLIST
1.
Unique ID: 1234
Song Name: Hello
Artist Name: Adele
Song Length (in seconds): 320
2.
Unique ID: 1122
Song Name: Shape of You
Artist Name: Ed Sheeran
Song Length (in seconds): 420
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: t
OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)
Total time: 740 seconds
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: a
ADD SONG
Enter song's unique ID: 1089
Enter song's name: Gasolina
Enter artist's name: Someone
Enter song's length (in seconds): 310
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: c
CHANGE POSITION OF SONG
Enter song's current position: 0
Invalid current position. Please try again.
Enter song's current position: 1
Enter new position for song: 2
"Hello" moved to position 2
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: o
Collection - OUTPUT FULL PLAYLIST
1.
Unique ID: 1122
Song Name: Shape of You
Artist Name: Ed Sheeran
Song Length (in seconds): 420
2.
Unique ID: 1234
Song Name: Hello
Artist Name: Adele
Song Length (in seconds): 320
3.
Unique ID: 1089
Song Name: Gasolina
Artist Name: Someone
Song Length (in seconds): 310
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: s
OUTPUT SONGS BY SPECIFIC ARTIST
Enter artist's name: Ed Sheeran
Unique ID: 1122
Song Name: Shape of You
Artist Name: Ed Sheeran
Song Length (in seconds): 420
Collection PLAYLIST MENU
a - Add song
d - Remove song
c - Change position of song
s - Output songs by specific artist
t - Output total time of playlist (in seconds)
o - Output full playlist
q - Quit
Choose an option: q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.