// This is a Java Data Structures Problem DO NOT EDIT Song.java and TestSongList.java. You are only allowed to make changes to SongList.java. You are provided with the files "Song.java" and...


// This is a Java Data Structures Problem


DO NOT EDIT Song.java and TestSongList.java. You are only allowed to make changes to SongList.java.


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. remove(String artist, String title) – 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. Note that you are only supposed to touch the above methods. You are NOT allowed to create any other methods, instance variables, or make any changes to methods other than these four methods or files other than "SongList.java".


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


// You're allowed to touch this file:


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


// Do not touch this file:


public class TestSongList
{
public static void main(String[] args)
{
SongList mylist = new SongList();
SongList builtlist = null;
int numPassedTests = 0;
int numTotalTests = 0;
String testResult;


// Test 1
numTotalTests++;
boolean bReturn = false;
testResult = "[Failed]";
String eMsg = "N/A";
try
{
mylist.add("Lewis Capaldi", "Someone You Loved");
bReturn = mylist.isEmpty();
if (bReturn == false)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": " + testResult + "\n add(\"Lewis Capaldi\", \"Someone You Loved\")\n Expected return of isEmpty(): false" );
if (eMsg.equals("N/A"))
System.out.println(" Your isEmpty() returns: " + bReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 2
numTotalTests++;
int iReturn = -1;
testResult = "[Failed]";
eMsg = "N/A";
try
{
mylist.add("Lil Nas X", "Old Town Road");
mylist.add("Ed Sheeran & Justin Bieber", "I Don't Care");
iReturn = mylist.size();
if (iReturn == 3)
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": " + testResult + "\n add(\"Lil Nas X\", \"Old Town Road\")\n add(\"Ed Sheeran & Justin Bieber\", \"I Don't Care\")\n Expected return of size(): 3" );
if (eMsg.equals("N/A"))
System.out.println(" Your size() returns: " + iReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");
// Test 3
numTotalTests++;
String sReturn = "";
testResult = "[Failed]";
eMsg = "N/A";
try
{
sReturn = mylist.toString();
if (sReturn.equals(" [Ed Sheeran & Justin Bieber - I Don't Care]\n [Lil Nas X - Old Town Road]\n [Lewis Capaldi - Someone You Loved]\n"))
{
numPassedTests++;
testResult = "[Passed]";
}
}
catch (RuntimeException e)
{
eMsg = "RuntimeException - \"" + e.getMessage() + "\"";
}
System.out.println("Test " + numTotalTests + ": " + testResult + "\n Expected return of toString():\n [Ed Sheeran & Justin Bieber - I Don't Care]\n [Lil Nas X - Old Town Road]\n [Lewis Capaldi - Someone You Loved]\n");
if (eMsg.equals("N/A"))
System.out.println(" Your toString() returns:\n" + sReturn + "\n");
else
System.out.println(" Yours: " + eMsg + "\n");


}

Oct 28, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here