In: Computer Science
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;
}
}
// 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;
}
}
Program Code Screenshot (SongList.java)
Program Code Screenshot (Main.java) for testing
Program Sample Input/Output Screenshot
Program Code to Copy (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) {
// check for first insertion
if (m_last == null) {
m_last = new Song(artist, title);
m_last.setLink(m_last);
m_numElements++;
} else {
// find sorted position of insertion
Song temp = m_last;
while (!temp.getLink().equals(m_last) && temp.getLink().getTitle().compareTo(title) < 0) {
temp = temp.getLink();
}
// create element
Song newSong = new Song(artist, title);
// insert after temp
newSong.setLink(temp.getLink());
temp.setLink(newSong);
m_last=temp.getLink();
// update number of elements
m_numElements++;
}
}
// 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) {
//check if it is the m_last element
if(m_last.getTitle().equals(title) && m_last.getArtist().equals(artist)){
Song temp = m_last;
// if m_las is the only element
if(temp.getLink()==m_last){
m_last=null;
}else{
//get new m_last
while(!temp.getLink().equals(m_last)){
temp=temp.getLink();
}
//update temp->link
temp.setLink(m_last.getLink());
}
m_numElements--;
return true;
}
//search for the element to remove
Song temp = m_last;
while(!temp.getLink().equals(m_last) && !(temp.getLink().getArtist().equals(artist) && temp.getLink().getTitle().equals(title))){
temp=temp.getLink();
}
System.out.println(temp.getTitle());
if(temp.getLink().equals(m_last)){
return false;
}else{
temp.setLink(temp.getLink().getLink());
m_numElements--;
return true;
}
}
// build and return a circular linked list that contains all songs from the
// given artist
public SongList buildList(String artist) {
// create a list
SongList list = new SongList();
// check if no element in list
if(m_last==null){
return list;
}
//check if we should add m_last
if(m_last.getArtist().equals(artist)){
list.add(artist, m_last.getTitle());
}
//keep searching in our list and add in new list if artist matches
Song temp = m_last.getLink();
while(!temp.equals(m_last)){
if(temp.getArtist().equals(artist)){
list.add(artist, temp.getTitle());
}
temp=temp.getLink();
}
return list;
}
// 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;
}
}
Program Code to Copy (Main.java)
class Main {
public static void main(String[] args) {
//test SongList
SongList list = new SongList();
//test add
list.add("Ed Sheeran", "Shape of You");
list.add("Juston Bieber", "Holy");
list.add("Ed Sheeran", "Perfect");
list.add("Ed Sheeran", "I Don't Care");
list.add("Ludacris", "Love Me");
System.out.println("List after addition : \n"+list);
//test delete
list.remove("Ed Sheeran", "Perfect");
System.out.println("List after removing (Ed Sheeran, Perfect) : \n"+list);
//test building list
list = list.buildList("Juston Bieber");
System.out.println("List built for artist Juston Bieber : \n"+list);
}
}