Create JUnit tests to test each method thoroughly.
Recall that
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.
pairwiseSum
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.
smallElements
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 )
scale
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 ).
insertAfter
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
next
references in the existing
ListItem objects to insert the new items in the right places.
Don't create a whole new list structure.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
In the ListItem class, write methods with the following specifications, and write thorough JUnit tests to check that they work correctly.
reverseRecurse
that 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.)
reverseLoop
that 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.)
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
This extension requires a substative development effort. It is possible I would give more points for this extension than are advertised. If you are interested please contact the instructor.
More specifically, you construct a model for simulating the shape of a catenary: the shape of our beloved St. Louis Gateway Arch.
Long before our city began its expansion into the NFL, the pioneers of our country expanded to the West, crossing the Mississippi and braving the perils of midwestern life. As a monument to their efforts, the City of St. Louis erected Eero Saarinen's famous Arch in 1965. Its grace, beauty, and elegance have inspired poets, musicians, and artists of the past five decades.
It's time that CSE131 had its turn.
A catenary is the shape made when you suspend a string by holding its ends, and let gravity pull on the string. If you look at a telephone wire strung between two poles, the shape you see is a catenary, though not one that would necessarily garner artistic praise.
It turns out that our Arch is also a catenary, but situated upside down.
How are we to simulate an catenary acting on a string? If we look really closely at the string, we see that it is composed of a series of mass points, with each adjacent pair of masses connected by a spring.
With gravity turned off, the picture is as follows:
Extra fun: Modify your solution so that the Arch starts at the bottom of the screen, and falls up instead of down.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches