You will be building a linked list Make sure to keep track o

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:

PROVIDE FULL CODES AND SAMPLE SUCCESSFUL OUTPUT!!! THANKS!

Solution

Please find the below Java code using linked list operation for playlist operation:

public class SongEntry {
   private String uniqueID;
   private String songName;
   private String artistName;
   private int songLength;
   private SongEntry nextNode;

   /**
   * Default constructor
   */
   public SongEntry() {
       this.uniqueID = \"none\";
       this.songName = \"none\";
       this.artistName = \"none\";
       this.songLength = 0;
       this.nextNode = null;
   }

   /**
   * Parameterized constructor
   *
   * @param uniqueID
   * @param songName
   * @param artistName
   * @param songLength
   * @param nextNode
   */
   public SongEntry(String uniqueID, String songName, String artistName, int songLength, SongEntry nextNode) {
       this.uniqueID = uniqueID;
       this.songName = songName;
       this.artistName = artistName;
       this.songLength = songLength;
       this.nextNode = nextNode;
   }

   void insertAfter(SongEntry currNode) {
       this.nextNode=currNode;
   }

   void setNext(SongEntry nextNode) {
       // Mutator
       this.nextNode=nextNode;
   }

   String getID() {
       // Accessor
       return uniqueID;
   }

   String getSongName() {
       // - Accessor
       return songName;
   }

   String getArtistName() {
       // - Accessor
       return artistName;
   }

   int getSongLength() {
       // - Accessor
       return songLength;
   }

   SongEntry getNext() {
       // - Accessor
       return nextNode;
   }

   void printPlaylistSongs() {
       System.out.println(\"Unique ID:\" + uniqueID);
       System.out.println(\"Song Name:\" + songName);
       System.out.println(\"Artist Name:\" + artistName);
       System.out.println(\"Song Length (in seconds):\" + songLength);

   }

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PlayList {

   private SongEntry head;
   private SongEntry tail;

   private String playlistsTitle;

   public static void main(String[] args) {
       try (BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in))) {
           PlayList playList = new PlayList();
           System.out.println(\"Enter playlist\'s title:\");
           playList.playlistsTitle = scanner.readLine();
           String choice = \"\";
           while (true) {
               printMenu();
               choice = scanner.readLine();
               switch (choice) {
               case \"a\":
                   playList.head = playList.addSong(scanner);
                   break;
               case \"d\":
                   playList.removeSong(scanner);
                   break;
               case \"c\":
                   playList.changePosition(scanner);
                   break;
               case \"s\":
                   playList.outpuSongsBySpecificArtist(scanner);
                   break;
               case \"t\":
                   playList.outputTotalTimeOfPlaylist();
                   break;
               case \"o\":
                   playList.outputFullPlaylist();
                   break;

               default:
                   System.out.println(\"Wrong option! Try again!\");
                   break;
               }
           }

       } catch (IOException e) {
           e.printStackTrace();
       }

   }

   /**
   * printMenu
   */
   private static void printMenu() {
       System.out.println(\"JAMZ PLAYLIST MENU\");
       System.out.println(\"a - Add song\");
       System.out.println(\"d - Remove song\");
       System.out.println(\"c - Change position of song\");
       System.out.println(\"s - Output songs by specific artist\");
       System.out.println(\"t - Output total time of playlist (in seconds)\");
       System.out.println(\"o - Output full playlist\");
       System.out.println(\"q - Quit\");
       System.out.println(\"Choose an option:\");

   }

   /**
   * outputFullPlaylist
   */
   public void outputFullPlaylist() {
       System.out.println(playlistsTitle + \" - OUTPUT FULL PLAYLIST\");
       if (head == null) {
           System.out.println(\"Playlist is empty\");
           return;
       }

       // Print full playlist
       int index = 1;
       while (head.getNext() != null) {
           System.out.println(index + \".\");
           head.printPlaylistSongs();
           head=head.getNext();
       }
       System.out.println(index+1 + \".\");
       head.printPlaylistSongs();
   }

   /**
   *
   * @param scanner
   * @return
   * @throws IOException
   */
   public SongEntry addSong(BufferedReader scanner) throws IOException {
       System.out.println(\"Enter song\'s unique ID:\");
       String uniqueID = scanner.readLine();
       System.out.println(\"Enter song\'s name:\");
       String songName = scanner.readLine();
       System.out.println(\"Enter artist\'s name:\");
       String artistName = scanner.readLine();
       System.out.println(\"Enter song\'s length (in seconds):\");
       int songLength = Integer.parseInt(scanner.readLine());
       SongEntry newSongEntry = new SongEntry(uniqueID, songName, artistName, songLength, null);
       // Check list is empty
       if (head == null) {
           head = newSongEntry;
           tail = newSongEntry;
           return head;
       }
       // Save the reference to the header so we can return it.
       SongEntry temp = head;
       // loop until find the end of the list
       while (head.getNext() != null) {
           head = head.getNext();
       }

       // add the new newSongEntry
       head.setNext(newSongEntry);
       tail = head;
       return temp;
   }

   /**
   * removeSong
   *
   * @param scanner
   * @throws IOException
   */
   public void removeSong(BufferedReader scanner) throws IOException {
       System.out.println(\"REMOVE SONG\");
       System.out.println(\"Enter song\'s unique ID:\");
       String uniqueID = scanner.readLine();
       SongEntry temp = head;
       SongEntry prev = null;
       // If head node itself holds the key or multiple occurrences of key
       while (temp != null && temp.getID().equals(uniqueID)) {
           head = temp.getNext(); // Changed head
           temp = head; // Change Temp
       }

       // Delete occurrences other than head
       while (temp != null) {
           // Search for the key to be deleted, keep track of the
           // previous node as we need to change \'prev->next\'
           while (temp != null && !temp.getID().equals(uniqueID)) {
               prev = temp;
               temp = temp.getNext();
           }

           // If key was not present in linked list
           if (temp == null)
               return;

           // Unlink the node from linked list
           prev.setNext(temp.getNext());

           // Update Temp for next iteration of outer loop
           temp = prev.getNext();
       }
       System.out.println(\"\\\"All For You\\\" removed\");

   }

   /**
   * changePosition
   *
   * @param scanner
   * @throws IOException
   * @throws NumberFormatException
   */
   public void changePosition(BufferedReader scanner) throws NumberFormatException, IOException {
       System.out.println(\"CHANGE POSITION OF SONG\");
       System.out.println(\"Enter song\'s current position:\");
       int currentPosition = Integer.parseInt(scanner.readLine());
       System.out.println(\"Enter new position for song:\");
       int newPosition = Integer.parseInt(scanner.readLine());

       // Nothing to do if currentPosition and newPosition are same
       if (currentPosition == newPosition)
           return;

       // Search for currentPosition (keep track of prevCurrentPosition and
       // currCurrentPosition)
       SongEntry prevCurrentPosition = null, currCurrentPosition = head;
       int positionCounter = 1;
       while (currCurrentPosition != null && positionCounter != currentPosition) {
           prevCurrentPosition = currCurrentPosition;
           currCurrentPosition = currCurrentPosition.getNext();
           positionCounter++;
       }

       // Search for newPosition (keep track of prevY and currY)
       SongEntry prevNewPosition = null, currNewPosition = head;
       positionCounter = 1;
       while (currNewPosition != null && positionCounter != newPosition) {
           prevNewPosition = currNewPosition;
           currNewPosition = currNewPosition.getNext();
           newPosition++;
       }

       // If either currCurrentPosition or currNewPosition is not present,
       // nothing to do
       if (currCurrentPosition == null || currNewPosition == null)
           return;

       // If prevCurrentPosition is not head of linked list
       if (prevCurrentPosition != null)
           prevCurrentPosition.setNext(currNewPosition);
       else // make currNewPosition the new head
           head = currNewPosition;

       // If prevNewPosition is not head of linked list
       if (prevNewPosition != null)
           prevNewPosition.setNext(currCurrentPosition);
       else // make currCurrentPosition the new head
           head = currCurrentPosition;

       // Swap next pointers
       SongEntry temp = currCurrentPosition.getNext();
       currCurrentPosition.setNext(currNewPosition.getNext());
       currNewPosition.setNext(temp);

       System.out.println(currCurrentPosition.getSongName() + \" moved to position \" + newPosition);

   }

   public void outpuSongsBySpecificArtist(BufferedReader scanner) throws IOException {
       System.out.println(\"OUTPUT SONGS BY SPECIFIC ARTIST\");
       System.out.println(\"Enter artist\'s name:\");
       String artistsName = scanner.readLine();

       if (head == null) {
           System.out.println(\"Playlist is empty\");
           return;
       }

       // Print full playlist
       int index = 1;
       while (head.getNext() != null) {
           if (head.getArtistName().equals(artistsName)) {
               System.out.println(index + \".\");
               head.printPlaylistSongs();
           }
           head=head.getNext();
       }
      
       if (head.getArtistName().equals(artistsName)) {
           System.out.println(index+1 + \".\");
           head.printPlaylistSongs();
       }
      
   }

   public void outputTotalTimeOfPlaylist() {
       System.out.println(\"OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)\");
       if (head == null) {
           System.out.println(\"Playlist is empty\");
           return;
       }
       // Print full playlist
       int totalTime = 0;
       while (head.getNext() != null) {
           totalTime += head.getSongLength();
           head=head.getNext();
       }
       totalTime += head.getSongLength();
       System.out.println(\"Total time: \"+totalTime);
   }

}

Steps to compile & run the above program:

1. Compile:

javac SongEntry.java

javac PlayList.java

2. Run:

java PlayList

Sample output:

Enter playlist\'s title:
JAMZ
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:
a
Enter song\'s unique ID:
AHHUUS
Enter song\'s name:
abc
Enter artist\'s name:
xyz
Enter song\'s length (in seconds):
12
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:
a
Enter song\'s unique ID:
PQRSJJ
Enter song\'s name:
qqw
Enter artist\'s name:
asas
Enter song\'s length (in seconds):
15
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:
o
JAMZ - OUTPUT FULL PLAYLIST
1.
Unique ID:AHHUUS
Song Name:abc
Artist Name:xyz
Song Length (in seconds):12
2.
Unique ID:PQRSJJ
Song Name:qqw
Artist Name:asas
Song Length (in seconds):15
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:

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site