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
ArrayList
---------------------------------------------------------------------------
public class Playlist
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
@Override public ArrayList
@Override public ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
