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 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 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...
(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...
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
(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?
 (a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class? (c) What is the effect of declaring instance variables and methods public or private? (d) Why do we often declare the instance variables of classes private? (e) Could we declare methods private? Would we want to do so?  (f) What does the identifier this mean? Give an example of its use (g) What is a constructor? (h) What is inheritance? Why is...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT