Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ch 8 Program: Playlist (Java) I'm almost done with this program but I can't seem to get some option to output correctly. Here are the

Ch 8 Program: Playlist (Java)

I'm almost done with this program but I can't seem to get some option to output correctly.

Here are the instructions along with some output examples:

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:

Unique ID: S123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237 

(2) In main(), prompt the user for the title of the playlist. (1 pt) Ex:

Enter playlist's title: JAMZ 

(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:

JAMZ 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: 

(4) Implement "Output full playlist" menu option. If the list is empty, output: Playlist is empty (3 pts) Ex:

JAMZ - OUTPUT FULL PLAYLIST 1. Unique ID: SD123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237 2. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391 3. Unique ID: J345 Song Name: Canned Heat Artist Name: Jamiroquai Song Length (in seconds): 330 4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197 5. Unique ID: SD567 Song Name: I Got The News Artist Name: Steely Dan Song Length (in seconds): 306 

Ex (empty playlist):

JAMZ - OUTPUT FULL PLAYLIST Playlist is empty 

(5) Implement the "Add song" menu item. New additions are added to the end of the list. (2 pts) Ex:

ADD SONG Enter song's unique ID: SD123 Enter song's name: Peg Enter artist's name: Steely Dan Enter song's length (in seconds): 237 

(6) Implement the "Remove song" method. Prompt the user for the unique ID of the song to be removed.(4 pts) Ex:

REMOVE SONG Enter song's unique ID: JJ234 "All For You" removed 

(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:

CHANGE POSITION OF SONG Enter song's current position: 3 Enter new position for song: 2 "Canned Heat" moved to position 2 

(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:

OUTPUT SONGS BY SPECIFIC ARTIST Enter artist's name: Janet Jackson 2. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391 4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197 

(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:

OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS) Total time: 1461 seconds

My 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; } public SongEntry(String uniqueID, String songName, String artistName, int songLength) { this.uniqueID = uniqueID; this.songName = songName; this.artistName = artistName; this.songLength = songLength; } 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; } 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 { private SongEntry headNode; private SongEntry tailNode; private int x; public Playlist() { this.headNode = null; this.tailNode = null; this.x = 0; } boolean isEmpty() { return (headNode == null); } 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; } } boolean songExists(String songID) { SongEntry song = this.headNode; while (song != null) { if (song.getID().equalsIgnoreCase(songID)) { return true; } song = song.getNext(); } return false; } 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; x += 1; } else { if (!songExists(uniqueID)) { tailNode.setNext(newSong); tailNode = newSong; x += 1; } else System.out.println("Song ID: " + uniqueID + " is already in the playlist."); } } String removeSong(String uniqueID) { SongEntry song = this.headNode; String title = null; if (headNode.getID().equalsIgnoreCase(uniqueID)) { title = headNode.getSongName(); headNode = headNode.getNext(); x -= 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()); x -= 1; break; } song = song.getNext(); } } return title; } 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(); } 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 == x) tailNode = song; } if (newPos <= 1) { target.setNext(headNode); headNode = target; actualPos = 1; } else if (newPos >= x) { tailNode.setNext(target); tailNode = target; target.setNext(null); actualPos = x; } 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; } void songByArtist(String artistName) { SongEntry song = this.headNode; while (song != null) { if (song.getArtistName().equalsIgnoreCase(artistName)) song.printPlaylistSongs(); song = song.getNext(); } } int totalPlaylistTime() { int time = 0; SongEntry song = this.headNode; while (song != null) { time += song.getSongLength(); song = song.getNext(); } return time; } 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.println("Choose an option:"); menuOption = scnr.nextLine(); switch(menuOption) { case "q": case "Q": break; case "a": case "A": System.out.println("ADD SONG"); System.out.println("Enter song's unique ID:"); String songID = scnr.nextLine(); System.out.println("Enter song's name:"); String songName = scnr.nextLine(); System.out.println("Enter artist's name:"); String artistName = scnr.nextLine(); System.out.println("Enter song's length (in seconds):"); int songLength = scnr.nextInt(); list.addSong(songID, songName, artistName, songLength); System.out.println(); break; case "d": case "D": System.out.println("REMOVE SONG"); if (!list.isEmpty()) { System.out.println("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": case "C": if (!list.isEmpty()) { System.out.println("CHANGE POSITION OF SONG"); int currPos = -1; do { System.out.println("Enter song's current position:"); currPos = scnr.nextInt(); if ((currPos < 1) || (currPos > list.x)) System.out.println("Invalid current position. Please try again."); } while ((currPos < 1 || currPos > list.x)); System.out.println("Enter new position for song:"); int newPos = scnr.nextInt(); newPos = (newPos < 1) ? 1 : ((newPos > list.x) ? list.x : 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": case "S": if (!list.isEmpty()) { System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST"); System.out.println("Enter artist's name: "); artistName = scnr.nextLine(); list.songByArtist(artistName); } else System.out.println(" Playlist is empty"); break; case "t": 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": 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.println("Enter playlist's title:"); String title = scnr.nextLine(); System.out.println(); printMenu(title, scnr); } } // end of Playlist.java. 

This is an example of the output I'm getting when a user enters:

JAMZ

a

SD123

Peg

Steely Dan

237

o

q

ADD SONG

Enter song's unique ID: Enter song's name: Enter artist's name: Enter song's length (in seconds):

JAMZ 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: JAMZ 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: JAMZ - OUTPUT FULL PLAYLIST 1. Unique ID: SD123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237

JAMZ 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:

What's bolded "JAMZ PLAYLIST MENU" is only suppose to print once.

It's meant to look like this:

ADD SONG Enter song's unique ID: Enter song's name: Enter artist's name: Enter song's length (in seconds):

JAMZ 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: JAMZ - OUTPUT FULL PLAYLIST 1. Unique ID: SD123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237

JAMZ 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

Here's one more example:

CHANGE POSITION OF SONG Enter song's current position: Enter new position for song: "Peg" moved to position 3

JAMZ 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: JAMZ 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: JAMZ - OUTPUT FULL PLAYLIST 1. Unique ID: JJ234 Song Name: All For You Artist Name: Janet Jackson Song Length (in seconds): 391

2. Unique ID: J345 Song Name: Canned Heat Artist Name: Jamiroquai Song Length (in seconds): 330

3. Unique ID: SD123 Song Name: Peg Artist Name: Steely Dan Song Length (in seconds): 237

4. Unique ID: JJ456 Song Name: Black Eagle Artist Name: Janet Jackson Song Length (in seconds): 197

5. Unique ID: SD567 Song Name: I Got The News Artist Name: Steely Dan Song Length (in seconds): 306

JAMZ 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:

I can't figure out what I'm doing wrong here. Been working on this none stop for the last few days so I'm pretty exhausted and annoyed. Maybe I just need some rest and I'll figure it out.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions