Question
Please help me, but any use of Arrays will not be accepted.Thank you so much! JUnit Test: package practicePackage._08_recursiveDataStructures.testsAttempts; import java.util.ArrayList; import serviceClasses.Graded; import java.lang.reflect.*;
Please help me, but any use of Arrays will not be accepted.Thank you so much! JUnit Test:
package practicePackage._08_recursiveDataStructures.testsAttempts;
import java.util.ArrayList; import serviceClasses.Graded; import java.lang.reflect.*; import java.io.IOException; import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;
import practicePackage._08_recursiveDataStructures.attempts.*;
public class MyLinkedListTest { public MyLinkedList emptyList, emptyList2; public MyLinkedList singleItemList, list1, list2, list3, list4;
public void ensureNoModify() { assertEquals("[]", emptyList.toString()); assertEquals("[10]", singleItemList.toString()); assertEquals("[12, 0, 3, -15, 7]", list1.toString()); assertEquals("[-8, 0, 0, 0, 0, 6, 18, 5, 12]", list2.toString()); } public static int score = 0; public static String result = ""; public static String currentMethodName = null; ArrayList
@BeforeEach public void run() { emptyList = new MyLinkedList(); emptyList2 = new MyLinkedList(); singleItemList = new MyLinkedList(); list1 = new MyLinkedList(); list2 = new MyLinkedList(); list3 = new MyLinkedList(); list4 = new MyLinkedList(); singleItemList.addAtFront(10);
list1.addAtFront(7); list1.addAtFront(-15); list1.addAtFront(3); list1.addAtFront(0); list1.addAtFront(12); //12 -> 0 -> 3 -> -15 -> 7
list2.addAtFront(12); list2.addAtFront(5); list2.addAtFront(18); list2.addAtFront(6); list2.addAtFront(0); list2.addAtFront(0); list2.addAtFront(0); list2.addAtFront(0); list2.addAtFront(-8); //-8 -> 0 -> 0 -> 0 -> 0 -> 6 -> 18 -> 5 -> 12 currentMethodName = null; }
@Test @Graded(description = "caninsertitemat", marks = 1) public void testCanInsertItemAt() { assertFalse(emptyList.canInsertItemAt(1)); assertFalse(singleItemList.canInsertItemAt(2)); assertFalse(list1.canInsertItemAt(-2)); assertFalse(list1.canInsertItemAt(6)); assertTrue(emptyList.canInsertItemAt(0)); assertTrue(list1.canInsertItemAt(5)); assertTrue(list2.canInsertItemAt(4));
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "totalpositives", marks = 1) public void testTotalPositives() { assertEquals(0, emptyList.totalPositives()); assertEquals(10, singleItemList.totalPositives()); assertEquals(22, list1.totalPositives()); assertEquals(41, list2.totalPositives());
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "countoccurrences", marks = 1) public void testCountOccurrences() { assertEquals(0, emptyList.countOccurrences(10)); assertEquals(0, singleItemList.countOccurrences(5)); assertEquals(1, singleItemList.countOccurrences(10)); assertEquals(1, list2.countOccurrences(-8)); assertEquals(1, list2.countOccurrences(12)); assertEquals(4, list2.countOccurrences(0));
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "alleven", marks = 1) public void testAllEven() { assertTrue(emptyList.allEven()); assertTrue(singleItemList.allEven()); assertFalse(list1.allEven()); assertFalse(list2.allEven());
ensureNoModify();
emptyList.addAtFront(-3); //emptyList is now [-3] assertFalse(emptyList.allEven()); singleItemList.addAtFront(12); //singleItemList is now [12, 10] assertTrue(singleItemList.allEven()); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "containsinrange", marks = 1) public void testContainsInRange() { assertFalse(emptyList.containsInRange(0, 10)); assertFalse(singleItemList.containsInRange(8, 9)); assertFalse(list1.containsInRange(13, 20)); assertFalse(list2.containsInRange(-18, -9)); assertTrue(singleItemList.containsInRange(10, 10)); assertTrue(list1.containsInRange(0, 5)); assertTrue(list2.containsInRange(18, 50)); assertTrue(list2.containsInRange(-8, -8));
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "highest", marks = 1) public void testHighest() { assertNull(emptyList.highest()); assertEquals((Integer)10, singleItemList.highest()); assertEquals((Integer)12, list1.highest()); assertEquals((Integer)18, list2.highest());
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "get", marks = 1) public void testGet() { assertNull(emptyList.get(0)); assertNull(singleItemList.get(1)); assertNull(list1.get(-1)); assertEquals((Integer)10, singleItemList.get(0)); assertEquals((Integer)7, list1.get(4)); assertEquals((Integer)6, list2.get(5)); assertEquals((Integer)0, list2.get(1));
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "remove", marks = 1) public void testRemove() { assertNull(emptyList.remove(0)); assertNull(singleItemList.remove(1)); assertNull(list1.remove(-2)); assertEquals((Integer)10, singleItemList.remove(0)); assertEquals("[]", singleItemList.toString()); assertEquals((Integer)7, list1.remove(4)); assertEquals("[12, 0, 3, -15]",list1.toString()); assertEquals((Integer)0, list2.remove(4)); assertEquals((Integer)6, list2.remove(4)); assertEquals("[-8, 0, 0, 0, 18, 5, 12]", list2.toString()); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "join", marks = 1) public void testJoin() { assertNotNull(emptyList.join(emptyList2)); assertEquals("[]", emptyList.join(emptyList2).toString()); assertNotNull(emptyList.join(singleItemList)); assertEquals("[10]", emptyList.join(singleItemList).toString()); assertNotNull(list1.join(list2)); assertEquals("[12, 0, 3, -15, 7, -8, 0, 0, 0, 0, 6, 18, 5, 12]", list1.join(list2).toString()); assertNotNull(list2.join(list1)); assertEquals("[-8, 0, 0, 0, 0, 6, 18, 5, 12, 12, 0, 3, -15, 7]", list2.join(list1).toString());
ensureNoModify(); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@Test @Graded(description = "same", marks = 1) public void testSame() { list3.addAtFront(-15); list3.addAtFront(7); list3.addAtFront(12); list3.addAtFront(0); list3.addAtFront(3); //list3 = -15 -> 7 -> 12 -> 0 -> 3
list4.addAtFront(3); list4.addAtFront(12); list4.addAtFront(7); list4.addAtFront(-15); list4.addAtFront(0); //0 -> -15 -> 7 -> 12 -> 3
assertTrue(emptyList.same(emptyList2)); assertFalse(emptyList.same(singleItemList)); assertFalse(list1.same(list2));
ensureNoModify();
emptyList.addAtFront(10); //emptyList is now [10] assertTrue(emptyList.same(singleItemList)); assertTrue(list1.same(list3)); //list1 = 12 -> 0 -> 3 -> -15 -> 7 //list3 = -15 -> 7 -> 12 -> 0 -> 3
assertTrue(list1.same(list4));
list4.addAtFront(2); //list4 is now [2, 0, -15, 7, 12, 3] assertFalse(list1.same(list4));
list3.remove(0); list3.addAtFront(8); //list3 is now [8, 0, 3, -15, 7] assertFalse(list1.same(list3)); currentMethodName = new Throwable().getStackTrace()[0].getMethodName(); }
@AfterEach public void logSuccess() throws NoSuchMethodException, SecurityException { if (currentMethodName != null && !methodsPassed.contains(currentMethodName)) { methodsPassed.add(currentMethodName); Method method = getClass().getMethod(currentMethodName); Graded graded = method.getAnnotation(Graded.class); score += graded.marks(); score = Math.min(score, 100); result += graded.description() + " passed. Marks awarded: " + graded.marks() + " "; } }
@AfterAll public static void wrapUp() throws IOException { if (result.length() != 0) { result = result.substring(0, result.length() - 1); // remove the last " " } System.out.println(result); System.out.println(" methods passed: " + score); } } Please help me with the code and make JUnit Test goes green. Thank you very much!
package practicePackage._08_recursiveDataStructures.attempts;
import practicePackage._08_recursiveDataStructures.Node;
public class MyLinkedList { public Node head;
//DO NOT MODIFY public MyLinkedList() { head = null; }
/** * DO NOT MODIFY * @return true if list is empty, false otherwise */ public boolean isEmpty() { return head == null; }
/** * DO NOT MODIFY * insert the value val at the beginning of the list * if the list = null before calling the method * and val = 50, then list should become [50] * after the method finishes execution * @param val */ public void addAtFront(int val) { Node temp = new Node(val, null); temp.next = head; head = temp; }
//DO NOT MODIFY public String toString() { String result = "["; Node current = head; while(current != null) { result = result + current.data + ", "; current = current.next; } if(result.length() > 1) { result = result.substring(0, result.length()-2); } return result + "]"; }
/** * DO NOT MODIFY - given as an example of traversal * @return number of items in the list */ public int size() { int count = 0; Node current = head; while(current != null) { count++; current = current.next; } return count; }
/** * * @param idx * @return true if an item can be added at the passed idx * * FOR EXAMPLE, * if list represents [10, 70, 20, 90], * items can be added at indices * 0 (before the first item), 1, 2, 3, 4 (after the last item) * but not at indices [... -2, -1] or [5, 6, ..] */ public boolean canInsertItemAt(int idx) { return false; //to be completed }
/** * * @return the sum of all positive items (0 if the list is empty) */ public int totalPositives() { return 0; //to be completed }
/** * @param target: item to count * @return the number of time target exists in the list * (0 if the list is empty) */ public int countOccurrences(int target) { return 0; //to be completed }
/** * * @return true if all items are even numbers, false otherwise * return true if list is empty * */ public boolean allEven() { return false; //to be completed }
/** * * @param low * @param high * @return true if ANY item is in the range [low...high] * false otherwise. * * return false if list is empty */ public boolean containsInRange(int low, int high) { return false; //to be completed }
/** * * @return the highest item in the list * return null if empty * list represents [10, 70, 20, 90], return 90 * head = null, return null * list represents [0, 0, 0, 0, return 0 */ public Integer highest() { return null; //to be completed }
/** * O(n) :( * that's why no one uses linked lists! * @param idx * @return item at given index if index is valid, null otherwise */ public Integer get(int idx) { return null; //to be completed }
/** * D level * Best case (idx = 0): O(1) * Worst case (idx = size()-1): O(n) * remove and return item at given idx if any * @param idx * @return item remove, null if idx invalid */ public Integer remove(int idx) { return null; //to be completed }
/** * * @param other * @return MyLinkedList object containing all * items of calling object followed by all items * of parameter object * FOR EXAMPLE: * if * calling object holds [10, 70, 20] * parameter object (other) holds [50, 90] * * then for the returned object, * list represents [10, 70, 20, 50, 90] */ public MyLinkedList join(MyLinkedList other) { return null; //to be completed }
/** * * @param other * @return true if calling object and parameter objects * have the same items (ok to be in different order) * * FOR EXAMPLE, * * if * calling object holds [10, 70, 20, 90] * parameter object (other) holds [90, 10, 20, 70] * return true * * if * calling object holds [10, 60, 20, 90] * parameter object (other) holds [90, 10, 20, 70] * return false * * if * calling object holds [10, 20, 90] * parameter object (other) holds [90, 10, 20, 70] * return false * if * calling object holds [10, 20, 90] * parameter object (other) holds [10, 20] * return false */ public boolean same(MyLinkedList other) { return false; //to be completed } }
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