Question

THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

THE ENTIRE CODE SHOULD BE IN JAVA

Playlist (output linked list)

Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node.

Ex: If the input is:

Stomp!
380
The Brothers Johnson
The Dude
337
Quincy Jones
You Don't Own Me
151
Lesley Gore
-1

the output is:

LIST OF SONGS
-------------
Title: Stomp!
Length: 380
Artist: The Brothers Johnson

Title: The Dude
Length: 337
Artist: Quincy Jones

Title: You Don't Own Me
Length: 151
Artist: Lesley Gore

Playlist.java Sample code

import java.util.Scanner;

public class Playlist {

// TODO: Write method to ouptut list of songs

public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);

SongNode headNode;
SongNode currNode;
SongNode lastNode;

String songTitle;
int songLength;
String songArtist;

// Front of nodes list   
headNode = new SongNode();
lastNode = headNode;

// Read user input until -1 entered
songTitle = scnr.nextLine();
while (!songTitle.equals("-1")) {
songLength = scnr.nextInt();
scnr.nextLine();
songArtist = scnr.nextLine();

currNode = new SongNode(songTitle, songLength, songArtist);
lastNode.insertAfter(currNode);
lastNode = currNode;

songTitle = scnr.nextLine();
}
  
// Print linked list
System.out.println("LIST OF SONGS");
System.out.println("-------------");
printPlaylist(headNode);
}
}

SongNode.java Sample Code

public class SongNode {
private String songTitle;
private int songLength;
private String songArtist;
private SongNode nextNodeRef; // Reference to the next node   

public SongNode() {
songTitle = "";
songLength = 0;
songArtist = "";
nextNodeRef = null;
}

// Constructor   
public SongNode(String songTitleInit, int songLengthInit, String songArtistInit) {
this.songTitle = songTitleInit;
this.songLength = songLengthInit;
this.songArtist = songArtistInit;
this.nextNodeRef = null;
}

// Constructor   
public SongNode(String songTitleInit, int songLengthInit, String songArtistInit, SongNode nextLoc) {
this.songTitle = songTitleInit;
this.songLength = songLengthInit;
this.songArtist = songArtistInit;
this.nextNodeRef = nextLoc;
}

// insertAfter
public void insertAfter(SongNode nodeLoc) {
SongNode tmpNext;

tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}

// Get location pointed by nextNodeRef
public SongNode getNext() {
return this.nextNodeRef;
}

// TODO: Write printSongInfo() method
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

import java.util.Scanner;

public class Playlist {

   public static void printPlaylist(SongNode songs){
       SongNode song = songs.getNext();
       while (song!=null) {
           song.printSongInfo();
           System.out.println();
           song = song.getNext();
       }
   }

   public static void main (String[] args) {
       Scanner scnr = new Scanner(System.in);

       SongNode headNode;
       SongNode currNode;
       SongNode lastNode;

       String songTitle;
       int songLength;
       String songArtist;

       // Front of nodes list
       headNode = new SongNode();
       lastNode = headNode;

       // Read user input until -1 entered
       songTitle = scnr.nextLine();
       while (!songTitle.equals("-1")) {
           songLength = scnr.nextInt();
           scnr.nextLine();
           songArtist = scnr.nextLine();

           currNode = new SongNode(songTitle, songLength, songArtist);
           lastNode.insertAfter(currNode);
           lastNode = currNode;

           songTitle = scnr.nextLine();
       }
      
       // Print linked list
       System.out.println("LIST OF SONGS");
       System.out.println("-------------");
       printPlaylist(headNode);
   }
}

class SongNode {
   private String songTitle;
   private int songLength;
   private String songArtist;
   private SongNode nextNodeRef; // Reference to the next node

   public SongNode() {
       songTitle = "";
       songLength = 0;
       songArtist = "";
       nextNodeRef = null;
   }

   // Constructor
   public SongNode(String songTitleInit, int songLengthInit, String songArtistInit) {
       this.songTitle = songTitleInit;
       this.songLength = songLengthInit;
       this.songArtist = songArtistInit;
       this.nextNodeRef = null;
   }

   // Constructor
   public SongNode(String songTitleInit, int songLengthInit, String songArtistInit, SongNode nextLoc) {
       this.songTitle = songTitleInit;
       this.songLength = songLengthInit;
       this.songArtist = songArtistInit;
       this.nextNodeRef = nextLoc;
   }

   // insertAfter
   public void insertAfter(SongNode nodeLoc) {
       SongNode tmpNext;

       tmpNext = this.nextNodeRef;
       this.nextNodeRef = nodeLoc;
       nodeLoc.nextNodeRef = tmpNext;
   }

   // Get location pointed by nextNodeRef
   public SongNode getNext() {
       return this.nextNodeRef;
   }
   public void printSongInfo(){
       System.out.println("Title: "+this.songTitle);
       System.out.println("Length: "+this.songLength);
       System.out.println("Artist: "+this.songArtist);
   }

}

NOTE:- If you need any modifications in the code, please comment below.Please give positive rating.THUMBS UP.

                     THANK YOU!!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • Grocery shopping list (linked list: inserting at the end of a list)

    import java.util.Scanner;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      ItemNode headNode;  // Create intNode objects                                                         ItemNode currNode;      ItemNode lastNode;      String item;      int i;      // Front of nodes list           ...

  • 10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)

    10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)Hello, I searched for similar problems, but their InsertAtEnd() have two parameters, while my question asks for one.Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list.DO NOT print the dummy head node.Ex. if the input is:4 Kale  Lettuce  Carrots  Peanutswhere 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • You will be building a linked list. Make sure to keep track of both the head and tail nodes.

    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 declarationPlaylist.java - Contains main() methodBuild the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.Private fieldsString uniqueID - Initialized to "none" in default constructorstring songName - Initialized to "none" in default constructorstring artistName - Initialized to "none"...

  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • 9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...

    9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Given the following class definition in the file Classroom.h, which XXX and YYY complete the corresponding.cpp...

    Given the following class definition in the file Classroom.h, which XXX and YYY complete the corresponding.cpp file? #ifndef CLASSROOM_H #define CLASSROOM_H class Classroom public: void SetNumber(int n); void Printo const; private: int num; }; Tendif #include <iostream using namespace std; XXX void Class Room:: Set Number(int n) { nun: cout << "Number of Class Rooms: << num << endl; include "Classroom.hr vold Classroom: Print( const sinclude "Classroom.cpp" vold Classroom. Print() const #include "Classroom.h" void Class Room::Print) const #include "Classroom.cpp" void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT