Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Must complete all methods. package list.exercises; import java.util.ArrayList; i

ID: 3888797 • Letter: M

Question

Must complete all methods.

package list.exercises;

import java.util.ArrayList;

import java.util.List;

@SuppressWarnings("serial")

public class ExtendedArrayList extends ArrayList {

/**

* Counts the number of elements in this list that are equal to e.

*

* Uses .equals to check for equality.

*

* @param e the element to count

* @return the number of elements equal to e

*/

public int count(E e) {

return 0;

}

/**

* Rotates the list right n places.

*

* Rotating a list right means moving the last item to the front of the list:

*

* 1, 2, 3, 4, 5 when rotated right once becomes 5, 1, 2, 3, 4

*

* A list is rotated right two places as follows:

*

* 4, 5, 1, 2, 3

*

* and so on.

*

* @param n the distance to rotate the list right

*/

public void rotateRight(int n) {

}

/**

* Intersperses e between each existing element of the list.

*

* For example, given the list: "hey", "ho", "hi", if we intersperse "yo" we get:

* "hey", "yo", "ho", "yo", "hi"

*

* @param e the element to intersperse

*/

public void intersperse(E e) {

}

/**

* Returns a copy of this list in reverse order.

*

* This list is unmodified by this operation.

*

* @return a reversed copy of the list

*/

public List reversed() {

return null;

}

}

Explanation / Answer

Given below is the completed implementation. Provided a test program to make sure the implementation is correct. Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.


package list.exercises;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("serial")
public class ExtendedArrayList<E> extends ArrayList<E> {
/**
* Counts the number of elements in this list that are equal to e.
*
* Uses .equals to check for equality.
*
* @param e the element to count
* @return the number of elements equal to e
*/
public int count(E e) {
int total = 0;
//iterate over the elements
for(int i = 0; i < size(); i++)
{
if(get(i).equals(e)) //if there is a match
total++;
}
return total;
}
/**
* Rotates the list right n places.
*
* Rotating a list right means moving the last item to the front of the list:
*
* 1, 2, 3, 4, 5 when rotated right once becomes 5, 1, 2, 3, 4
*
* A list is rotated right two places as follows:
*
* 4, 5, 1, 2, 3
*
* and so on.
*
* @param n the distance to rotate the list right
*/
public void rotateRight(int n) {
if(size() <= 1)//nothing to do if the list is empty or has just 1 element
return;
for(int times = 1; times <= n; times++) //do as many rotations as needed
{
E last = remove(size()-1); //fetch and remove the last element
add(0, last); //add the last element to the 0th index
}
}
/**
* Intersperses e between each existing element of the list.
*
* For example, given the list: "hey", "ho", "hi", if we intersperse "yo" we get:
* "hey", "yo", "ho", "yo", "hi"
*
* @param e the element to intersperse
*/
public void intersperse(E e) {
for(int i = 1; i < size(); i+= 2) //increment i by 2 so we insert in alternate locations
{
add(i, e);
}
}
/**
* Returns a copy of this list in reverse order.
*
* This list is unmodified by this operation.
*
* @return a reversed copy of the list
*/
public List reversed() {
ArrayList<E> rev = new ArrayList<E>();
for(int i = size() - 1; i >= 0; i--) //iterate from last to first element
rev.add(get(i));
return rev;
}
}

TestExtendedArray.java


package list.exercises;
import java.util.Arrays;
public class TestExtendedArray {
public static void main(String[] args) {
ExtendedArrayList<Integer> list1 = new ExtendedArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
System.out.println("list1");
System.out.println(Arrays.toString(list1.toArray()));
System.out.println("reverse of list1");
System.out.println(Arrays.toString(list1.reversed().toArray()));
System.out.println("list1 before rotation");
System.out.println(Arrays.toString(list1.toArray()));
//make 2 right rotations
list1.rotateRight(2);
System.out.println("list1 after 2 right rotation");
System.out.println(Arrays.toString(list1.toArray()));
list1.intersperse(10);
System.out.println("list1 intersperse 10");
System.out.println(Arrays.toString(list1.toArray()));
System.out.println("Count of 10 in list1 = " + list1.count(10));
}
}

output

list1
[1, 2, 3, 4, 5]
reverse of list1
[5, 4, 3, 2, 1]
list1 before rotation
[1, 2, 3, 4, 5]
list1 after 2 right rotation
[4, 5, 1, 2, 3]
list1 intersperse 10
[4, 10, 5, 10, 1, 10, 2, 10, 3]
Count of 10 in list1 = 4