CSE1030Z Lab 01

Wed Jan 9 2:30-4:00 PM

Introduction

The purpose of this lab is to get students back into the practice of programming in the CSE lab environment, to clarify the style rules expected of your programs, and to help train the teaching assistants.

The lab also introduces the idea of writing a computer simulation to solve or explore a problem. Computer simulations are used widely in the sciences and engineering to study problems that are hard or impossible to solve with absolute certainty: for example, climate simulations are used to study the potential effects of global warming; computational fluid dynamics are used to simulate the flow of air around an airplane or rocket; many scientists are trying to simulate the human brain on a computer; and your professor uses simulations to study positioning errors in computer-assisted surgery. This lab uses simulation to study the behavior of some playing dice; the problems studied in the lab are simple, but sufficient to illustrate many aspects common to creating more sophisticated simulations.

There are two questions to this lab. You need to submit only the first question by the end of the lab session, but the solution to the second question may be useful for Lab 02.

Style Rules

The style rules are not overly restrictive in CSE1030.

1. Your programs should use the normal Java conventions (class names begin with an uppercase letter, variable names begin with a lowercase letter, etc.).

2. In general, use descriptive variable names. There are exceptions to this rule; for example, traditional loop variables are often called i, j, k, etc.

3. Use a consistent indentation size. Beware of the TAB vs SPACE problem: Tabs have no fixed size; one editor might interpret a tab to be 4 spaces and another might use 8 spaces. If you mix tabs and spaces, you will have indenting errors when your code is viewed in different editors.

4. Use a consistent brace style:

// left aligned braces

class X
{
  public void someMethod()
  {
    // ...
  }
  
  public void anotherMethod()
  {
    for (int i = 0; i < 1; i++)
    {
      // ...
    }
  }
}

or

// ragged braces

class X {
  public void someMethod() {
    // ...
  }
  
  public void anotherMethod() {
    for (int i = 0; i < 1; i++) {
      // ...
    }
  }
}

5. This one always causes problems for students. Insert a space around operators (except the period ".").

The following is

    // some code somewhere
    boolean isBetween = (x > MIN_VALUE) && (x > MAX_VALUE);
    int someValue = x + y * z;

much easier to read than this

    // AVOID DOING THIS
    
    // some code somewhere
    boolean isBetween=(x>MIN_VALUE)&&(x>MAX_VALUE);
    int someValue=x+y*z;

Dice

Dice are used in many games involving chance. The most common form of dice are 6-sided with the sides, or faces, being numbered sequentially from 1 through 6; however, many different variations are possible.

The class Die (die is the singular form of dice) represents a die having at least 2 sides with the faces numbered sequentially starting at 1. The API for the Die class can be found here.

If you are using Eclipse, download the jar file for the class and install it in Eclipse.

If you are using a text editor (such as jEdit) then the jar files have already been installed system-wide for you.

Question 1

Write a CSE1020 style program called Lab01A that tries to determine if a die created by the Die class is fair. One way to determine if the die is fair is to roll it many times and count the number of times each face is rolled; all of the faces should occur almost the same number of times if the die is fair.

Your program should:

  1. Create a single 6-sided die.
  2. Roll the die 6,000 times, counting the number of times each face occurs.
  3. Output the number of times each face occurs as shown below. (Note: your numbers will be different between runs of your program because the die generates random face values.)
1 : 1016
2 : 977
3 : 1000
4 : 1050
5 : 966
6 : 991

If you run your program several times, you should see that each of the possible face values occurs approximately 1,000 times, which suggests that the die is more or less fair. You should take a moment to see what happens if you roll the die a much smaller number of times (like 60) or a much larger number of times (like 6,000,000).

The easy way to solve this problem is to create 6 variables, one variable to count the occurrence of the each of the possible face values. You are encouraged not to solve the problem this way. Try to write your program so that it is easy to change to determine if an N-sided die is fair (where N might be a large number). Hint: Consider using an array, list, or map to store the counts of each face value.

Submit your solution using the submit command:

submit 1030Z L1W Lab01A.java

Question 2

Most games use two or more dice. One such game is Yahtzee where the goal is to try to roll certain combinations of values.

The game of Yahtzee uses five 6-sided dice. In Yahtzee, a large straight occurs when the player rolls 5 sequential values (i.e., 1-2-3-4-5 or 2-3-4-5-6).

Write a CSE1020 style program called Lab01B that tries to determine the odds of rolling a large straight; your program should do this by simulating the rolling of five dice many times and counting the number of times that a large straight occurs.

Your program should:

  1. Create five 6-sided dice (perhaps grouping them in a list...).
  2. Roll the group of five dice 1,000,000 times, counting the number of times a large straight occurs.
  3. Output the number of times a large straight occurs divided by the number of times the dice were rolled (i.e., 1,000,000); you should use printf to format the output to six decimal places. An example run of the solution is shown below. (Note: your output will be different between runs of your program because the dice generate random face values.)
0.030753

Again, you are encouraged to try to create a solution that is easy to generalize; for example, it should be easy to modify your program so that you can find the odds of rolling a straight of nine sequential values using ten 10-sided dice.

The output suggests that the player has about a 3% chance of rolling a large straight with a single roll of all five dice. The actual odds are 240/7776, which is approximately 0.030864. Notice that the simulation result does not exactly match the actual value. This is a common feature of computer simulations.

Also notice that when you run your program several times you get a somewhat different result each time; this is referred to as the variance of the simulation, and occurs because you are simulating a random process. You can decrease the simulation variance by increasing the number of times you roll the dice in your simulation. In this case, the difference between the simulated result and actual value will also decrease if you increase the number of times you roll the dice. Of course, the amount of time it takes to run the simulation will increase.

You do not need to submit your program, but you may need your solution for next week's lab...