Freshman Honors Seminar: Exercise 1

Problem 1: Function Tables

Several of the samples on the FHS site focus on computation of functions. This is because functions are familiar from courses on mathematics and because functions lend themselves to showing basic patterns of computation.

Two of the function samples, Fibonacci Variations and Table of Factorials, illustrate dealing with discrete functions that produce integer values that may be larger than what may be represented in the primitive types int or long. This leads to the use of either the Java class BigInteger or the JPT class XBigInteger.

The goal of this problem is to have you imitate the Table of Factorials sample and create methods to make tables for two additional functions. You may use the file Methods.java from that sample as a starter file or you can just as easily make a fresh file. In the factorials example, we show how to create the table using either BigInteger or XBigInteger. It is sufficient for you to choose one approach and implement it.

So, we want you make a method that will compute tables for:

Perhaps the simplest way to describe this problem in more detail is to give you the comments and header from our code.

First, 2 raised to the power n.

    /**
     * Table of 2-to-the-power-n.
     * 
     * Output for n = 10:
     * 
     * 2 to power 0 = 1
     * 2 to power 1 = 2
     * 2 to power 2 = 4
     * 2 to power 3 = 8
     * 2 to power 4 = 16
     * 2 to power 5 = 32
     * 2 to power 6 = 64
     * 2 to power 7 = 128
     * 2 to power 8 = 256
     * 2 to power 9 = 512
     * 2 to power 10 = 1024
     */
    public void TableOfPowerOf2(int n) { ... }

Second, the binomial coefficient B(n,k).

    /**
     * The binomial coefficients B(n,k) arise as the coeffients
     * in the expansion of (x + y) to the power n.
     * 
     * B(n,0) = 1.
     * 
     * B(n,k) = B(n,k-1) * (n-k+1) / k, for 0 < k <= n.
     * 
     * Equivalently, B(n,k) = n!/(k! * (n-k)!), but the formulas
     * above are more efficient for computing an entire table.
     * 
     * Output for n = 10:
     * 
     * B(10,0) = 1
     * B(10,1) = 10
     * B(10,2) = 45
     * B(10,3) = 120
     * B(10,4) = 210
     * B(10,5) = 252
     * B(10,6) = 210
     * B(10,7) = 120
     * B(10,8) = 45
     * B(10,9) = 10
     * B(10,10) = 1
     * 
     * For example, the coeffient of x^4 * y^6 in (x + y)^10 is
     * 210.
     */
    public void BinomialTable(int n) { ... }

You can see that for each table, I have provided example output as well as a discussion of what the computation should do. This is an example of where the methodology taught in Fundamentals really pays off.

Problem 2: Binomial Revisited

In solving Problem 1, you never actually define a “Binomial function”. Rather, you accumulate mathematical results and print them while taking advantage of prior computations to get the next value. In this problem, we want you to define such a function. Depending on whether you want to use the Java class BigInteger or the JPT class XBigInteger, the header will be:

    public static BigInteger Binomial(int n, int k)

or

    public static XBigInteger Binomial(int n, int k)

The keyword static simply means that the function is a stand-alone function that may be called without building another object. Mathematical functions are usually static so rather than build bad habits we introduce the keyword here.

Note that you must make a choice here. You cannot define two functions with the same name and argument types that differ only in the type of the return value.

Some additional specification and suggestions:

Speaking of efficiency, you will later learn that there are ways to save information from one binomial computation to be used in another such computation. This is called “memoization”.

Binomial in Scheme

For comparison, you can see how the binomial problems are solved in scheme, by clicking on: Binomial In Scheme .

Problem 3: Dice and Random Throws

In the Playing Cards sample program, you can see how to load images from the JPT web site. There is a a directory there with dice images:

http://www.ccs.neu.edu/jpt/images/dice/

In this directory, there are 7 images named die0.jpg ... die6.jpg. The image “die0.jpg” is blank and acts as a placeholder so that when the other images are loaded then there index in the array is equal to their value as one of 6 dice possibilities. Here is a snapshot that shows all 7 images.

All 7 dice images

We want you make a simple program that will use random numbers to select 2 dice values and then display the corresponding pair of images on the screen.

When the program starts up, it should look something like this:

Dice initial state

After one or more tosses of the dice, it should look something like this:

Dice after toss

To show you that getting the dice images is simple, we will give you the code for the program that displayed all 7 dice images shown above.

public class Methods extends JPF 
{
    
    public static void main(String[] args) { 
        LookAndFeelTools.adjustAllDefaultFontSizes(5);
        
        new Methods();
    }
    
    /** The dice URL. */
    private String diceURL  =
        "http://www.ccs.neu.edu/jpt/images/dice/";
    
    /** The image list file name for reading the dice. */
    private String diceList =
        "imagelist.txt";
    
    /** The dice as an ImagePaintableLite[]. */
    private ImagePaintableLite[] dice =
        WebImageTools.readImagesAsPaintableLite
            (diceURL, diceList);
    
    /** The size of the dice array. */
    private int N = dice.length;
    
    
    public void ShowAllDice() {
        int gap = 10;
        HTable table = new HTable(dice, gap, gap, CENTER);
        
        if (N == 0)
            table.addObject
                ("Dice images failed to load from the web", 0, 0);
        
        table.setBackground(Colors.tan);
        table.frame("All Dice");
    }
}

The above program reads all dice images into an array and then loads the entire array into a horizontal table which is then framed. You will need to be more subtle since you only want to show 2 dice at a time and you want to be able to change the dice afterwards. Notice, also, how the above program checks the size N of the array and installs an error message if the images could not be loaded. When loading from the web, one can never be certain that the requested task has succeeded.

The key to be able to change dice images is the create two wrappers, specifically Tile objects, and put the wrappers into the GUI. Then afterwards you can change the images in the tile and the GUI will update automatically.

The two tiles are defined as follows as member data.

    /** The tile for die #1. */
    private Tile die1 = null;
    
    /** The tile for die #2. */
    private Tile die2 = null;

Notice that we have only named the tiles here. We have not yet constructed them. That is because until we check N in the constructor we do not know whether or not the dice images loaded.

The constructor looks like this.

    public Dice() {
        if (N == 0) {
            addObject("Dice images failed to load from the web");
            return;
        }
        
        makeTiles();
        makeGUI();
    }

Notice that we don't attempt to make tiles if we have no dice images.

The method makeTiles is also simple.

    /** The method to make the initial blank tiles. */
    private void makeTiles() {
        die1 = new Tile(dice[0]);
        die2 = new Tile(dice[0]);
    }

Initially, the tiles are blank since they both use the blank image that is stored in dice[0]. To change what dice image is showing in a tile, use the pattern that is illustrated with the tile die1.

       die1.setPaintable(dice[a]);

Here a is a random int between 1 and 6 that may be obtained by the method call:

      a = MathUtilities.randomInt(1, 6);

What you need to do is put all this together. There should be a SimpleAction that is used to create the button with the label “Toss Dice”. The two dice tiles should be in a horizontal table and then that table and the simple action should be placed in a vertical table.

You will of course need to code a method private void tossDice() that actually implements the simple action.

The main program method is very simple.

    /** The main program. */
    public static void main(String[] args) {
        new Dice().frame("Random Dice");
    }

The call new Dice() will build the tiles and the Dice program GUI which is then put in a frame with the title “Random Dice”.