Question: Java: Finish all unfinished methods in Playlist.java. Implement an ARRAY that stores the data, and will mak use of a method to expand it if

Java:

Finish all unfinished methods in Playlist.java. Implement an ARRAY that stores the data, and will mak use of a method to expand it if it reaches max capacity. You must test the Playlist by writing Junit test cases.

These must be implemented in all unimplemeneted methods:

boolean add(E element)

void expandCapacity()

E shufflePlay()

void clearPlayList()

int size()

boolean isEmpty()

ArrayList createSmartPlayList(E song)

ArrayList getAllSongsonAlbum(String albumName)

---------------------------------------------------------------------------

public class Playlist implements PlaylistADT { private E[] playlist; private int size; private final int DEFAULT_CAPACITY = 10;

public Playlist() { this.playlist = (E[]) new Object[DEFAULT_CAPACITY]; size = 0; }

@Override public boolean add(E element) { throw new UnsupportedOperationException("Not supported yet."); } /** * Copies and doubles the size of the playlist array. */ private void expandCapacity() { throw new UnsupportedOperationException("Not supported yet."); }

@Override public boolean remove(E element) { //if the playlist array is empty, we can't remove anything, return false if (this.isEmpty()) { return false; } //if the element is null, we will get a null pointer exception, return false if (element == null) { return false; } //if the song we want to remove isn't in the array, we can't remove it, //so return false. if (!this.contains(element)) { return false; } //get the index of element we wish to remove. int index = this.search(element); //swap the last element in the list with the element we wish to remove. playlist[index] = playlist[size - 1]; //set the last element in the list to null playlist[size - 1] = null; //decrement size counter size--; //return true return true; }

@Override public E shufflePlay() { throw new UnsupportedOperationException("Not supported yet."); }

@Override public E play(int index) { //if the playlist array is empty, return null. if (this.isEmpty()) { return null; } //check to make sure the passed-in index is valid if (index < 0 || index >= size) { return null; } //return the element at the passed-in index return playlist[index]; }

@Override public int search(E element) { throw new UnsupportedOperationException("Not supported yet."); }

@Override public boolean contains(E element) { //make sure the array isn't empty and the passed in element is valid if (!this.isEmpty() && element != null) { //loop through all the songs in the playlist array for (int i = 0; i < size; i++) { //if the passed-in song matches a song in the array, return true if (playlist[i].equals(element)) { return true; } } } //otherwise, return false. return false; }

@Override public void clearPlaylist() { throw new UnsupportedOperationException("Not supported yet."); }

@Override public int size() { throw new UnsupportedOperationException("Not supported yet."); }

@Override public boolean isEmpty() { throw new UnsupportedOperationException("Not supported yet."); }

@Override public ArrayList createSmartPlaylist(E song) { throw new UnsupportedOperationException("Not supported yet."); }

@Override public ArrayList getAllSongsByArtist(String artistName) { //if the playlist array is empty, return null if (this.isEmpty()) { return null; } //if the passed-in artist name is null, return null if (artistName == null) { return null; } //create an ArrayList to hold songs - we need the dynamic size changing //capabilities for this because we don't know how many songs we have //by the same artist. ArrayList songs = new ArrayList<>(); //loop through the array for (int i = 0; i < size; i++) { //please note the type cast here - the parantheses are important!! - //we can't use methods of the Song class without the cast. if (((Song) playlist[i]).getArtist().equals(artistName)) { songs.add(playlist[i]); } } return songs; }

@Override public ArrayList getAllSongsOnAlbum(String albumName) { throw new UnsupportedOperationException("Not supported yet."); } @Override public String toString() { String s = "Playlist: { "; for (int i = 0; i < size; i++) { s += i + 1 + ". "; s += playlist[i].toString(); s += " "; } s += "}"; return s; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!