Question

In: Computer Science

You are provided with the files "Song.java" and "SongList.java". You are required complete the methods in...

You are provided with the files "Song.java" and "SongList.java". You are required complete the methods in the latter file to implement the sorted circular linked list. The list is sorted according to the title of each song in alphabetical order. You need to write these methods: add(String artist, String title) – This method takes an artist and a title and adds a Song node with these values into the list. The list must still be sorted in ascending order by song title. You do not need to handle duplicates – you can assume that we never insert the same song for more than once. This method takes an artist and a title and remove a Song node that matches the artist and the title from the list. If remove is successful, return true. If no such node exists, return false. buildList(String artist) – This method takes an artist and searches in the list for all the song nodes associated with this artist. It then adds all these nodes into a new sorted circular linked list and returns it as a SongList object. Do not alter the original song list in this method.

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

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)
{
find(artist);
if(found)
{
if (Songlist == Songlist.getLink())
list = null;
else if (previous.getLink() == Songlist)
previous.setLink(m_last.getLink());
numElements--;
}
  
return found;

}
  
  
// 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)
   {
       // create a new Song node
       Song song = new Song(artist, title);
      
       if(isEmpty()) // list is empty
       {
           m_last = song; // set m_last to song
           m_last.setLink(m_last); // set link of m_last to itself
       }
       else // list is not empty
       {
           // initialize curr to first node i.e m_last.link
           Song curr = m_last.getLink();
          
           Song prev = m_last; // initialize prev to last node i.e node previous to curr
          
           // loop over the list until curr reaches 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
               }
              
               // move the pointer to the next node
               prev = curr;
               curr = curr.getLink();
           }
          
           // curr is not the last node, hence insert song before curr
           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
           {
               // curr points to m_last
               if(m_last.getTitle().compareTo(title) > 0) // song's title < m_last's title, insert before m_last
               {
                   song.setLink(m_last); // 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()) // list is not empty
       {
           // initailize curr to first node and prev to last node i.e node previous to curr
           Song curr = m_last.getLink();
           Song prev = m_last;
          
           // loop over the list until curr reaches 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 result = new SongList();
      
       if(!isEmpty()) // list is not empty
       {
           // initialize curr to first node
           Song curr = m_last.getLink();
          
           // loop over the list until curr reaches the last node
           while(curr != m_last)
           {
               // curr's artist = artist
               if(curr.getArtist().equals(artist))
               {
                   result.add(curr.getArtist(), curr.getTitle()); // insert curr's song into result
               }
              
               curr = curr.getLink();
           }
          
           // last node's artist = artist, insert last node's data into result
           if(m_last.getArtist().equals(artist))
               result.add(m_last.getArtist(), m_last.getTitle());
       }
      
       return result;
   }
  
   // 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

Integrating Word and Excel: Of the files, you submitted to complete the required assignments, which linking...
Integrating Word and Excel: Of the files, you submitted to complete the required assignments, which linking or embedding exercise did you find most useful? Explain why. Explain how you could utilize object linking and embedding concepts in your work environment. Explain how you could utilize object linking and embedding with files you have created for your personal use.
Online Store Simulator cpp files needed hpp files provided You will be writing a (rather primitive)...
Online Store Simulator cpp files needed hpp files provided You will be writing a (rather primitive) online store simulator. It will have three classes: Product, Customer and Store. To make things a little simpler for you, I am supplying you with the three .hpp files. You will write the three implementation files. You should not alter the provided .hpp files. Here are the .hpp files: Product.hpp, Customer.hpp and Store.hpp three files are below the questions; Here are descriptions of methods...
Download the following zip file Bag and complete the program. You will need to complete files...
Download the following zip file Bag and complete the program. You will need to complete files MyBag and . MyBag uses the java API class ArrayList as the underline data structure. Using the class variables given, complete the methods of the MyBag class. In your BagHand class you should set appropriate default values for select class variables in the constructor in the add method, use the MyBag object to add a PlayingCard type to the bag and find and set...
short answers (complete sentences are not required) Is it appropriate to use our methods to compute...
short answers (complete sentences are not required) Is it appropriate to use our methods to compute confidence intervals or do a hypothesis test if you know the variable is approximately normally distributed, with no outliers and the sample size is 18? Is it appropriate to use our methods to compute confidence intervals or do a hypothesis test if you know the variable is highly skewed with no outliers and the sample size is 100. Is it appropriate to use our...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Magdalena have provided you with the following information in the table and you are required to...
Magdalena have provided you with the following information in the table and you are required to find the value of the below bond to assist her with her investment decision: Cash Flow Appropriate required return End of year Amount 1 3,000.00 2.1% 2 3,000.00 3 3,000.00 4 53,000.00
arah provided you with the following information in the table and you are required to find...
arah provided you with the following information in the table and you are required to find the value of the below bond to assist her with her investment decision: Cash Flow Appropriate required return End of year Amount (N$) 1 750.00 15% 2 750.00 3 750.00 4 750.00 until infinity 750.00
For this assignment you are provided with four scenarios. For each scenario, you are required to...
For this assignment you are provided with four scenarios. For each scenario, you are required to answer the following question: What are the access-to-care issues in the given situations? Suggest at least two solutions to address the access-to-care issues in these scenarios. Scenarios: Mr. A is a 30-year-old African American male. His employer provides for his health insurance, which covers emergency room visits, hospitalization, and some preventive-care services such as yearly physicals. Whenever he schedules appointments for preventive-care services, he...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the following three methods in the class RandomFile: ​public static void randomBinaryFile(String fileName, int length) ​public static void randomNumberFile(String fileName, intlength) ​public static void randomCharFile(String fileName, int length) The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file. ● randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length; ● randomNumberFile...
There are three steps (open, action, close) required for handling files. You need to write a...
There are three steps (open, action, close) required for handling files. You need to write a Python program what executes each of these three steps. Your program must include:  Extensive comments to ensure explanation of your code (1).  Open a file (file name must include your student number, thus filexxxxxxxx.txt). (1).  Write the details of the five students (from Question 1 above) to a file (3).  Close the file. (1)  Then open the file again...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT