Question

In: Computer Science

// The Song class that represents a song // Do not make any changes to this...

// The Song class that represents a song
// Do not make any changes to this file!

public class Song
{
// instance variables
private String m_artist;
private String m_title;
private Song m_link;

// constructor
public Song(String artist, String title)
{
m_artist = artist;
m_title = title;
m_link = null;
}

// getters and setters
public void setArtist(String artist)
{
m_artist = artist;
}

public String getArtist()
{
return m_artist;
}

public void setTitle(String title)
{
m_title = title;
}

public String getTitle()
{
return m_title;
}
  
public void setLink(Song link)
{
m_link = link;
}

public Song getLink()
{
return m_link;
}
}

Complete the required methods:

public class SongList
{
// instance variables
private Song m_last;
private int m_numElements;

// constructor
// Do not make any changes to this method!
public SongList()
{
m_last = null;
m_numElements = 0;
}

// check whether the list is empty
// Do not make any changes to this method!
boolean isEmpty()
{
if (m_last == null)
return true;
else
return false;
}

// return the size of the list (# of Song nodes)
// Do not make any changes to this method!
public int size()
{
return m_numElements;
}

// add a new Song to the circular linked list with the given artist and
// title, keeping the list sorted by *song title*.
public void add(String artist, String title)
{
// TODO: implement this method
}

// remove a Song associated with the given artist and title from the list,
// keeping the list sorted by *song title*.
public boolean remove(String artist, String title)
{
// TODO: implement this method
}
  
  
// build and return a circular linked list that contains all songs from the
// given artist
public SongList buildList(String artist)
{
// TODO: implement this method
}
  
// return a string representation of the list
// Do not make any changes to this method!
public String toString()
{   
String listContent = "";
Song current = m_last;
  
if (m_last != null)
do
{
current = current.getLink();
listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";

} while (current != m_last);

return listContent;
}
}

Solutions

Expert Solution

// Song.java

public class Song
{
   // instance variables
   private String m_artist;
   private String m_title;
   private Song m_link;

   // constructor
   public Song(String artist, String title)
   {
       m_artist = artist;
       m_title = title;
       m_link = null;
   }

   // getters and setters
   public void setArtist(String artist)
   {
       m_artist = artist;
   }

   public String getArtist()
   {
       return m_artist;
   }

   public void setTitle(String title)
   {
       m_title = title;
   }

   public String getTitle()
   {
       return m_title;
   }
  
   public void setLink(Song link)
   {
       m_link = link;
   }

   public Song getLink()
   {
       return m_link;
   }
}

//end of Song.java

// SongList.java

public class SongList {
  
   // instance variables
   private Song m_last;
   private int m_numElements;

   // constructor
   // Do not make any changes to this method!
   public SongList()
   {
       m_last = null;
       m_numElements = 0;
   }

   // check whether the list is empty
   // Do not make any changes to this method!
   boolean isEmpty()
   {
       if (m_last == null)
           return true;
       else
           return false;
   }

   // return the size of the list (# of Song nodes)
   // Do not make any changes to this method!
   public int size()
   {
       return m_numElements;
   }

   // add a new Song to the circular linked list with the given artist and
   // title, keeping the list sorted by *song title*.
   public void add(String artist, String title)
   {
       Song song = new Song(artist, title); // create a new Song with given artist and title
       if(isEmpty()) // empty list, make m_last equal to song
       {
           m_last = song;
           m_last.setLink(m_last); // set link of m_last to itself
       }
       else // non-empty list
       {
           // curr to first node
           Song curr = m_last.getLink();
           Song prev = m_last; // prev to last node
          
           // loop over the list until curr is the last node
           while(curr != m_last)
           {
               if(curr.getTitle().compareTo(title) > 0) // title of curr > title of song, insert before curr
               {
                   break; // exit the loop
               }
              
               prev = curr;
               curr = curr.getLink();
           }
          
           // curr has not reached the last node
           if(curr != m_last)
           {
               song.setLink(curr); // set song's link to curr
               prev.setLink(song); // set prev's link to song
           }
           else // curr has reached the last node
           {
               // compare title of song with title of last node
               if(curr.getTitle().compareTo(title) > 0) // song's title < m_last's title, insert before curr
               {
                   song.setLink(curr); // set song's link to curr
                   prev.setLink(song); // set prev's link to song
               }
               else // make song the last node
               {
                   // set song's link to the first node
                   song.setLink(m_last.getLink());
                   m_last.setLink(song); // set m_last's link to song
                   m_last = song; // update m_last to song
               }
           }
       }
      
       m_numElements++; // increment number of elements
   }
  
   // remove a Song associated with the given artist and title from the list,
   // keeping the list sorted by *song title*.
   public boolean remove(String artist, String title)
   {
      
       if(!isEmpty()) // not an empty list
       {
           // curr to first node and prev to last node
           Song curr = m_last.getLink();
           Song prev = m_last;
          
           // loop over the list until curr = last node
           while(curr != m_last)
           {
               // curr's title and artist match, remove curr
               if(curr.getArtist().equals(artist) && curr.getTitle().equals(title))
               {
                   // set link of prev to link of curr
                   prev.setLink(curr.getLink());
                   m_numElements--; // decrement number of elements
                   return true; // deletion successful
               }
              
               prev = curr;
               curr = curr.getLink();
           }
      
           // last node is the node to remove
           if(m_last.getArtist().equals(artist) && m_last.getTitle().equals(title))
           {
               // list contains 1 element, set it to empty list
               if(m_last.getLink() == m_last)
               {
                   m_last = null;
               }
               else
               {
                   prev.setLink(m_last.getLink()); // set prev's link to first node
                   m_last = prev; // update m_last to prev
               }
              
               m_numElements--; // decrement number of elements
               return true; // delete successful
           }
          
       }
      
       return false; // delete failed
   }
  
  
   // build and return a circular linked list that contains all songs from the
   // given artist
   public SongList buildList(String artist)
   {
       // create an empty list to contain the songs of artist
       SongList artistList = new SongList();
       if(!isEmpty()) // not an empty list
       {
           // curr to first node
           Song curr = m_last.getLink();
          
           // loop over the list until curr is the last node
           while(curr != m_last)
           {
               // curr's = artist
               if(curr.getArtist().equals(artist))
               {
                   artistList.add(curr.getArtist(), curr.getTitle()); // insert curr's song into artistList
               }
              
               curr = curr.getLink();
           }
          
           // last node's artist = artist, insert last node's data into artistList
           if(m_last.getArtist().equals(artist))
               artistList.add(m_last.getArtist(), m_last.getTitle());
       }
      
       return artistList;
   }
  
   // return a string representation of the list
   // Do not make any changes to this method!
   public String toString()
   {   
       String listContent = "";
       Song current = m_last;
      
       if (m_last != null)
       do
       {
           current = current.getLink();
           listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";
  
       } while (current != m_last);
  
       return listContent;
   }
}


// end of SongList.java


Related Solutions

Pick a song, any song, that has made a lasting impression on you, name the song...
Pick a song, any song, that has made a lasting impression on you, name the song and try to explain why it had an effect on you.
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
Song struct to store the data for a single song (make it a private member), and you must have an array of Song structures as a member variable.
USE C++ Song struct to store the data for a single song (make it a private member), and you must have an array of Song structures as a member variable.Player(string name, float size) a constructor for the class. ’name' is a name for it'size' is a maximum capacity in Mb.addSong(string band, string title, string length, float size) a function for adding a new song.'band' is a name of the band'title' is a name of the song'length' is a time length of...
Declare a Song struct to store the data for a single song (make it a private member), and you must have anarray of Song structures as a member variable.
USE C++ Declare a Song struct to store the data for a single song (make it a private member), and you must have anarray of Song structures as a member variable.Player(string name, float size) a constructor for the class. ’name' is a name for it'size' is a maximum capacity in Mb.addSong(string band, string title, string length, float size) a function for adding a new song.'band' is a name of the band'title' is a name of the song'length' is a time length...
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
Would you make any changes to Warren Buffet's investment strategy? Why or why not?
Would you make any changes to Warren Buffet's investment strategy? Why or why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT