CSCI A202 - Introduction to Programming (II)

Lecture 4: Review Wrap-up (last updated Wed Jan 20)

This is supposed to be a wrap up for the A201 review. The "review" has included many new things, such as object and class terminology and the use of the BreezyGUI package.

By the end of today's lecture we hope to have covered: arrays, do-while, break, continue, switch, the Math functions random and the cast operator.

There should be no mistery left about these things at the end of the lecture.

At the same time we will forge ahead at the object oriented simulations that are supposed to give you a good idea of how classes and objects behave.

1. Random numbers

For our simulations we will use random numbers. Let's get back to the line we wrote in lecture 3 that was intended to give use random numbers between 1 and 6 (integers only, including 1 and 6):

dice = (int)Math.round(Math.random() * 6 + 1);
where dice is an int.

Since Math.random() produces numbers in [0, 1) then

Math.random() * 6
will produce numbers in [0, 6) and if you add 1 to this it means that
Math.random() * 6 + 1
will produce numbers in the interval [1, 7).

Now the news: completely forget the rounding operation.

As Schmalensee put it recently in the Microsoft antitrust trial,

``My immediate reaction is, what could I have been thinking?''

He was talking about a different thing, but if we floor the interval [1, 7) we end up with the following set of integer numbers:

{ 1, 2, 3, 4, 5, 6}
and that's what we wanted.

Taking the integer part of a double can be done with the cast operator:

(int)aDouble
returns the integer part of the aDouble variable (that is a number with decimals in our example).

So if we eliminate the round part altogether in our line we end up with what we wanted in the first place, that is for the integer numbers 1 through 6 to come up with equal probability.

In other words, like throwing a dice (and a fair one at that).

As noted by Joel Mason (e-mail communication) the dice needs to be fair. Not because we want the objects of our Hoosier class to cheer uniformly in one of the three ways we were designing them to cheer, but because we want to make sure that if we need to use this dice again we can safely rely on it being unbiased.

You're right, Joel, using round makes a biased dice.

Fortunately using (int) restores the fairness since casting to an int is equivalent to taking the floor of the number.

2. Taking advantage of it

The good part of all this is that we can now write a better definition for the class Hoosier that we used in our second play, that is, Hoosiers(.java).

We take the original code:

class Hoosier { 
  void cheer() {
    int dice; 
    dice = (int)(Math.random() * 6 + 1); 
    switch (dice) {
      case 1: 
      case 2: System.out.println("I-N-D-I-A-N-A!"); 
              break; 
      case 3: 
      case 4: System.out.println("Go IU Beat Purdue!"); 
              break; 
      case 5: 
      case 6: System.out.println("Hoosiers! Hoosiers!");     
              break; 
    } 
  } 
}
and turn it into:
class Hoosier { 
  void cheer() {
    int dice; 
    dice = random(1, 6); 
    switch (dice) {
      case 1: 
      case 2: System.out.println("I-N-D-I-A-N-A!"); 
              break; 
      case 3: 
      case 4: System.out.println("Go IU Beat Purdue!"); 
              break; 
      case 5: 
      case 6: System.out.println("Hoosiers! Hoosiers!");     
              break; 
    } 
  } 
  int random(int start, int stop) {
    return (int)(Math.random() * stop + start); 
  } 
}
where all the changes have been marked in blue.

3. Arrays

This example throws a fair dice 10 times and fills an array with ints and then calculates their product. It illustrates arrays, for loops, break and continue.

public class Arrays {
  public static void main(String[] args) {
    int[] myArray = new int[10]; 
    for (int i=0; i < myArray.length; i++) 
      myArray[i] = random(1, 6);
    int product = 1;   
    for (int i=0; i < myArray.length; i++) {
      System.out.println(i + ": " + myArray[i]);   
      if      (myArray[i] == 0) { product = 0; break; }
      else if (myArray[i] == 1) { continue; } 
      else                      { product *= myArray[i]; } 
    } 
    System.out.println(product); 
  } 
  static int random(int start, int stop) {
    return (int)(Math.random() * stop + start); 
  } 
} 
More detailed explanation of break and continue will be given in class, although a few things will have to be said here. [...]

We've been carrying our own definition of random around. In later examples we may simply use the line without first coding it into a definition for an instance method, random. But note that random needs to be static here.

4. switch revisited

5. Arrays of Hoosiers

to be continued...

I will update this with the code we wrote in class and a few other notes before Saturday.