In: Computer Science
What is the issue is in the add method in the SongList Class? It doesn't seem to add the songs when running testing.
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
Song newSong = new Song(artist, title);
if(m_last == null)
{
m_last = newSong;
m_numElements++;
return;
}
Song previous = null;
Song current = m_last;
while (current != null)
{
System.out.print(" [" + current.getArtist() + " - " +
current.getTitle() + "]\n");
int compareToResult =
(current.getTitle()).compareToIgnoreCase(title);
  
if (compareToResult < 0)
{
previous = current;
current = current.getLink();
}
else
break;
}
System.out.print(" [" + current.getArtist() + " - " +
current.getTitle() + "]\n");
if (previous == null)
{
newSong.setLink(m_last);
m_last = newSong;
m_numElements++;
}
else
{
//adds element to a non-empty list
previous.setLink(newSong);
newSong.setLink(current);
}
}
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;
}
}
Summary:
CirculerLinkedList is a linked list whose head is connected to tail.
Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end.
I fixed your SongList class, it was not storing any values because in constructor you did not initialize the instance variables properly. You must use this keyword inside your constructor. This refers to the object that is calling the method/constructor.
Another issue with your implementation was toString method, it was throwing error while printing songs.
It was failing because while condition was wrong. I fixed that as well.
But there is a problem with your implementation in circular linked there should not be any null at the end. In your implementation you are adding null to the end that is to the link of the song.
Anyways if you just want your code to work then here is your solution:
------------------------------------------Song.java----------------------------------
public class Song
{
    // instance variables
    private String m_artist;
    private String m_title;
    private Song m_link;
    Song(){
        this.m_artist = null;
        this.m_title = null;
        this.m_link = null;
    }
    // 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;
    }
}
----------------------------------------------SongList.java------------------------------------
public class SongList {
    // instance variables
    private Song m_last;
    private static int m_numElements;
    public SongList() {
        this.m_last = null;
        this.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
        Song newSong = new Song(artist, title);
        if (m_last == null) {
            m_last = newSong;
            m_numElements++;
            return;
        }
        Song previous = null;
        Song current = m_last;
        while (current != null) {
            //System.out.print(" [" + current.getArtist() + " - " + current.getTitle() + "]\n");
            int compareToResult = (current.getTitle()).compareToIgnoreCase(title);
            if (compareToResult < 0) {
                previous = current;
                current = current.getLink();
            } else
                break;
        }
        //System.out.print(" [" + current.getArtist() + " - " + current.getTitle() + "]\n");
        if (previous == null) {
            newSong.setLink(m_last);
            m_last = newSong;
            m_numElements++;
        } else {
//adds element to a non-empty list
            previous.setLink(newSong);
            newSong.setLink(current);
            m_numElements++;
        }
    }
    @Override
    public String toString() {
        String listContent = "";
        Song current = this.m_last;
        if (this.m_last != null)
            do {
                listContent += " [" + current.getArtist() + " - " + current.getTitle() + "]\n";
                current = current.getLink();
            } while (current != null);
        return listContent;
    }
}
------------------------------------------Driver.java---------------------------------------
import javax.sound.midi.Soundbank;
public class Driver {
    public static void main(String args[]) {
        // You implementation of CircularLinkedList
        SongList songList = new SongList();
        songList.add("Eminem", "Rap God");
        songList.add("Katy Perry", "Roar");
        System.out.println("Number of songs in list: ");
        System.out.println(songList.size());
        System.out.println("Songs in the list: ");
        System.out.println(songList.toString());
        // Correct implementation of CircularLinkedList. Please remove below comments to test my implementation.
        
        /*CircularLinkedList circularLinkedList = new CircularLinkedList();
        circularLinkedList.addNode("Eminem", "Rap God");
        circularLinkedList.addNode("Katy Perry", "Roar");*/
    }
}
Output:

If you want correct implentaion of Circular Linked List you can
refer my code:
-----------------------------------------CircularLinkedList.java----------------------------
public class CircularLinkedList {
    // head of the circularlinked list
    private Songs head = null;
    // tail of the circularlinked list
    private Songs tail = null;
    // function to add nodes to the tail of the circular linked list
    public void addNode(String str, String title) {
        Songs newNode = new Songs(str,title);
        if (head == null) {
            head = newNode;
        } else {
            tail.nextNode = newNode;
        }
        tail = newNode;
        tail.nextNode = head;
    }
    // function to traverse the list.
    public void traverseList() {
        Songs currentNode = head;
        if (head != null) {
            do {
                // print nodes here
                currentNode = currentNode.nextNode;
            } while (currentNode != head);
        }
    }
}
class Songs {
    // instance variables
     String m_artist;
     String m_title;
    Songs nextNode;
    Songs(){
        this.m_artist = null;
        this.m_title = null;
        this.nextNode = null;
    }
    // constructor
    public Songs(String artist, String title)
    {
        m_artist = artist;
        m_title = title;
        nextNode = 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(Songs link)
    {
        nextNode = link;
    }
    public Songs getLink()
    {
        return nextNode;
    }
}
Please rate postive if this helped you. Thanks.