Question

In: Computer Science

Complete the required methods: public class SongList { // instance variables private Song m_last; private int...

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

Solutions

Expert Solution

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

Related Solutions

public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
In Java The Order class should have:  Seven instance variables: the order number (an int),...
In Java The Order class should have:  Seven instance variables: the order number (an int), the Customer who made the order, the Restaurant that receives the order, the FoodApp through which the order was placed, a list of food items ordered (stored as an array of FoodItem), the total number of items ordered (an int), and the total price of the order (a double).  A class constant to set the maximum number of items that can be ordered...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT