Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8.12 Ch 8 Program: Playlist (Java) You will be building a linked list. Make sure

ID: 3859328 • Letter: 8

Question

8.12 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

package songs;

//Include packages.
import java.util.Scanner;

//Create the class Playlist with main() method.
public class Playlist
{

   //Declare the objects.
   public static Scanner sc = new Scanner(System.in);

   public static Scanner scInt = new Scanner(System.in);
  
   //Head node object of the SongEntry.
   public static SongEntry headSong = new SongEntry();
  
   //Tail node object of the SongEntry.
   public static SongEntry tailSong = new SongEntry();

   public static SongEntry allEntries;

   public static int numberOfNodes = 0;
  
   //printMenu() method to print the entire menu.
   public static void printMenu(String playlistTitle)

   {
  
   //Print on the console.
   System.out.println(" "+playlistTitle.toUpperCase()+" PLAYLIST MENU");

   System.out.println("a - Add song d - Remove song c - Change position "
           + "of song s - Output songs by specific artist");

   System.out.println("t - Output total time of playlist (in seconds) o "
           + "- Output full playlist q - Quit");
  
   //prompt the user to enter the choice.
System.out.println(" Choose an option: ");

String option = sc.next();
  
//Check the choice.
boolean isEnter = option.equals("a") || option.equals("d") || option.equals("c")
    || option.equals("s") || option.equals("t")
    || option.equals("o") || option.equals("q");
  
//Valid choice.
if(isEnter)

{
  
   //Start the switch-case
switch(option.charAt(0))

{
  
//Addsong.
case 'a': addSong();

printMenu(playlistTitle);

break;
  
//Remove song
case 'd': allEntries = removeSong(allEntries);

printMenu(playlistTitle);

break;
  
//Change the song position.
case 'c': allEntries = changeSongPosition(allEntries);

printMenu(playlistTitle);

break;

//Song by the specific artist.
case 's': songsBySpecificArtist(allEntries);

printMenu(playlistTitle);

break;
  
//Total time of the playlist.
case 't': totalTimeofPlaylist(allEntries);

printMenu(playlistTitle);

break;

//Output full playlist.
case 'o': outputFullPlaylist(allEntries);

printMenu(playlistTitle);

break;
  
//Quit
case 'q': break;

}

}
  
//Invalid choice.
else

{

System.out.println("Invalid Choice !");

printMenu(playlistTitle);

}

}

//outputFullPlaylist() method
public static void outputFullPlaylist(SongEntry entries)

{

int counter = 1;
  
//Playlist has certain songs.
if(entries != null)

{
  
System.out.println(counter+".");
  
// head node
entries.printPlaylistSongs();

counter++;
  
//Print the result.
while(entries.nextNode != null) // all the remaining nodes

{

entries = entries.nextNode;

System.out.println(counter+".");

entries.printPlaylistSongs();

counter++;

}

}
  
//Playlist is empty.
else

{

System.out.println("Playlist is empty");

}

}

//addSong() method
public static void addSong()

{
   //Prompt the user to add all the details about the song.
sc = new Scanner(System.in);

System.out.println("ADD SONG");

System.out.println("Enter song's Unique ID: ");

String songID = sc.next();

sc = new Scanner(System.in);

System.out.println("Enter song's name: ");

String songname = sc.nextLine();

sc = new Scanner(System.in);

System.out.println("Enter artist's name: ");

String artistName = sc.nextLine();

System.out.println("Enter song's length(in seconds): ");

int songlength = scInt.nextInt();

SongEntry entry = new SongEntry(songID, songname, artistName, songlength);
  
//If all the Entries are null.
if(allEntries == null)

{

headSong = entry; // this is the head

allEntries = entry;

tailSong = entry; // this is the tail

numberOfNodes++;

}
  
//entries are present.
else

{

allEntries.insertAfter(entry);

tailSong = entry;

numberOfNodes++;

}

}

//removeSong() method
public static SongEntry removeSong(SongEntry entries)

{
   //Prompt the user to enter the Song ID to be removed.
System.out.println("Enter the song's unique ID: ");

String id = sc.next();

SongEntry newEntry = null, entry=null;

int counter = 0;
  
//Run the while loop till the entries are null.
while(entries != null)

{
   //if counter is not equal to zero.
if(counter!=0)

{

newEntry.nextNode = null;

newEntry = newEntry.nextNode;

}  
  
  
if(!entries.getID().equals(id))

{
   //Create an object of the SongEntry.
newEntry = new SongEntry();
  
  
newEntry.setUniqueID(entries.getID());

newEntry.setSongName(entries.getSongName());

newEntry.setArtistName(entries.getArtistName());

newEntry.setSongLength(entries.getSongLength());

if(entry == null)

entry = newEntry;

else

entry.insertAfter(newEntry);

counter++;

}

else

{

   System.out.println(entries.getSongName()+" removed");

numberOfNodes--;

}

entries = entries.nextNode;

}

return entry;

}

//changeSongPosition() method.
public static SongEntry changeSongPosition(SongEntry entries)

{
  
   //Prompt the user to enter the old and new song position.
System.out.println("CHANGE POSITION OF SONG");

System.out.println("ENTER song's current position: ");

int currentPos = scInt.nextInt();

System.out.println("Enter new position of song: ");

int newPos = scInt.nextInt();

SongEntry currentPosEntry = null, entry = null, newPosEntry = null, returnEntry = null;

entry = entries;

int counter = 1;

// If new position is -1, set it to 1.

if(newPos<1)

newPos = 1;
  
//If the new position is more than the total number of songs
else if(newPos>numberOfNodes)
  
   //Set the new position to the total number of nodes.
newPos = numberOfNodes;

System.out.println("cuurent pos: "+currentPos);

System.out.println("new pos: "+newPos);
  
  
//Run the for loop to change the position.
for(int i=1; i<=numberOfNodes; i++)

{

if(i==currentPos)

currentPosEntry = entries;

else if(i==newPos)

newPosEntry = entries;

else

entries = entries.nextNode;

}


entries = entry;
  
//Run the for loop to adjust the current position.
while(counter <= numberOfNodes+1)

{

if(counter == currentPos) //need to adjust the current position

{

entries = entries.nextNode;

if(entries !=null)

{

   entry = new SongEntry(entries.getID(), entries.getSongName(),
           entries.getArtistName(), entries.getSongLength());

if(returnEntry == null)

returnEntry = entry;

else

returnEntry.insertAfter(entry);

entries = entries.nextNode;

}

counter++;

}
  
  
else if(counter == newPos)

{

entry = currentPosEntry;

entry.nextNode = null;

if(returnEntry == null)

returnEntry = entry;

else

returnEntry.insertAfter(entry);

counter++;

}

else

{

if(entries !=null)

{

   entry = new SongEntry(entries.getID(), entries.getSongName(),
           entries.getArtistName(), entries.getSongLength());

if(returnEntry == null)

returnEntry = entry;

else

returnEntry.insertAfter(entry);

entries = entries.nextNode;

}

counter++;

}

}

return returnEntry;

}

//totalTimeofPlaylist() method
public static void totalTimeofPlaylist(SongEntry entries)

{

   System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");

int totalSeconds = entries.getSongLength();

entries = entries.nextNode;
  
//Run the while loop to calculate the toatl song length.
while(entries != null)

{

totalSeconds += entries.getSongLength();

entries = entries.nextNode;

}

System.out.println("Total Time: "+totalSeconds+" seconds");

}

//songsBySpecificArtist() method
public static void songsBySpecificArtist(SongEntry entries)

{

sc = new Scanner(System.in);

System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
  
//Prompt the user to enter the artist name.
System.out.println("Enter artist's name: ");

String artistname = sc.nextLine();
  
//Print till the song entries are not null.
while(entries != null)

{

if(entries.getArtistName().equals(artistname))

{

entries.printPlaylistSongs();

}

entries = entries.nextNode;

}

}

/**

* @param args

*/

//main() method
public static void main(String[] args)

{
   //Prompt the user to enter the playlist title.
System.out.println("Enter playlist's title: ");

sc = new Scanner(System.in);

String title = sc.nextLine();
  
//Call the printMenu() method.
printMenu(title);

}

}

//SongEntry.java

package songs;


//Create the class SongEntry
public class SongEntry

{
   //Declare private data members.
private String uniqueID;

private String songName;

private String artistName;

private int songLength;

SongEntry nextNode;
  
//Default constructor.
SongEntry()

{
  
uniqueID = "";

songName = "";

artistName = "";

songLength = 0;

nextNode = null;

}
  
//Parameterized constructor
SongEntry(String uniqueID, String songName, String artistName, int songLength)

{

this.uniqueID = uniqueID;

this.songName = songName;

this.songLength = songLength;

this.artistName = artistName;

this.nextNode = null;

}
  
//insertAfter() method to insert songs in the linked list.
public void insertAfter( SongEntry entry)

{
  
SongEntry entries = this;

while(entries.nextNode != null)

{

entries = entries.nextNode;

}

entries.nextNode = entry;

}
  
//setNext() method to return the nextNode.
public void setNext(SongEntry entry){

this.nextNode = entry;

}
  
//getID() method to return the unique ID
public String getID()

{

return this.uniqueID;

}
  
//getSongName() method to return the songName.
public String getSongName()

{

return this.songName;

}
  
//getArtistName() method to return the artistName.
public String getArtistName()

{

return this.artistName;

}
  
//getSongLength() method to return the songLength
public int getSongLength()

{

return this.songLength;

}
  
//getNext() method to return the nextNode.
public SongEntry getNext()

{

return this.nextNode;

}
  
//setUniqueID() to set the unique ID.
public void setUniqueID(String uniqueID)

{

this.uniqueID = uniqueID;

}
  
//setSongName() method to set the SongName.
public void setSongName(String songName)

{

this.songName=songName;

}
  
//setArtistName() to set the artist name.
public void setArtistName(String artistName)

{

this.artistName=artistName;

}
  
  
//setSongLength() method to return the songLength.
public void setSongLength(int songLength )

{

this.songLength=songLength;

}
  
//printPlaylistSongs() method to print the results.
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());

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote