Question

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

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

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
  
}

Playlist.java sample 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);
}
}

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

The printSongInfo method could be written as:

public void printSongInfo()
   {
           System.out.println("Title: "+this.songTitle);
           System.out.println("Length: "+this.songLength);
           System.out.println("Artist: "+this.songArtist);
   }

The output of the code :

Whole Code for the reference:

SongNode.java

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
   public void printSongInfo()
   {
           System.out.println("Title: "+this.songTitle);
           System.out.println("Length: "+this.songLength);
           System.out.println("Artist: "+this.songArtist);
   }
  
  

}

Playlist.java

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);
   }
}

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...
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
  • 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...

  • 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"...

  • Introduction In this final programming exercise, you'll get a chance to put together many of the...

    Introduction In this final programming exercise, you'll get a chance to put together many of the techniques used during this semester while incorporating OOP techniques to develop a simple song playlist class. This playlist class allows a user to add, remove and display songs in a playlist. The Base Class, Derived Class and Test Client Unlike the other PAs you completed in zyLabs, for this PA you will be creating THREE Java files in IntelliJ to submit. You will need...

  • (1) Create two files to submit: Song.java - Class definition MainClass.java - Contains main() method (2)...

    (1) Create two files to submit: Song.java - Class definition MainClass.java - Contains main() method (2) Build the Song class in Song.java with the following specifications: Private fields String songTitle String songArtist int secDuration Initialize String fields to "Not defined" and numeric to 0. Create overloaded constructor to allow passing of 1 argument (title), 2 (title, artist) and 3 arguments (title, artist, duration). Create public member method printSongInfo () with output formatted as below: Title: Born in the USA Artist:...

  • public class Song { private String title; private String artist; private int duration; public Song() {...

    public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...

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

  • 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