L08

Due by the end of today by midnight

Starter Files:


SUBMIT   /   Submission Summary

Today's Activities

Activity 1
Write a program called FibonacciRatios that has a recursive method fibRatio that takes two integer values (high and low) and returns the ratio high! / low!. That is, it calculates the product of all the numbers in the range (low, high]. For example, fibRatio(10, 6) would return 10 * 9 * 8 * 7 (10! / 6!).
Yes, it would be more efficient to write a loop to do this, but I want you to practice recursion.

Have your program print out the following fibonacci factorial ratios:

fibRatio(5, 0) = 120 fibRatio(10, 5) = 30,240 fibRatio(15, 10) = 360,360 fibRatio(20, 15) = 1,860,480 fibRatio(25, 20) = 6,375,600 fibRatio(30, 25) = 17,100,720
The output above was produced using the command:
System.out.printf("fibRatio(%d, %d) = %,d%n", n, n - 5, fibRatio(n, n - 5));
It's the "%,d" that's responsible for printing out the large numbers with comma separators.

Hint: the method will be very similar to the fibonacci factorial method, but will stop earlier. Consider, for example, what fibRatio(10, 10) should return.

Activity 2
Download the classes Closest and Common. Note the method in Common to print part of an array. You will be using this method in your code below.

Extend the program Closest to include two methods named findAndPrintClosest. The complete javadoc for the first has been provided, and an incomplete javadoc for the second has also been provided.

The second method must use recursion. In particular, it must split the array into its lower and upper "halves" and compare the answer from each "half" to get the answer for the whole.

Once again, it would be more efficient to do this using a loop. I don't care. I want you to practice recursion by splitting arrays.

When complete, the first part of your program should produce the following output:

Recursive Finding Closest Value ------------------------------- ...press enter... The closest to 10 in 5 ... ... ... ... is 5 ...press enter... The closest to 10 in ... 8 ... ... ... is 8 ...press enter... The closest to 10 in 5 8 ... ... ... is 8 ...press enter... The closest to 10 in ... ... 15 ... ... is 15 ...press enter... The closest to 10 in ... ... ... 21 ... is 21 ...press enter... The closest to 10 in ... ... ... ... 11 is 11 ...press enter... The closest to 10 in ... ... ... 21 11 is 11 ...press enter... The closest to 10 in ... ... 15 21 11 is 11 ...press enter... The closest to 10 in 5 8 15 21 11 is 11 ...press enter... The closest to 10 in [5, 8, 15, 21, 11] is 11 ...press enter...

The last part of the program should produce output like this (but with different numbers):

The closest to 500 in 133 ... ... ... ... ... ... ... is 133 ...press enter... The closest to 500 in ... 887 ... ... ... ... ... ... is 887 ...press enter... The closest to 500 in 133 887 ... ... ... ... ... ... is 133 ...press enter... The closest to 500 in ... ... 194 ... ... ... ... ... is 194 ...press enter... The closest to 500 in ... ... ... 297 ... ... ... ... is 297 ...press enter... The closest to 500 in ... ... 194 297 ... ... ... ... is 297 ...press enter... The closest to 500 in 133 887 194 297 ... ... ... ... is 297 ...press enter... The closest to 500 in ... ... ... ... 59 ... ... ... is 59 ...press enter... The closest to 500 in ... ... ... ... ... 442 ... ... is 442 ...press enter... The closest to 500 in ... ... ... ... 59 442 ... ... is 442 ...press enter... The closest to 500 in ... ... ... ... ... ... 934 ... is 934 ...press enter... The closest to 500 in ... ... ... ... ... ... ... 439 is 439 ...press enter... The closest to 500 in ... ... ... ... ... ... 934 439 is 439 ...press enter... The closest to 500 in ... ... ... ... 59 442 934 439 is 442 ...press enter... The closest to 500 in 133 887 194 297 59 442 934 439 is 442 ...press enter... The closest to 500 in [133, 887, 194, 297, 59, 442, 934, 439] is 442

Submit this/these files:


SUBMIT   /   Submission Summary