CS401 Lab 4: Using and Writing Methods

This lab is NOT graded


Introduction

In order to understand classes and objects better and to get used to using them in programs, it is a good idea to practice with them.  Soon we will build our own classes, but for now we will just work with some predefined classes in Java and write some static methods.


Background

Java has many predefined classes that we can use in our programs in two ways:

  1. Call helpful static utility methods that we otherwise would have to write ourselves.  Recall from the lecture notes that static methods are associated with an entire class rather than with individual objects (which is why they are also called class methods).  Recall also that static methods are invoked through the class directly, so we would call them by ClassName.methodName().  For example, the Math class contains a number of static methods to calculate common functions such as Math.abs(), Math.pow() and Math.sin().
  2. Create objects that we can also use in our programs.  We have already seen an example of this with the Scanner class -- we used a Scanner object to read tokens from the input stream in a logical way.

Before doing this lab, refer to the course lecture notes and Chapter 5 in the Gaddis text for more background on Java static methods.


Math Class

The Math Class is a predefined class that contains many useful static methods.  We have used it already in several examples discussed in lecture.  For details on the Math class see the Java API.  Note that many of the methods have multiple versions to accommodate the different primitive Java types. You will not need the Math class to complete this lab exercise, but it is useful be familiarize yourself with the class nonetheless.


Random Class

The Random class in Java is a predefined class that enables the programmer to generate pseudo-random numbers.  These are useful for simulations and scientific experiments.  However, unlike the Math class, the methods in the Random Class are instance methods, not static methods.  Thus, to use it we must first create a Random object, then use that object to generate our random numbers.  Look up the Random class in the Java API and note that many of the methods have the same name as those we saw in the Scanner class.  This is because in a way they are similar -- objects of both Scanner and Random produce sequences of values, but the Scanner class obtains them from the input stream while the Random class generates them using an algorithm.


Exercise

Write a Java program that will simulate rolling 2 six-sided dice, and keep track of how many times each possible roll (2, 3, ... 12) occurs.

First "roll" the dice 100 times and calculate the fraction of each of the values (2, 3, ... 12).  Compare these fractions with the probabilistic values for each number:

Value

Fraction

2

1/36

3

2/36

4

3/36

5

4/36

6

5/36

7

6/36

8

5/36

9

4/36

10

3/36

11

2/36

12

1/36

Next "roll" the dice 100000 times and calculate the fractions again.  Again compare them to the probabilistic values.  Do they match up better with the values this time?  Make sure you understand why.


Details

Complete your program in the following way:

Write a static void method called RollDice that has two parameters: an int and a Random.  The int parameter determines how many times to roll the dice and the Random is used to generate the actual values (note: we could make the Random variable local to the method, but that would create a new object with each call, which is not necessary).  Think carefully about which method in Random to call and how to appropriately generate the actual roll values.  In the method do the "rolls" and count how many times each number comes up.  Then print out the number of times each number comes up and its fraction out of all of the rolls.

In the main program create the Random object, then enter a conditional loop.  At each iteration of the loop ask the user to enter the number of rolls desired and call RollDice with the appropriate parameters.  Then ask the user if he/she wants to continue.  If so, repeat the process; if not terminate the program


Grading and Submission

This program is not graded, but it will definitely benefit you to get it working.  If you have trouble, ask your TA.