Question
JUnit test the following methods. @Override public boolean add(E element) { //if the element is null, return false if (element == null) { return false;
JUnit test the following methods.
@Override public boolean add(E element) { //if the element is null, return false if (element == null) { return false; } if (this.playlist.length > size) { //add element playlist[size] = element; //increment size counter size++; //return true return true; } else { //expand arraay this.expandCapacity(); //add element playlist[size] = element; //increment size counter size++; //return true return true; } } /** * Copies and doubles the size of the playlist array. */ private void expandCapacity() { //save array E[] firstPlaylist = this.playlist; //create array of double size this.playlist = (E[]) new Object[2 * firstPlaylist.length]; //fill values System.arraycopy(firstPlaylist, 0, this.playlist, 0, +firstPlaylist.length); }
@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() { //create a Random object Random rand = new Random(); //generate a random number within playlist int index = rand.nextInt(this.playlist.length); //store the song in temp E temp = playlist[index]; int i; for (i = index ; i < playlist.length - 1 ; i++) playlist[i] = playlist[i + 1]; //decrement size counter this.size--; //return temp return temp; }
@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]; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started