Question
Need some help coding this extended array list. This file describes a class that extends (that is, subclasses) ArrayList, adding several instance methods. Implement each
Need some help coding this extended array list. This file describes a class that extends (that is, subclasses) ArrayList, adding several instance methods. Implement each method as described. Thank You!
package list.exercises;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("serial")
public class ExtendedArrayList
/**
* 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
return null;
}
}
ExtendedArrayList.test
/* * Copyright 2017 Marc Liberatore. */
package list.exercises;
import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout;
public class ExtendedArrayListTest {
// Uncomment these two lines if you want to catch infinite loops // @Rule // public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds
ExtendedArrayList
/** * each of the ExtendedArrayLists declared above are reset before each test */ @Before public void setup() { empty = new ExtendedArrayList
@Test public void testCountOne() { assertEquals(1, one.count(1)); }
@Test public void testCountOneFirst() { assertEquals(1, oneTwo.count(1)); }
@Test public void testCountOneTwoLast() { assertEquals(1, oneTwo.count(2)); } @Test public void testCountTwo() { assertEquals(2, names.count("John")); }
@Test public void testCountNone() { assertEquals(0, names.count("Marc")); } @Test public void testRotateRightEmpty() { empty.rotateRight(1); assertEquals(Arrays.asList(), empty); } @Test public void testRotateRightOne() { one.rotateRight(1); assertEquals(Arrays.asList(1), one); one.rotateRight(2); assertEquals(Arrays.asList(1), one); }
@Test public void testRotateRightOneTwo() { oneTwo.rotateRight(1); assertEquals(Arrays.asList(2,1), oneTwo); oneTwo.rotateRight(1); assertEquals(Arrays.asList(1,2), oneTwo); oneTwo.rotateRight(2); assertEquals(Arrays.asList(1,2), oneTwo); } @Test public void testRotateRightNames() { names.rotateRight(6); assertEquals(Arrays.asList("Jingleheimer","Suzanne","John","Patrice", "Washington","Kelly","John","Justine"), names); }
@Test public void testRotateRightNamesFurther() { names.rotateRight(14); assertEquals(Arrays.asList("Jingleheimer","Suzanne","John","Patrice", "Washington","Kelly","John","Justine"), names); }
@Test public void testIntersperseEmpty() { empty.intersperse(0); assertEquals(Arrays.asList(), empty); }
@Test public void testIntersperseOne() { one.intersperse(0); assertEquals(Arrays.asList(1), one); }
@Test public void testIntersperseOneTwo() { oneTwo.intersperse(0); assertEquals(Arrays.asList(1, 0, 2), oneTwo); } @Test public void testIntersperseNames() { names.intersperse("yeah"); assertEquals(Arrays.asList("John","yeah","Justine","yeah","Jingleheimer","yeah","Suzanne", "yeah","John","yeah","Patrice","yeah","Washington","yeah","Kelly"), names); }
@Test public void testReversedEmpty() { assertEquals(Arrays.asList(), empty.reversed()); }
@Test public void testReversedOne() { assertEquals(Arrays.asList(1), one.reversed()); }
@Test public void testReversedTwo() { assertEquals(Arrays.asList(2,1), oneTwo.reversed()); } @Test public void testReversedNames() { assertEquals(Arrays.asList("Kelly", "Washington", "Patrice", "John", "Suzanne", "Jingleheimer", "Justine", "John"), names.reversed()); }
@Test public void testReversedParameterUnchanged() { List
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