Answered step by step
Verified Expert Solution
Question
1 Approved Answer
implement and test each of the following methods found in A4Exercises. show output of A4Tester java public class A4LinkedList implements A4List { // Completed for
implement and test each of the following methods found in A4Exercises. show output of A4Tester
java
public class A4LinkedList implements A4List { // Completed for you, should not be changed private StudentNode head; private StudentNode tail; private int numElements; // Completed for you, should not be changed public A4LinkedList() { head = null; tail = null; numElements = 0; } // Completed for you, should not be changed public int size(){ return numElements; } // Completed for you, should not be changed public boolean isEmpty() { return head == null; } // Completed for you, should not be changed public void insert(Student s) { StudentNode n = new StudentNode(s); if (head == null) { head = n; } else { tail.next = n; } tail = n; numElements++; } /* * Purpose: create a string representation of list * Parameters: none * Returns: String - the string representation * * Completed for you, should not be changed */ public String toString() { return "{" + toStringRecursive(head) + "}"; } public String toStringRecursive(StudentNode cur) { if (cur == null) { return ""; } else if (cur.next == null) { return cur.getData().toString() + toStringRecursive(cur.next); } else { return cur.getData().toString() + ", " + toStringRecursive(cur.next); } } public boolean inProgram(String program) { return inProgramRecursive(head, program); } private boolean inProgramRecursive(StudentNode cur, String program) { return false; // so it compiles } public Student getStudent(String sID) { return getStudentRecursive(head, sID); } private Student getStudentRecursive(StudentNode cur, String sID) { return null; // so it compiles } public double averageGPA() { if (size() == 0) { return 0.0; } else { return sumGPARecursive(head)/size(); } } private double sumGPARecursive(StudentNode cur) { return 0.0; // so it compiles } public double programAverage(String program) { // call a recursive helper method! return 0.0; } public Student highestGPA() { // call a recursive helper method! return null; } }import java.util.Arrays; public class A4Tester { private static int testPassCount = 0; private static int testcount - @; private static double THRESHOLD = 2.1; // allowable margin of error for floating point results private static Allist liste - null; private static Allist listi - null; private static Allist list2 = null; private static Allist list3 = null; public static void main(String[] args) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 PART I: implement and test each of the following methods found in A4Exercises.java, one at a time testAddToAll(); // testArrayContains(); // testameInAROW(); PART II implement and test each of the following methods found in Allinkedlist.java, one at a time. Documentation can be found in A4List.java */ 34 // initlists(); // do not modify // testInProgram(); // testGetStudent(); // testAverageGPA(); // test Programaverage(); 1/ testHighestGPA(); 35 36 37 38 39 40 41 42 43 44 "testCount + " tests"); System.out.println(" PASSED " + testPassCount 0 public static void testAddToAll() { System.out.println(" Starting at ToAll tests"); 45 public static void testAddToA11() { System.out.println(" Starting at Toall tests"); 12 13 14 45 46 47 48 49 50 51 52 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 06 int[] arre- (); int[] arri- {7, 1, 3); int[] arr2 - (9, 2, 7, 3, 0, 6); int[] expectede - (); int[] expectedi - {8, 2, 4); int[] expected2 - (11, 5, 7); int[] expected3 - (14, 7, 12, 8, 5, 11); A4Exercises.addToAll(arre, 1); displayResults(Arrays.equals(arre, expectede), "add 1 to all values in empty array"); A4Exercises.addToAll(arri, 1); displayResults(Arrays.equals(arri, expectedl), "add 1 to all values in arri"); AdExercises.addToAll(arri, 3); displayResults (Arrays.equals(arri, expected2), "add 3 to all values in updated arr1"); A4Exercises.addToAll(arr2, 5); displayResults(Arrays.equals(arra, expected), "uda s to all values in arr2"); } public static void testArraycontains() { System.out.println(" Starting arrayContains tests"); int[] arre - (); int[] arri. (7, 1, 3); int[] arr2 - (9, 2, 7, 3, 9, 6); boolean result = false; result - Exercises.arraycontains (arre, 4); displayResults (Tresult, "empty array contains 4"); result - Mexercises.arraycontains(arri, 1); displayResults (result, "arri contains 1"); result. Mexercises.arrayContains(arri, 2); displayResults(Iresult, "arri contains 2"); result - Mexercises, arrayContains(arr2, 9); displayResults(result, "arr2 contains 9"); AAEWARE Autontained displayResults(result, "arr2 contains 9"); result - A4Exercises.arrayContains(arr2, 6); displayResults(result, "arr2 contains 6"); result = A4Exercises.arrayContains(arr2, 5); displayResults(Iresult, "arr2 contains 5"); ) public static void testSame InRow() { System.out.println(" Starting sameInArow tests"); int[] arre - (); int[] arri - (7, 1, 3); int[] arr2 - (7, 1, 1, 3); int[] arr3 = {7, 7, 1, 3); int[] arr4 = {7, 1, 3, 3); int[] arr5 - (7, 7, 1, 1, 3, 3); int[] arr6 - (7, 7, 1, 1, 1, 3, 3); int[] arr7 - (2, 2, 2, 2, 2, 2, 2); int result - @; int expected - 0; result - Alexercises.sameInArow(arro); displayResults (resultuexpected, "same in a row, empty array"); result - A4Exercises.sameInAROW(arri); displayResults(result--expected, "same in a row, (7,1,3)"); result - A4Exercises.sameInARow(arr2); expected - 1; displayResults(resulterexpected, "same in a row, (7,1,1,3}"); result - A4Exercises.same InRow(arr3); expected - 1; displayResults(resulte-expected, "same in a row, (7,7,1,3}"); result = A4Exercises.sameInAROW(arra); expected - 1; displayResults(result--expected, "same in a row, (7,1,3,3)); result = A4Exercises.same InRow (arr5); expected - 3; displayResults(resulte-expected, "same in a row, {7,7,1,1,3,3}"); result = A4Exercises.sameInARow(arro); result = A4Exercises. Same InRow(arr6); expected = 4; displayResults(resulte-expected, "same in a row, (7,7,1,1,1,3,3)); 29 30 31 32 33 34 35 36 32 result A4Exercises.same InRow(arr7); expected - 6; displayResults(resulterexpected, "same in a row, (2,2,2,2,2,2,2}"); } public class A4Exercises / Purpose: add valToAdd to all elements in the given array Parameters: int[] array - the array to modify int valToAdd - the value to modify items by Returns: void - nothing public static void addToA11(int[] array, int valToAdd) { addToAll Recursive(array, valToAdd, array.length-1); ) private static void addToAllRecursive(int[] array, int valToAdd, Int 1) { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 Purpose: determines whether the given array contains to find Parameters: Int[] array - the array to search Int toFind the value to search for Returns: boolean - true if found, false otherwise / public static boolean arraycontains(int[] array, Int tofind) return arrayContainsRecursive(array, toFind, array.length-1); 3 private static boolean arraycontains Recursive(int[] array, int toFind, int ) { return false; // so it compiles > Purpose: gets the number of times there are two of the same element in a row Parameters: int[] array - the array to search Returns: Int. the number of occurrences where two adjacent elements are the same */ public static int same InArow(int[] array) ( return same InRowRecursive (array, -1, array.length-1); ) private static int same In RowRecursive(int() array, int prev, int 1) { return 0; // so it compiles import java.util.Arrays; public class A4Tester { private static int testPassCount = 0; private static int testcount - @; private static double THRESHOLD = 2.1; // allowable margin of error for floating point results private static Allist liste - null; private static Allist listi - null; private static Allist list2 = null; private static Allist list3 = null; public static void main(String[] args) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 PART I: implement and test each of the following methods found in A4Exercises.java, one at a time testAddToAll(); // testArrayContains(); // testameInAROW(); PART II implement and test each of the following methods found in Allinkedlist.java, one at a time. Documentation can be found in A4List.java */ 34 // initlists(); // do not modify // testInProgram(); // testGetStudent(); // testAverageGPA(); // test Programaverage(); 1/ testHighestGPA(); 35 36 37 38 39 40 41 42 43 44 "testCount + " tests"); System.out.println(" PASSED " + testPassCount 0 public static void testAddToAll() { System.out.println(" Starting at ToAll tests"); 45 public static void testAddToA11() { System.out.println(" Starting at Toall tests"); 12 13 14 45 46 47 48 49 50 51 52 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 06 int[] arre- (); int[] arri- {7, 1, 3); int[] arr2 - (9, 2, 7, 3, 0, 6); int[] expectede - (); int[] expectedi - {8, 2, 4); int[] expected2 - (11, 5, 7); int[] expected3 - (14, 7, 12, 8, 5, 11); A4Exercises.addToAll(arre, 1); displayResults(Arrays.equals(arre, expectede), "add 1 to all values in empty array"); A4Exercises.addToAll(arri, 1); displayResults(Arrays.equals(arri, expectedl), "add 1 to all values in arri"); AdExercises.addToAll(arri, 3); displayResults (Arrays.equals(arri, expected2), "add 3 to all values in updated arr1"); A4Exercises.addToAll(arr2, 5); displayResults(Arrays.equals(arra, expected), "uda s to all values in arr2"); } public static void testArraycontains() { System.out.println(" Starting arrayContains tests"); int[] arre - (); int[] arri. (7, 1, 3); int[] arr2 - (9, 2, 7, 3, 9, 6); boolean result = false; result - Exercises.arraycontains (arre, 4); displayResults (Tresult, "empty array contains 4"); result - Mexercises.arraycontains(arri, 1); displayResults (result, "arri contains 1"); result. Mexercises.arrayContains(arri, 2); displayResults(Iresult, "arri contains 2"); result - Mexercises, arrayContains(arr2, 9); displayResults(result, "arr2 contains 9"); AAEWARE Autontained displayResults(result, "arr2 contains 9"); result - A4Exercises.arrayContains(arr2, 6); displayResults(result, "arr2 contains 6"); result = A4Exercises.arrayContains(arr2, 5); displayResults(Iresult, "arr2 contains 5"); ) public static void testSame InRow() { System.out.println(" Starting sameInArow tests"); int[] arre - (); int[] arri - (7, 1, 3); int[] arr2 - (7, 1, 1, 3); int[] arr3 = {7, 7, 1, 3); int[] arr4 = {7, 1, 3, 3); int[] arr5 - (7, 7, 1, 1, 3, 3); int[] arr6 - (7, 7, 1, 1, 1, 3, 3); int[] arr7 - (2, 2, 2, 2, 2, 2, 2); int result - @; int expected - 0; result - Alexercises.sameInArow(arro); displayResults (resultuexpected, "same in a row, empty array"); result - A4Exercises.sameInAROW(arri); displayResults(result--expected, "same in a row, (7,1,3)"); result - A4Exercises.sameInARow(arr2); expected - 1; displayResults(resulterexpected, "same in a row, (7,1,1,3}"); result - A4Exercises.same InRow(arr3); expected - 1; displayResults(resulte-expected, "same in a row, (7,7,1,3}"); result = A4Exercises.sameInAROW(arra); expected - 1; displayResults(result--expected, "same in a row, (7,1,3,3)); result = A4Exercises.same InRow (arr5); expected - 3; displayResults(resulte-expected, "same in a row, {7,7,1,1,3,3}"); result = A4Exercises.sameInARow(arro); result = A4Exercises. Same InRow(arr6); expected = 4; displayResults(resulte-expected, "same in a row, (7,7,1,1,1,3,3)); 29 30 31 32 33 34 35 36 32 result A4Exercises.same InRow(arr7); expected - 6; displayResults(resulterexpected, "same in a row, (2,2,2,2,2,2,2}"); } public class A4Exercises / Purpose: add valToAdd to all elements in the given array Parameters: int[] array - the array to modify int valToAdd - the value to modify items by Returns: void - nothing public static void addToA11(int[] array, int valToAdd) { addToAll Recursive(array, valToAdd, array.length-1); ) private static void addToAllRecursive(int[] array, int valToAdd, Int 1) { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 Purpose: determines whether the given array contains to find Parameters: Int[] array - the array to search Int toFind the value to search for Returns: boolean - true if found, false otherwise / public static boolean arraycontains(int[] array, Int tofind) return arrayContainsRecursive(array, toFind, array.length-1); 3 private static boolean arraycontains Recursive(int[] array, int toFind, int ) { return false; // so it compiles > Purpose: gets the number of times there are two of the same element in a row Parameters: int[] array - the array to search Returns: Int. the number of occurrences where two adjacent elements are the same */ public static int same InArow(int[] array) ( return same InRowRecursive (array, -1, array.length-1); ) private static int same In RowRecursive(int() array, int prev, int 1) { return 0; // so it compiles
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