Please complete these without infinite loops /** * * @param list * @param target * @return the SECOND index at which tar

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Please complete these without infinite loops /** * * @param list * @param target * @return the SECOND index at which tar

Post by answerhappygod »

Please complete these without infinite loops
/**
*
* @param list
* @param target
* @return the SECOND index at which target
exists in the list passed.
* return -1 if list is null or target doesn't exist TWICE in the
list.
*/
public static int secondIndexOf(ArrayList<Integer> list,
int target) {
return 0;
}
/**
*
* @param list
* @param target
* @return the SECOND-LAST index at which target
exists in the list passed.
* return -1 if list is null or target doesn't exist TWICE in the
list.
*/
public static int secondLastIndexOf(ArrayList<Integer>
list, int target) {
return 0; //to be completed
}
/**
*
* @param list
* @return a list containing all the positive
items from the list passed.
* return null if list is null
*/
public static ArrayList<Integer>
getPositives(ArrayList<Integer> list) {
return null; //to be completed
}
/**
* Remove all negative items from the list
* @param list
*
* For example,
*
* if list = [2, 4, 0, -1, -2, -3, 0, 4, -8, 3, 0, -1, -1],
* it should become [2, 4, 0, 0, 4, 3, 0]
*
* TIP: use remove method and use the debugger to ensure that the
right item is being removed
*/
public static void removeNegatives(ArrayList<Integer>
list) {
//to be completed
}
/**
*
* @param list
* @return true if the list contains two
instances of target in a row, false otherwise.
* return false if list is null or if it contains less than 2
items
*/
public static boolean twoInARow(ArrayList<Integer> list,
int target) {
return false; //to be completed
}
public class Stage3Test {
public ArrayList<Integer> nullList, emptyList,
singleItemList, list1, list2;
public ArrayList<Integer> multiplesTen1, multiplesTen2,
reverseMultiplesTen, mixedPosNeg, allZeroes1, allZeroes2;
@BeforeEach
public void run() {
nullList = null;
emptyList = new ArrayList<Integer>();
singleItemList = new
ArrayList<Integer>(Arrays.asList(777));
list1 = new
ArrayList<Integer>(Arrays.asList(10,70,20,90));
list2 = new ArrayList<Integer>(Arrays.asList(-5,
0, 8, -7, 9, 15, 23, -1, 5));
multiplesTen1 = new
ArrayList<Integer>(Arrays.asList(10, 70, 20,
90));
multiplesTen2 = new
ArrayList<Integer>(Arrays.asList(10, 70, 20,
90));
reverseMultiplesTen = new
ArrayList<Integer>(Arrays.asList(90, 20, 70,
10));
mixedPosNeg = new
ArrayList<Integer>(Arrays.asList(-50, 30, -20, 0,
20, -30, 50));
allZeroes1 = new
ArrayList<Integer>(Arrays.asList(0, 0, 0, 0,
0));
allZeroes2 = new
ArrayList<Integer>(Arrays.asList(0, 0, 0, 0,
0));
}
@Test
void testSecondIndexOf() {
assertEquals(-1, Stage3.secondIndexOf(null,
65));
assertEquals(-1,
Stage3.secondIndexOf(emptyList, 96));
assertEquals(-1,
Stage3.secondIndexOf(singleItemList, -7));
assertEquals(-1,
Stage3.secondIndexOf(singleItemList, 1));
assertEquals(-1,
Stage3.secondIndexOf(multiplesTen1, 10));
assertEquals(-1,
Stage3.secondIndexOf(multiplesTen1, 70));
assertEquals(-1,
Stage3.secondIndexOf(multiplesTen1, 20));
assertEquals(-1,
Stage3.secondIndexOf(multiplesTen1, 90));
assertEquals(1,
Stage3.secondIndexOf(allZeroes1, 0));
ArrayList<Integer> temp = new
ArrayList<Integer>(Arrays.asList(10,70,20,90,70,30,10,10,10,70));
assertEquals(4, Stage3.secondIndexOf(temp,
70));
assertEquals(6, Stage3.secondIndexOf(temp,
10));
}
@Test
void testSecondLastIndexOf() {
assertEquals(-1,
Stage3.secondLastIndexOf(null, 65));
assertEquals(-1,
Stage3.secondLastIndexOf(emptyList, 96));
assertEquals(-1,
Stage3.secondLastIndexOf(singleItemList, -7));
assertEquals(-1,
Stage3.secondLastIndexOf(singleItemList, 1));
assertEquals(-1,
Stage3.secondLastIndexOf(multiplesTen1, 10));
assertEquals(-1,
Stage3.secondLastIndexOf(multiplesTen1, 70));
assertEquals(-1,
Stage3.secondLastIndexOf(multiplesTen1, 20));
assertEquals(-1,
Stage3.secondLastIndexOf(multiplesTen1, 90));
assertEquals(3,
Stage3.secondLastIndexOf(allZeroes1, 0));
ArrayList<Integer> temp = new
ArrayList<Integer>(Arrays.asList(10,70,20,90,70,30,10,10,10,70));
assertEquals(4, Stage3.secondLastIndexOf(temp,
70));
assertEquals(7, Stage3.secondLastIndexOf(temp,
10));
}
@Test
void testGetPositives() {
assertNotNull(Stage3.getPositives(list1));
assertEquals("[10, 70, 20, 90]",
Stage3.getPositives(list1).toString());
//to check that you are not modifying the list passed
assertNotNull(Stage3.getPositives(list1));
assertEquals("[10, 70, 20, 90]",
Stage3.getPositives(list1).toString());
assertNotNull(Stage3.getPositives(list2).toString());
assertEquals("[8, 9, 15, 23, 5]",
Stage3.getPositives(list2).toString());
assertNotNull(Stage3.getPositives(singleItemList).toString());
assertEquals("[777]",
Stage3.getPositives(singleItemList).toString());
assertNotNull(Stage3.getPositives(emptyList).toString());
assertEquals("[]",
Stage3.getPositives(emptyList).toString());
assertNull(Stage3.getPositives(nullList));
}
@Test
void testRemoveNegatives() {
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
list2.add(-6);
//a whole lot of -6s at the end
Stage3.removeNegatives(list2);
assertEquals("[0, 8, 9, 15, 23, 5]",
list2.toString());
Stage3.removeNegatives(singleItemList);
assertEquals("[777]", singleItemList.toString());
}
@Test
void testTwoInARow() {
assertFalse(Stage3.twoInARow(list1, 5));
assertFalse(Stage3.twoInARow(list1, 10));
assertFalse(Stage3.twoInARow(list1, 70));
assertFalse(Stage3.twoInARow(list1, 20));
assertFalse(Stage3.twoInARow(list1, 90));
list1 = new
ArrayList<Integer>(Arrays.asList(10,70,5,5));
assertTrue(Stage3.twoInARow(list1, 5));
list1 = new
ArrayList<Integer>(Arrays.asList(5,5,8,3));
assertTrue(Stage3.twoInARow(list1, 5));
list1 = new
ArrayList<Integer>(Arrays.asList(1,2,3,4,5,5,4,3,2,1));
assertTrue(Stage3.twoInARow(list1, 5));
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply