commit
ad13ae19c6
3 changed files with 501 additions and 0 deletions
-
52Song.java
-
74SongList.java
-
375TestSongList.java
@ -0,0 +1,52 @@ |
|||||
|
// The Song class that represents a song |
||||
|
// Do not make any changes to this file! |
||||
|
// Xiwei Wang |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,74 @@ |
|||||
|
// The SongList class that represents a circular linked list of Song nodes |
||||
|
// Your name here |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,375 @@ |
|||||
|
// Test driver for the SongList class |
||||
|
// Do not make any changes to this file! |
||||
|
// Xiwei Wang |
||||
|
|
||||
|
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"); |
||||
|
|
||||
|
// Test 4 |
||||
|
numTotalTests++; |
||||
|
bReturn = false; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
bReturn = mylist.remove("Ava Max", "Sweet But Psycho"); |
||||
|
|
||||
|
if (bReturn == false) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Ava Max\", \"Sweet But Psycho\")\n Expected: false" ); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Yours: " + bReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
// Test 5 |
||||
|
numTotalTests++; |
||||
|
iReturn = -1; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
mylist.add("Ava Max", "Sweet But Psycho"); |
||||
|
mylist.add("Billie Eilish", "Bad Guy"); |
||||
|
mylist.add("Mabel", "Don't Call Me Up"); |
||||
|
mylist.add("Lewis Capaldi", "Hold Me While You Wait"); |
||||
|
iReturn = mylist.size(); |
||||
|
|
||||
|
if (iReturn == 7) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult + "\n add(\"Ava Max\", \"Sweet But Psycho\")\n add(\"Billie Eilish\", \"Bad Guy\")\n add(\"Mabel\", \"Don't Call Me Up\")\n add(\"Lewis Capaldi\", \"Hold Me While You Wait\")\n Expected return of size(): 7" ); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Your size() returns: " + iReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
System.out.println(mylist); |
||||
|
|
||||
|
// Test 6 |
||||
|
numTotalTests++; |
||||
|
bReturn = false; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
bReturn = mylist.remove("Billie Eilish", "Bad Guy"); |
||||
|
|
||||
|
if (bReturn == true) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Billie Eilish\", \"Bad Guy\")\n Expected: true" ); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Yours: " + bReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
// Test 7 |
||||
|
numTotalTests++; |
||||
|
bReturn = false; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
bReturn = mylist.remove("Ava Max", "Sweet But Psycho"); |
||||
|
|
||||
|
if (bReturn == true) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Ava Max\", \"Sweet But Psycho\")\n Expected: true" ); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Yours: " + bReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
// Test 8 |
||||
|
numTotalTests++; |
||||
|
sReturn = ""; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
mylist.add("Billie Eilish", "Bury A Friend"); |
||||
|
mylist.add("Lewis Capaldi", "Grace"); |
||||
|
sReturn = mylist.toString(); |
||||
|
|
||||
|
if (sReturn.equals(" [Billie Eilish - Bury A Friend]\n [Mabel - Don't Call Me Up]\n [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\n [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 add(\"Billie Eilish\", \"Bury A Friend\")\n add(\"Lewis Capaldi\", \"Grace\")\n Expected return of toString():\n" |
||||
|
+ " [Billie Eilish - Bury A Friend]\n [Mabel - Don't Call Me Up]\n [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\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"); |
||||
|
|
||||
|
// Test 9 |
||||
|
numTotalTests++; |
||||
|
bReturn = false; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
bReturn = mylist.remove("Mabel", "Don't Call Me Up"); |
||||
|
|
||||
|
if (bReturn == true) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult +"\n remove(\"Mabel\", \"Don't Call Me Up\")\n Expected: true" ); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Yours: " + bReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
// Test 10 |
||||
|
numTotalTests++; |
||||
|
iReturn = -1; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
iReturn = mylist.size(); |
||||
|
|
||||
|
if (iReturn == 6) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult + "\n Expected return of size(): 6" ); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Your size() returns: " + iReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
// Test 11 |
||||
|
numTotalTests++; |
||||
|
builtlist = null; |
||||
|
sReturn = ""; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
builtlist = mylist.buildList("Lewis Capaldi"); |
||||
|
sReturn = builtlist.toString(); |
||||
|
|
||||
|
if (sReturn.equals(" [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\n [Lewis Capaldi - Someone You Loved]\n")) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult + "\n mylist.buildList(\"Lewis Capaldi\")\n Expected return of toString() on the new list:\n" |
||||
|
+ " [Lewis Capaldi - Grace]\n [Lewis Capaldi - Hold Me While You Wait]\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"); |
||||
|
|
||||
|
// Test 12 |
||||
|
numTotalTests++; |
||||
|
builtlist = null; |
||||
|
sReturn = ""; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
builtlist = mylist.buildList("Lil Nas X"); |
||||
|
sReturn = builtlist.toString(); |
||||
|
|
||||
|
if (sReturn.equals(" [Lil Nas X - Old Town Road]\n")) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult + "\n mylist.buildList(\"Lil Nas X\")\n Expected return of toString() on the new list:\n" |
||||
|
+ " [Lil Nas X - Old Town Road]\n"); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Your toString() returns:\n" + sReturn + "\n"); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
// Test 13 |
||||
|
numTotalTests++; |
||||
|
builtlist = null; |
||||
|
sReturn = ""; |
||||
|
testResult = "[Failed]"; |
||||
|
eMsg = "N/A"; |
||||
|
try |
||||
|
{ |
||||
|
builtlist = mylist.buildList("Ava Max"); |
||||
|
sReturn = builtlist.toString(); |
||||
|
|
||||
|
if (sReturn.equals("")) |
||||
|
{ |
||||
|
numPassedTests++; |
||||
|
testResult = "[Passed]"; |
||||
|
} |
||||
|
} |
||||
|
catch (RuntimeException e) |
||||
|
{ |
||||
|
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
||||
|
} |
||||
|
|
||||
|
System.out.println("Test " + numTotalTests + ": " + testResult + "\n mylist.buildList(\"Ava Max\")\n Expected return of toString() on the new list:\n An empty list\n"); |
||||
|
if (eMsg.equals("N/A")) |
||||
|
System.out.println(" Your toString() returns:\n" + (sReturn.equals("") ? " An empty list\n" : sReturn)); |
||||
|
else |
||||
|
System.out.println(" Yours: " + eMsg + "\n"); |
||||
|
|
||||
|
System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests)); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue