Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

do questions 3,4,5 in first two pictures, do code in rest of pictures, run in Lab5Tester java elements long 3. In IntegerLinkedList.java test and implement

do questions 3,4,5 in first two pictures, do code in rest of pictures, run in Lab5Tester

java image text in transcribed image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

elements long 3. In IntegerLinkedList.java test and implement the next two functions (one at a time) marked with //ToDo comments following the given documentation. To help you with the implementation, follow these steps: a. Create a public stub for the method b. In Lab5Tester.java write tests calling this method on an empty list and on a list that is at least 3 elements long (model after addOneRecursive tests) c. Write the template for the private recursive helper function (ie, addOneRecursiveHelper) and place a call to that helper function in the public stub method you created in Step a. d. Add code for your base case answer/result e. Add code to deal with the current Node f. Run the tests you added to Lab5Tester.java with the Tifuu are not comfortable 4. In IntegerLinkedList java test and implement the next function marked with //ToDo comments following the given documentation. Use the process from Step 3 above to help. Look at the implementation of the recursive doubleOddPosition method. This method also has that general structure of the template shown on Page 1, but it has an additional parameter. This additional parameter is called an accumulator which is used to keep track of and pass on context that would otherwise be lost in the recursive call . In this case, the accumulator (position) is keeping track of the position of the The position of the first element of the list is, the next element is at position 1, the next at position 2... There are 4 steps to adding an accumulator to your recursive function: Add it to the parameter list in the private helper function: public void doubleOddPositionValues(IntegerNode n, int position), Give the accumulator an initial value in the call to the private helper method: double OddPositionValues (head, 0); Exploit (use) the value of the accumulator as needed within the method: if (position % 21 = 0) { Pass the likely updated) value of the accumulator as a parameter to the recursive call: doubleOddPositionValues(n. next position+1); 5. In IntegerLinkedtist java test and implement the next two functions (one at a time) marked with //ToDo comments following the given documentation HINT: Think about if you need an accumulator or not. Is there context that you need to know about from the previous steps of the recursion. One of these methods will need one! Again, use the process from Step 3 above to heln and follow the stone aluan shnu. public class Labs Tester private static int testPassCount ; private static int testCount - e; public static void main(String[] args) { try { testAddone(); testAddOneRecursive(); testSumvalues(); testDoubleAtoddPositions(); 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 44 45 46 47 } catch (Exception e) { System.out.println("Your code threw an exception."); System.out.println("Perhaps a stack trace will help: "); e.printStackTrace(System.out); } System.out.println("Passed + testPasscount + "/" + testcount + " tests"); } public static void testAddone() { IntegerLinkedlist emptylist - new IntegerLinkedlist(); IntegerLinkedlist list3 = new IntegerLinkedList(); list3.addFront(-2); 11st3.addFront(); list3.addFront(7); emptyList.addone(); System.out.println(emptylist); displayResults(emptylist.toString().equals(""), "testAddone - empty"); list3.addone(); System.out.println(list3); displayResults(11st3.toString().equals("8 1 -1), "testAddone - length"); } public static void testAddOneRecursive() { IntegerLinkedlist emptyList = new IntegerLinkedList(); IntegerLinkedList list3 = new IntegerLinkedList(); list3.addFront(-2); list3.addFront(); list3.addFront(7); emptyList.addOneRecursive(); System.out.println(emptyList); displayResults(emptyList.toString().equals(""), "testAddone - empty"); list3.addOneRecursive(); System.out.println(list3); displayResults(list3.toString().equals("8 1 -1"), "testAddone - length3"); public static void testSumValues() { IntegerLinkedlist emptylist - new IntegerLinkedList(); IntegerLinkedlist list3-new IntegerLinkedList(); list3.addFront(-2); list3.addFront(); list3.addFront(7); int result - emptyList.sum(); displayResults(result -- @, "testSumValues empty"); result - list3.sum(); displayResults(result -- 5, "testSumValues - length3"); } public static void testDoubleAtOddPositions() { IntegerLinkedlist emptyList = new IntegerLinkedList(); IntegerLinkedlist list3a = new IntegerLinkedlist(); list3a. addFront(-2); list3a. addFront (5); list3a. addFront(7); list3a. addFront(8): IntegerLinkedlist emptyList = new IntegerLinkedlist(); IntegerLinkedlist list3a = new IntegerLinkedlist(); list3a. addFront(-2); list3a. addFront(5); list3a.addFront (7); list3a.addFront(s); emptyList.doubleOddPositionValues(); displayResults (emptylist.toString().equals(""), "testDoubleAtoddPositions - empty"); list3a.doubleOddPositionValues(); displayResults(11st3a.toString().equals("8 14 5 -4"), "testDoubleAtoddPositions - length3"); public static void displayResults (boolean passed, String testName) { /* There is some magic going on here getting the line number Borrowed from: http://blog.taragana.com/index.php/archive/core-java-how-to-get-java-source-code-line-number-file-name-in-co testCount++; if (passed) { System.out.println ("Passed test: " +testCount); testPassCount++; } else { System.out.println("Failed test: " + testName + " at line + Thread.currentThread().getStackTrace()[2].getLineNumber()); public class IntegerNode { IntegerNode next; int public IntegerNode() { next = null; e = @; } public Integer Node (Integer e) { this.e = e; next = null;; } public Integer Node (Integer e, IntegerNode next) { this.e = e; this.next = next; } public IntegerNode getNext() return next; } public void setNext (IntegerNode next) { this.next = next; } public int getElement() { return e; } public void setElement (Integer e) { this.e = e; 3 public class IntegerLinkedlist! 1 2 3 4 5 private IntegerNode head; private int count) 8 public IntegerLinkedList() { head. null; count = 0; size Purpose: returns the size of this Integerlist Parameters: none Returns: int - the size */ public int size() { return count; } 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 addFront Purpose: adds element with i to the front of this Integerlist Parameters: int - i Returns: nothing */ public void addFront (int i) { Integer Node n = new Integer Node(1, head); head = n; 32 count++; } 33 34 35 36 37 38 39 40 41 42 43 44 toString * Purpose: returns a string representation of this Integerlist Parameters: none Returns: String - the String representation public String toString() { 45 44 45 46 47 48 public String toString() { String s = ""; Integer Node tmp- head; 49 50 while (tmpl=null) { s + tmp.getElement(); if(tmp.nextI=null) S +" "; tmp = tmp.next; return s; } 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 .addOne Purpose: adds 1 to every element in this Integerlist Parameters: none Returns: nothing */ public void addone() { Integer Node tmp = head; while (tmpl=null) { int valPlusOne = tmp.getElement() + 1; tmp.setElement(valPlusOne); tmp = tmp.next; 70 } 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 addOneRecursive Purpose: recursively adds 1 to every element in this Integerlist Parameters: none Returns: nothing */ public void addOneRecursive() { addOneRecursiveHelper (head); } addOneRecursiveHelper Purpose: recursively adds 1 to IntegerNode n and every element following n Parameters: IntegerNoden 85 86 87 88 89 90 91 92 addoneRecursiveHelper Purpose: recursively adds 1 to IntegerNoden and every element following n Parameters: IntegerNoden Returns: nothing private void addOneRecursiveHelper(IntegerNode n) { if (nuanull) { return; } else { 1/ get data in current node and add 1 to it int valPlusOne - n.getElement() + 13 // set element in current node to valPlusone n.setElement(valPlusone), 93 // add one to the elements in the REST of the list addOneRecursiveHelper(n.next); } 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 doubleA11 Purpose: recursively doubles every element in this integerlist Parameters: none Returns: nothing */ // Todo: implement and test this method doubleOddValues Purpose: recursively doubles every odd valued element in this Integerlist By "odd valued element" we mean the Node's element is an odd number NOT that it is at an odd position in the 11st Parameters: none Returns: nothing */ // Todo: implement and test this method sum 2 3 4 5 5 Purpose: recursively sums every element in this Integerlist Parameters: none Returns: int - the sum */ public int sum() { return sum(head); } B sun 1 Purpose: recursively sums element in IntegerNoden and every element following n Parameters: IntegerNoden Returns: int - the sum private int sum(Integer Node n) { if (nasnull) { return; } else { int first = n.getElement(); int sumRest = sum(n.next); return first + sunRest; } 2 3 4 5 6 7 8 9 @ 1 2 3 -4 5 56 57 8 69 se 51 52 53 54 55 56 57 58 69 70 71 product Purpose: recursively computes the product of every element in this Integerlist Note: the product of an empty list is 1 * Returns: int - the product */ // TODO: implement and test this method # Parameters: none - doubleOddPositionValues Purpose: recursively doubles every element at an odd postion in this Integerlist the first element in this list is at position (is not odd) the second element in this list is at position 1 (is odd) the third element in this list is at position 2 (is not odd) * Purpose: recursively doubles every element at an odd postion in this Integerlist the first element in this list is at position @ (is not odd) the second element in this list is at position 1 (is odd) the third element in this list is at position 2 (is not odd) Parameters: none Returns: nothing */ public void doubleOddPositionValues() { doubleOddPositionValues (head, ); } 1 . 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 doubleOddPositionValues Purpose: recursively doubles element in Integer Noden if n is at odd position and every element after n at odd positions Parameters: IntegerNode - n, int - position Returns: nothing private void doubleOddPositionValues(Integernode n, int position) { if (n=null) { return; } else { if (position % 2 I. e) { int doubleval - n.getElement() . 2; n.setElement(doubleval); } doubleOddPositionValues(n.next, position+1); issorted Purpose: recursively determines whether every element in this Integerlist is sorted in ascending order {1, 2, 2, 5) is sorted in ascending order (3, 2, 2, 5) is not sorted in ascending order Parameters: none Returns: boolean - true if sorted, false otherwise * Note: an empty list is considered sorted // ToDo: implement and test this method 16 -17 18 19 220 221 222 223 224 225 226 allNegative * Purpose: recursively determines whether all elements in this Integerlist are negative true if all negative, false otherwise * Note: an empty list is considered to have all negative values (as there are no non-negative values found in the list) * Parameters: none Returns: boolean // TODO: implement and test this method 227 228 229 230 elements long 3. In IntegerLinkedList.java test and implement the next two functions (one at a time) marked with //ToDo comments following the given documentation. To help you with the implementation, follow these steps: a. Create a public stub for the method b. In Lab5Tester.java write tests calling this method on an empty list and on a list that is at least 3 elements long (model after addOneRecursive tests) c. Write the template for the private recursive helper function (ie, addOneRecursiveHelper) and place a call to that helper function in the public stub method you created in Step a. d. Add code for your base case answer/result e. Add code to deal with the current Node f. Run the tests you added to Lab5Tester.java with the Tifuu are not comfortable 4. In IntegerLinkedList java test and implement the next function marked with //ToDo comments following the given documentation. Use the process from Step 3 above to help. Look at the implementation of the recursive doubleOddPosition method. This method also has that general structure of the template shown on Page 1, but it has an additional parameter. This additional parameter is called an accumulator which is used to keep track of and pass on context that would otherwise be lost in the recursive call . In this case, the accumulator (position) is keeping track of the position of the The position of the first element of the list is, the next element is at position 1, the next at position 2... There are 4 steps to adding an accumulator to your recursive function: Add it to the parameter list in the private helper function: public void doubleOddPositionValues(IntegerNode n, int position), Give the accumulator an initial value in the call to the private helper method: double OddPositionValues (head, 0); Exploit (use) the value of the accumulator as needed within the method: if (position % 21 = 0) { Pass the likely updated) value of the accumulator as a parameter to the recursive call: doubleOddPositionValues(n. next position+1); 5. In IntegerLinkedtist java test and implement the next two functions (one at a time) marked with //ToDo comments following the given documentation HINT: Think about if you need an accumulator or not. Is there context that you need to know about from the previous steps of the recursion. One of these methods will need one! Again, use the process from Step 3 above to heln and follow the stone aluan shnu. public class Labs Tester private static int testPassCount ; private static int testCount - e; public static void main(String[] args) { try { testAddone(); testAddOneRecursive(); testSumvalues(); testDoubleAtoddPositions(); 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 44 45 46 47 } catch (Exception e) { System.out.println("Your code threw an exception."); System.out.println("Perhaps a stack trace will help: "); e.printStackTrace(System.out); } System.out.println("Passed + testPasscount + "/" + testcount + " tests"); } public static void testAddone() { IntegerLinkedlist emptylist - new IntegerLinkedlist(); IntegerLinkedlist list3 = new IntegerLinkedList(); list3.addFront(-2); 11st3.addFront(); list3.addFront(7); emptyList.addone(); System.out.println(emptylist); displayResults(emptylist.toString().equals(""), "testAddone - empty"); list3.addone(); System.out.println(list3); displayResults(11st3.toString().equals("8 1 -1), "testAddone - length"); } public static void testAddOneRecursive() { IntegerLinkedlist emptyList = new IntegerLinkedList(); IntegerLinkedList list3 = new IntegerLinkedList(); list3.addFront(-2); list3.addFront(); list3.addFront(7); emptyList.addOneRecursive(); System.out.println(emptyList); displayResults(emptyList.toString().equals(""), "testAddone - empty"); list3.addOneRecursive(); System.out.println(list3); displayResults(list3.toString().equals("8 1 -1"), "testAddone - length3"); public static void testSumValues() { IntegerLinkedlist emptylist - new IntegerLinkedList(); IntegerLinkedlist list3-new IntegerLinkedList(); list3.addFront(-2); list3.addFront(); list3.addFront(7); int result - emptyList.sum(); displayResults(result -- @, "testSumValues empty"); result - list3.sum(); displayResults(result -- 5, "testSumValues - length3"); } public static void testDoubleAtOddPositions() { IntegerLinkedlist emptyList = new IntegerLinkedList(); IntegerLinkedlist list3a = new IntegerLinkedlist(); list3a. addFront(-2); list3a. addFront (5); list3a. addFront(7); list3a. addFront(8): IntegerLinkedlist emptyList = new IntegerLinkedlist(); IntegerLinkedlist list3a = new IntegerLinkedlist(); list3a. addFront(-2); list3a. addFront(5); list3a.addFront (7); list3a.addFront(s); emptyList.doubleOddPositionValues(); displayResults (emptylist.toString().equals(""), "testDoubleAtoddPositions - empty"); list3a.doubleOddPositionValues(); displayResults(11st3a.toString().equals("8 14 5 -4"), "testDoubleAtoddPositions - length3"); public static void displayResults (boolean passed, String testName) { /* There is some magic going on here getting the line number Borrowed from: http://blog.taragana.com/index.php/archive/core-java-how-to-get-java-source-code-line-number-file-name-in-co testCount++; if (passed) { System.out.println ("Passed test: " +testCount); testPassCount++; } else { System.out.println("Failed test: " + testName + " at line + Thread.currentThread().getStackTrace()[2].getLineNumber()); public class IntegerNode { IntegerNode next; int public IntegerNode() { next = null; e = @; } public Integer Node (Integer e) { this.e = e; next = null;; } public Integer Node (Integer e, IntegerNode next) { this.e = e; this.next = next; } public IntegerNode getNext() return next; } public void setNext (IntegerNode next) { this.next = next; } public int getElement() { return e; } public void setElement (Integer e) { this.e = e; 3 public class IntegerLinkedlist! 1 2 3 4 5 private IntegerNode head; private int count) 8 public IntegerLinkedList() { head. null; count = 0; size Purpose: returns the size of this Integerlist Parameters: none Returns: int - the size */ public int size() { return count; } 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 addFront Purpose: adds element with i to the front of this Integerlist Parameters: int - i Returns: nothing */ public void addFront (int i) { Integer Node n = new Integer Node(1, head); head = n; 32 count++; } 33 34 35 36 37 38 39 40 41 42 43 44 toString * Purpose: returns a string representation of this Integerlist Parameters: none Returns: String - the String representation public String toString() { 45 44 45 46 47 48 public String toString() { String s = ""; Integer Node tmp- head; 49 50 while (tmpl=null) { s + tmp.getElement(); if(tmp.nextI=null) S +" "; tmp = tmp.next; return s; } 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 .addOne Purpose: adds 1 to every element in this Integerlist Parameters: none Returns: nothing */ public void addone() { Integer Node tmp = head; while (tmpl=null) { int valPlusOne = tmp.getElement() + 1; tmp.setElement(valPlusOne); tmp = tmp.next; 70 } 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 addOneRecursive Purpose: recursively adds 1 to every element in this Integerlist Parameters: none Returns: nothing */ public void addOneRecursive() { addOneRecursiveHelper (head); } addOneRecursiveHelper Purpose: recursively adds 1 to IntegerNode n and every element following n Parameters: IntegerNoden 85 86 87 88 89 90 91 92 addoneRecursiveHelper Purpose: recursively adds 1 to IntegerNoden and every element following n Parameters: IntegerNoden Returns: nothing private void addOneRecursiveHelper(IntegerNode n) { if (nuanull) { return; } else { 1/ get data in current node and add 1 to it int valPlusOne - n.getElement() + 13 // set element in current node to valPlusone n.setElement(valPlusone), 93 // add one to the elements in the REST of the list addOneRecursiveHelper(n.next); } 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 doubleA11 Purpose: recursively doubles every element in this integerlist Parameters: none Returns: nothing */ // Todo: implement and test this method doubleOddValues Purpose: recursively doubles every odd valued element in this Integerlist By "odd valued element" we mean the Node's element is an odd number NOT that it is at an odd position in the 11st Parameters: none Returns: nothing */ // Todo: implement and test this method sum 2 3 4 5 5 Purpose: recursively sums every element in this Integerlist Parameters: none Returns: int - the sum */ public int sum() { return sum(head); } B sun 1 Purpose: recursively sums element in IntegerNoden and every element following n Parameters: IntegerNoden Returns: int - the sum private int sum(Integer Node n) { if (nasnull) { return; } else { int first = n.getElement(); int sumRest = sum(n.next); return first + sunRest; } 2 3 4 5 6 7 8 9 @ 1 2 3 -4 5 56 57 8 69 se 51 52 53 54 55 56 57 58 69 70 71 product Purpose: recursively computes the product of every element in this Integerlist Note: the product of an empty list is 1 * Returns: int - the product */ // TODO: implement and test this method # Parameters: none - doubleOddPositionValues Purpose: recursively doubles every element at an odd postion in this Integerlist the first element in this list is at position (is not odd) the second element in this list is at position 1 (is odd) the third element in this list is at position 2 (is not odd) * Purpose: recursively doubles every element at an odd postion in this Integerlist the first element in this list is at position @ (is not odd) the second element in this list is at position 1 (is odd) the third element in this list is at position 2 (is not odd) Parameters: none Returns: nothing */ public void doubleOddPositionValues() { doubleOddPositionValues (head, ); } 1 . 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 doubleOddPositionValues Purpose: recursively doubles element in Integer Noden if n is at odd position and every element after n at odd positions Parameters: IntegerNode - n, int - position Returns: nothing private void doubleOddPositionValues(Integernode n, int position) { if (n=null) { return; } else { if (position % 2 I. e) { int doubleval - n.getElement() . 2; n.setElement(doubleval); } doubleOddPositionValues(n.next, position+1); issorted Purpose: recursively determines whether every element in this Integerlist is sorted in ascending order {1, 2, 2, 5) is sorted in ascending order (3, 2, 2, 5) is not sorted in ascending order Parameters: none Returns: boolean - true if sorted, false otherwise * Note: an empty list is considered sorted // ToDo: implement and test this method 16 -17 18 19 220 221 222 223 224 225 226 allNegative * Purpose: recursively determines whether all elements in this Integerlist are negative true if all negative, false otherwise * Note: an empty list is considered to have all negative values (as there are no non-negative values found in the list) * Parameters: none Returns: boolean // TODO: implement and test this method 227 228 229 230

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago