Create JUnit tests to test each method thoroughly.
to call static methods of the ListItem class
from your JUnit test methods, you can specify the class name each time,
as in ListItem.evenElements(ls). Alternatively, you can save characters and just write evenElements(ls) if you import the static methods from the ListItem class by adding the line
import static lab8.ListItem.*;
near the top of your file, after the package declaration.
As you work through these exercises, be sure to use recursion or iteration as indicated. Where not indicated, you may choose either approach. Each method will take references to list items as parameters. Unless otherwise specified, you should handle the case of a null parameter value.
PARAMETERS: two ListItem references, ls1 an ls2 RETURN VALUE: a ListItem at the front of a new list containing the pairwise sum of the elements in ls1 and ls2 for example, given lists ( 3 2 6 ) and ( 1 4 2), the return value would be the list ( 4 6 8 )You may assume that the given lists are the same length.
PARAMETERS: a ListItem reference, ls an integer n RETURN VALUE: a new list identical to the given list, except that it contains no occurrences of numbers greater than n. for example, given input list ( 3 2 6 3 4 ) and 3, the return value would be the list ( 3 2 3 )
PARAMETERS: a ListItem reference, ls an integer n RETURN VALUE: none (void) the procedure mutates the list by multiplying each number in the list by the given number n. for example, given input lists ( 3 2 6 3 4) and the number 3, the modified list would be ( 9 6 18 9 12 ).
PARAMETERS: a ListItem reference, ls two integers i and j (not equal) RETURN VALUE: none (void) the procedure mutates the list by inserting the given number i after each occurrence of j. for example, given input lists ( 3 2 6 3 4) and the number 5 and 3, the modified list would be ( 3 5 2 6 3 5 4 ).Be sure to do this by creating new list items only for the inserted numbers. Modifying the
nextreferences in the existing ListItem objects to insert the new items in the right places. Don't create a whole new list structure.
In the ListItem class, write methods with the following specifications, and write thorough JUnit tests to check that they work correctly.
reverseRecursethat takes no parameters and returns a ListItem. The return value should be a new list structure that contains the same numbers as this list, but in the reverse order. (Hint: Use a helper procedure with an extra parameter that accumulates the reversed list.)
reverseLoopthat takes no parameters and returns a ListItem. The return value should be a new list structure that contains the same numbers as this list, but in the reverse order. (Hint: Create a loop variable that's a ListItem reference to keep track of your current position in the list.)