JAVA Linked lists You will be building a linked list. Make sure to keep track of
ID: 3575543 • Letter: J
Question
JAVA Linked lists
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
insertAfter() (1 pt)
setNext() - Mutator (1 pt)
getID() - Accessor
getSongName() - Accessor
getArtistName() - Accessor
getSongLength() - Accessor
getNext() - Accessor
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 outputs a menu of options to manipulate the playlist. 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:
(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
PROGRAM CODE:
SongEntry.java
package playlist;
public class SongEntry {
private String uniqueID;
private String songName;
private String artistName;
private int songLength;
SongEntry nextNode;
SongEntry()
{
uniqueID = "";
songName = "";
artistName = "";
songLength = 0;
nextNode = null;
}
SongEntry(String uniqueID, String songName, String artistName, int songLength)
{
this.uniqueID = uniqueID;
this.songName = songName;
this.songLength = songLength;
this.artistName = artistName;
this.nextNode = null;
}
public void insertAfter( SongEntry entry)
{
SongEntry entries = this;
while(entries.nextNode != null)
{
entries = entries.nextNode;
}
entries.nextNode = entry;
}
public void setNext(SongEntry entry){
this.nextNode = entry;
}
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;
}
public void setNextNode(SongEntry nextNode) {
this.nextNode = nextNode;
}
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 SongEntry getNext()
{
return this.nextNode;
}
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());
}
}
Playlist.java
package playlist;
import java.util.Scanner;
public class Playlist {
public static Scanner sc = new Scanner(System.in);
public static Scanner scInt = new Scanner(System.in);
public static SongEntry headSong = new SongEntry();
public static SongEntry tailSong = new SongEntry();
public static SongEntry allEntries;
public static int numberOfNodes = 0;
public static void printMenu(String playlistTitle)
{
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");
System.out.println(" Choose an option: ");
String option = sc.next();
boolean isEnter = option.equals("a") || option.equals("d") || option.equals("c") || option.equals("s") || option.equals("t") || option.equals("o") || option.equals("q");
if(isEnter)
{
switch(option.charAt(0))
{
case 'a': addSong();
printMenu(playlistTitle);
break;
case 'd': if(!isPlayListEmpty())
{
allEntries = removeSong(allEntries);
numberOfNodes--;
printMenu(playlistTitle);
}
break;
case 'c': if(!isPlayListEmpty())
{
allEntries = changeSongPosition(allEntries);
printMenu(playlistTitle);
}
break;
case 's': if(!isPlayListEmpty())
{
songsBySpecificArtist(allEntries);
printMenu(playlistTitle);
}
break;
case 't': if(!isPlayListEmpty())
{
totalTimeofPlaylist(allEntries);
printMenu(playlistTitle);
}
break;
case 'o': System.out.println(playlistTitle.toUpperCase() + " - OUTPUT FULL PLAYLIST");
outputFullPlaylist(allEntries);
printMenu(playlistTitle);
break;
case 'q': break;
}
}
else
{
System.out.println("Invalid Choice !");
printMenu(playlistTitle);
}
}
public static boolean isPlayListEmpty()
{
boolean isEmpty = false;
if(numberOfNodes<=0)
{
System.out.println("Playlist is empty ");
isEmpty = true;
}
return isEmpty;
}
public static void outputFullPlaylist(SongEntry entries)
{
int counter = 1;
if(entries != null)
{
System.out.println(counter+".");
entries.printPlaylistSongs(); // head node
counter++;
while(entries.nextNode != null) // all the remaining nodes
{
entries = entries.nextNode;
System.out.println(counter+".");
entries.printPlaylistSongs();
counter++;
}
}
else
{
System.out.println("Playlist is empty");
}
}
public static void addSong()
{
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(allEntries == null)
{
headSong = entry; // this is the head
allEntries = entry;
tailSong = entry; // this is the tail
numberOfNodes++;
}
else
{
allEntries.insertAfter(entry);
tailSong = entry;
numberOfNodes++;
}
}
public static SongEntry removeSong(SongEntry entries)
{
System.out.println("Enter the song's unique ID: ");
String id = sc.next();
SongEntry newEntry = null, entry=null;
int counter = 0;
while(counter<numberOfNodes)
{
if(!entries.getID().equals(id))
{
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);
}
else
{
System.out.println(""" + entries.getSongName()+"" removed");
}
entries = entries.nextNode;
counter++;
}
return entry;
}
public static SongEntry changeSongPosition(SongEntry entries)
{
if(numberOfNodes<=1)
{
System.out.println("There is only one song in the playlist");
return entries;
}
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(newPos<1)
newPos = 1;
else if(newPos>numberOfNodes)
newPos = numberOfNodes;
entry = entries;
for(int i=1; i<=numberOfNodes; i++)
{
if(i==currentPos)
currentPosEntry = entries;
else if(i==newPos)
newPosEntry = entries;
entries = entries.nextNode;
}
//entries = entry;
while(counter<=numberOfNodes)
{
SongEntry internalEntry = new SongEntry();
if(counter == currentPos)
{
internalEntry.setUniqueID(newPosEntry.getID());
internalEntry.setArtistName(newPosEntry.getArtistName());
internalEntry.setSongLength(newPosEntry.getSongLength());
internalEntry.setSongName(newPosEntry.getSongName());
}
else if(counter == newPos)
{
internalEntry.setUniqueID(currentPosEntry.getID());
internalEntry.setArtistName(currentPosEntry.getArtistName());
internalEntry.setSongLength(currentPosEntry.getSongLength());
internalEntry.setSongName(currentPosEntry.getSongName());
}
else
{
internalEntry.setUniqueID(entry.getID());
internalEntry.setArtistName(entry.getArtistName());
internalEntry.setSongLength(entry.getSongLength());
internalEntry.setSongName(entry.getSongName());
}
if(returnEntry == null)
returnEntry = internalEntry;
else
returnEntry.insertAfter(internalEntry);
entry = entry.nextNode;
counter++;
}
System.out.println(""" + currentPosEntry.getSongName() + """ + " moved to position " + newPos);
return returnEntry;
}
public static void totalTimeofPlaylist(SongEntry entries)
{
System.out.println("OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)");
int totalSeconds = entries.getSongLength();
entries = entries.nextNode;
while(entries != null)
{
totalSeconds += entries.getSongLength();
entries = entries.nextNode;
}
System.out.println("Total Time: "+totalSeconds+" seconds");
}
public static void songsBySpecificArtist(SongEntry entries)
{
sc = new Scanner(System.in);
System.out.println("OUTPUT SONGS BY SPECIFIC ARTIST");
System.out.println("Enter artist's name: ");
String artistname = sc.nextLine();
while(entries != null)
{
if(entries.getArtistName().equals(artistname))
{
entries.printPlaylistSongs();
}
entries = entries.nextNode;
}
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter playlist's title: ");
sc = new Scanner(System.in);
String title = sc.nextLine();
printMenu(title);
}
}
OUTPUT:
Enter playlist's title:
jmz
JMZ 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:
SD123
Enter song's name:
Peg
Enter artist's name:
Steely Dan
Enter song's length(in seconds):
237
JMZ 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:
JJ234
Enter song's name:
All For You
Enter artist's name:
Janet Jackson
Enter song's length(in seconds):
391
JMZ 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:
J345
Enter song's name:
Canned Heat
Enter artist's name:
Jamiroquai
Enter song's length(in seconds):
330
JMZ 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:
JJ456
Invalid Choice !
JMZ 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:
JJ456
Enter song's name:
Black Eagle
Enter artist's name:
Janet Jackson
Enter song's length(in seconds):
197
JMZ 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:
SD567
Enter song's name:
I Got The News
Enter artist's name:
Steely Dan
Enter song's length(in seconds):
306
JMZ 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:
SD765
Enter song's name:
Hello
Enter artist's name:
Adele
Enter song's length(in seconds):
310
JMZ 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:
6
Enter new position of song:
1
"Hello" moved to position 1
JMZ 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
JMZ - OUTPUT FULL PLAYLIST
1.
Unique ID: SD765
Song Name: Hello
Artist Name: Adele
Song Length(in seconds): 310
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
6.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length(in seconds): 237
JMZ 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:
Janet Jackson
Unique ID: JJ234
Song Name: All For You
Artist Name: Janet Jackson
Song Length(in seconds): 391
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length(in seconds): 197
JMZ 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: 1771 seconds
JMZ 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:
d
Enter the song's unique ID:
J345
"Canned Heat" removed
JMZ 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
JMZ - OUTPUT FULL PLAYLIST
1.
Unique ID: SD765
Song Name: Hello
Artist Name: Adele
Song Length(in seconds): 310
2.
Unique ID: JJ234
Song Name: All For You
Artist Name: Janet Jackson
Song Length(in seconds): 391
3.
Unique ID: JJ456
Song Name: Black Eagle
Artist Name: Janet Jackson
Song Length(in seconds): 197
4.
Unique ID: SD567
Song Name: I Got The News
Artist Name: Steely Dan
Song Length(in seconds): 306
5.
Unique ID: SD123
Song Name: Peg
Artist Name: Steely Dan
Song Length(in seconds): 237
JMZ 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.