lab2
package.
Update by right-clicking (control-click on Mac) the main project in the package explorer, drag down to Team... and click Update.You should see a lab2 package in the src directory.
lab2
package because the compiler can't find JUnit
.
In the lab2
package, double click on the file name
RecursionTest.java
to open it in the editor. You'll see an error marked in the eclipse editor. Click on the light bulb in the left margin next to the error, and one of the choices should be to fix the project setup.
Double-click that that and follow from there
to add JUnit4
to the build path. This should tell the compiler where to find JUnit
, resolving the error.
@Test public void testAddition() { assertEquals(1, 1+0); assertEquals(131, 2*60 + 11); }
At this point, be sure to ask questions if anything is unclear so far.
You will be writing new methods and JUnit tests in this lab, and this may well be the first time you have done that. You will be patterning what you do after the examples that are already in the files provided for this lab. Try to reason about what you see in the files we provided, and feel free to try things. As always, ask for help as soon as you need it!Your work will consist of:
- Writing a very short piece of recursive code in a new method you define. This happens in the Recursion.java file.
- Writing a corresponding test to for that method. The test will call your method and then compare its return value to the expected result. This happens in RecursionTest.java.
sumDownBy2
with the following specification.
PARAMETERS: an integer n RETURN VALUE: the sum of the positive integers n + (n-2) + (n-4) + ... EXAMPLES: sumDownBy2(7) is 7+5+3+1 = 16 sumDownBy2(8) is 8+6+4+2 = 20 sumDownBy2(0) is 0 sumDownBy2(-1) is 0
- Once you've written code that you think is correct for harmonicSum in your Recursion.java file, then you should write and run a corrsponding @Test method into the RecursionTest.java file.
- A good name for the test method is testHarmonicSum, and you should put in a few assertEqual tests in testHarmonicSum to make sure your code for harmonicSum is working properly.
- Make sure you see green for this test before going forward.
Continue the lab in this fashion: write code, test the code, and then move forward.
harmonicSum
with the following specification.
PARAMETERS: a positive integer, n RETURN VALUE: the sum 1 + 1/2 + 1/3 + ... + 1/(n-1) + 1/n
Warning: When using assertEquals to compare two double types, supply a third parameter to indicate the tolerance.For example:
assertEquals(d1, d2, .01);compares d1 and d2 with tolerance 1/100. The two doubles will be considered the same if the are within 1/100 of each other.
geometricSum
with the following specification.
PARAMETERS: a non-negative integer, k RETURN VALUE: the sum 1 + 1/2 + 1/4 + 1/8 + ... + 1/Math.pow(2,k)
mult
with the following specification
without using the multiplication operator. Write a recursive
method that performs the multiplication by repeated addition.
Make your method work for both positive and negative integers, as well
as zero. Start by calling a separate helper method that assumes both parameters are non-negative. Then, in the calling method, make an adjustment afterwards for the case when the signs of the two original numbers were different.
PARAMETERS: integers j and k RETURN VALUE: the product j*k
expt
with the following specification. Use repeated multiplication.
(Do not use the built-in exponentiation method.)
PARAMETERS: integers n and k, where k >= 0 RETURN VALUE: the value of n to the power k EXAMPLES: expt(3,2) is 9 expt(5,0) is 1 expt(2,5) is 32Hint: rewrite n^k as n * n^(k-1)
lcm
with the following specification.
PARAMETERS: positive integers j and k RETURN VALUE: the least common multiple (LCM) of j and k EXAMPLES: lcm(3,5) is 15 lcm(6,8) is 24Hint: Write a helper method with an extra parameter. If j >= k, start the extra parameter at j and keep adding j to it at each recursive call until you reach a value divisible by k. Think about how you can be assured this will terminate.
PARAMETERS: a loan amount (principal) in dollars an annual interest rate a monthly payment RETURN VALUE: the number of months until the loan is paid off, assuming that the interest is compounded monthly EXAMPLES: loanLength(1000, 0.10, 200) is 6 months loanLength(1000, 0.10, 1050) is 1 month loanLength(0, 0.90, 50) is 0 months
Banks charge interest before they deduct payment.In the last month, the payment may be less than the monthly payment amount. So, think of the loan as being paid off when principal is 0 or less.
Month 0: $1000 Month 1: $758 Month 2: $515 Month 3: $269 Month 4: $21