11 Jan 2012 [ 201 week1 ]

Examples

The answers will eventually be filled in after we do them in class.

1: Simple time of day example

Complete the following program so that it prints a time-appropriate greeting. Remember that System.out is the object that represents standard output, and its println method prints a value on its own line.

import java.util.Calendar;

public class Greet {

    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        System.out.println("Hour: " + hour);
        // Print a greeting that's appropriate for the hour of the day:
        if (hour < 6) {
            System.out.println("Damn it, why are you awake already?");
        } else if (hour < 12) {
            System.out.println("Good morning!");
        } else if (hour < 19) {
            System.out.println("Why aren't you taking a nap?");
        } else {
            System.out.println("Damn it Ada, why are you still awake?");
        }
    }
}

2: Random numbers

The Math class contains many static methods that compute mathematical functions. The Math.random method returns a random number (of type double) in the range [0,1).

Complete the following program so that it does a basic test of how uniformly distributed the random numbers are:

public class RandTest {
    public static void main(String[] args) {
        // Generate a million random numbers using the Math.random method, 
        // and print the number that fall in the range [0, 0.1).
        int count = 0; // count the number that fall in range.
        int reps = 1000000;
        for (int n = 0; n < reps; n++) {
            double r = Math.random();
            if (r >= 0 && r < 0.1) {
                count++;
            }
        }
        System.out.println("Count: " + count);
        // Note: must cast count to double to avoid integer division.
        System.out.println("Fraction: " + (double)count/reps);
    }
}

3: Multiplication table

Create a program that prints a multiplication table for the numbers 1 through 5 in binary. Hint: the Integer class has a toBinaryString method that you can use.

public class Multiplication {
    public static void main(String[] args) {
        // Loop through rows, then loop through columns
        for (int row = 1; row <= 5; row++) {
            for (int col = 1; col <= 5; col++) {
                int product = row * col;
                String binary = Integer.toBinaryString(product);
                // System.out.print(binary + "\t");
                System.out.printf("%6s", binary);
            }
            System.out.println();
        }
    }
}

4: Monty Hall problem

On the old game show "Let's make a deal," the host (Monty) would show you (the contestant) 3 doors, with a prize behind one. You have to guess which door the prize is behind. After you make your initial guess, Monty will open one of the remaining doors to show that it does not contain the prize, and then give you the chance to switch your guess.

The question is: is it better for you to stick with your original guess, or better to switch to the remaining door, or does it not make any difference?

To investigate, program a simulation that tests each strategy. Here are some things you'll need to do to design your simulation:

public class Monty {
    public static void main(String[] args) {
        int switchWins = 0;
        int stayWins = 0;
        // Repeat game a million times
        for (int rep = 0; rep < 1e6; rep++) {
            int prize = (int)(Math.random() * 3 + 1);
            int guess = 1;
            // Which door is opened?
            int open; 
            if (prize == 1) {
                open = (int)(Math.random() * 2 + 2);
            } else if (prize == 2) {
                open = 3;
            } else  { // (prize is 3)
                open = 2;
            }
            // Which door do we switch to?
            // open is never 1
            int switchDoor;
            if (open == 2) {
                switchDoor = 3;
            } else { // (open is 3)
                switchDoor = 2;
            }
            // Do we win if we switch?
            if (switchDoor == prize) {
                switchWins++;
            }
            // Do we win if we stay?
            if (guess == prize) {
                stayWins++;
            }
        }
        System.out.println("Switch wins: " + switchWins);
        System.out.println("Stay wins: " + stayWins);
    }
}

Readings

Read chapter 5 (Conditionals and Loops). For now, you can ignore the sections that relate to more advanced data types, such as ArrayList and Iterator types.

Section 1.3 from the Introduction to Programming in Java outline provides a shorter overview of the same material.