L12

Due by the end of Tuesday, 6 April 2021


SUBMIT   /   CHECK

Today's Activities

Programming Activity 1
Download the file OneFern.java. (This is a simpler program than the one from class -- it doesn't allow you to choose the size of the fern it draws.) Compile and run the program. Examine the picture of the fern.
Examine the program code.

Revise the code so that it produces a picture like the following:

The changes are: These changes are simple. Most of it can be done by just changing numbers already in the code.

Programming Activity 2
For this activity you are going to implement a recursive function. This function calculates the number of ways of choosing r distinct elements from a set of n distinct elements. For example, the number of different ways of choosing five different cards from a deck of fifty-two cards (which is, FYI, 2 598 960).

Download the file Combinations.java. Complete the definition of the recursive method static int numCombinations(int n, int r). The recursive definition is:

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
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:
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:

5 choose 0 = 1 5 choose 1 = 5 5 choose 2 = 10 5 choose 3 = 10 5 choose 4 = 5 5 choose 5 = 1 5 choose 6 = 0 6 choose 0 = 1 6 choose 1 = 6 6 choose 2 = 15 6 choose 3 = 20 6 choose 4 = 15 6 choose 5 = 6 6 choose 6 = 1 7 choose 0 = 1 7 choose 1 = 7 7 choose 2 = 21 7 choose 3 = 35 7 choose 4 = 35 7 choose 5 = 21 7 choose 6 = 7 8 choose 0 = 1 8 choose 1 = 8 8 choose 2 = 28 8 choose 3 = 56 8 choose 4 = 70 8 choose 5 = 56 8 choose 6 = 28 The total number of distinct poker hands is 2,598,960.

Submit this/these files:

You will be graded on the following:

  1. OneFernApplication has been submitted, revised, and it compiles.
  2. ... the stem is the correct length
  3. ... the fronds are the correct size
  4. ... the angles are correct
  5. Combinations.java has been submitted, revised, and it compiles.
  6. ... numCombinations is a self-contained, recursive method that:
  7. ... ... returns the correct value when r < 0
  8. ... ... returns the correct value when n < r
  9. ... ... returns the correct value when r == 0
  10. ... ... returns the correct value when n == r
  11. ... ... returns the correct value in other comditions
  12. Code meets the requirements for style and design

SUBMIT   /   CHECK