Due by the end of Tuesday, 6 April 2021
Examine the program code.![]()
Revise the code so that it produces a picture like the following:
The changes are:![]()
Download the file Combinations.java. Complete the definition of the recursive method static int numCombinations(int n, int r). The recursive definition is:
You don't actually, need to understand that definition -- you just need to translate it to Java. On the other hand, if you are interested, here's the reasoning:
numCombinations(n, r) = { 0 if r < 0 OR n < r { 1 if r = 0 OR n = r { numCombinations(n-1,r-1) + numCombinations(n-1,r) otherwise
The easy case is if you want to choose all of them or none of them. In either of these cases, there is only one way to do it, so the number of combinations is just 1. Even easier, tho, is if you want to choose more items than there are available -- there's no way to do that (so the number of combinations is zero). Similarly, if you want to choose a negative number of items, there is no way to do it.For any other case, we break the problem down into smaller but similar problems. If you have to pick r elements from n elements, then you can either:
- pick the first of the n elements and r-1 of the rest,
or- skip the first of the n elements and pick r of the rest.
Adding those two numbers together gives you all the different ways to choose r items from a set of n items.
And here is the output you should see if you did it all correctly:
Submit this/these files:
You will be graded on the following: