Intro to CS110 Project 2

due April 7 3PM

Betty O’Neil, from an old project of Bob Wilson

 

Note: this is only part of the full document, just what you need to understand to get started. Please read the full document posted on the class web site.

 

We are going to generate a histogram showing the frequency of occurrence for the sum of two dice rolls.  We will display the histogram in the form of two bar graphs – one horizontal and one vertical.  This project will require you to write several loops that process the integer variables in an array.

 

You will use the Lewis and Loftus code for the Die class.  You will not be modifying this code.  You should read it and understand how it works.  You will write code in the main method of a class named DiceStats and in three methods of a class named Histogram. 

 

You can find an initial version of the DiceStats.java, Histogram.java, and Die.java source files in Project2.zip.  As always, download and unzip this file on your removable media and create/save a Dr Java project file in the Project2 directory.  The DiceStats.java and Histogram.java file are incomplete and you need to complete them in this project. 

 

Sample Output:

 

> java DiceStats

How many dice rolls do you want?

 [1000]

Estimated probabilities for outcome of the first roll:

Win:          0.214

Lose:         0.119

Roll again:   0.667

 

Value 2:  ******* 7

Value 3:  ********** 10

Value 4:  ************* 13

Value 5:  ************************** 26

Value 6:  ****************************** 30

Value 7:  ************************************ 36

Value 8:  *************************** 27

Value 9:  *********************** 23

Value 10: ******************* 19

Value 11: ********* 9

Value 12: ****** 6

 

Plus a vertical graph—see project doc


 

 

Turn in the three .java files and Report (memo.txt)

 

For this project and all projects in this course, the memo.txt file that you upload to the turn-in system MUST BE A PLAIN TEXT FILE - NOT A WORD (.DOC or .RTF) FILE.  On a Windows PC, I recommend that you use Notepad to create this file.  On a MAC, you must use a suitable editor to create a plain text file.

 

Write a report that answers the following questions: see full document.

 

Turning in your Assignment:

 

Use the secure file transfer program to upload your Project2 folder containing your DiceStats.java, Histogram.java, and memo.txt files to your UNIX file directory: “account/cs110/Project2” where “account” is your account ID. 

 

You must upload the files to our UNIX system before the posted deadline (April 7).  You will get no credit for late submissions this time, that is, ones submitted after April 7. You can get help submissions in class on April 7, and if all else fails, you may email your files to eoneil@cs.umb.edu on April 7. If you have not finished, submit your files on time anyway - even if the program does not work correctly yet.  That way you are eligible for part credit on the assignment.  If you upload your files more than once, only the most recent copy will be saved. 

 

//********************************************************************

//  Die.java       Author: Lewis/Loftus

//

//  Represents one die (singular of dice) with faces showing values

//  between 1 and 6.

//********************************************************************

 

public class Die

{

   private final int MAX = 6;  // maximum face value

 

   private int faceValue;  // current value showing on the die

 

   //-----------------------------------------------------------------

   //  Constructor: Sets the initial face value.

   //-----------------------------------------------------------------

   public Die()

   {

      faceValue = 1;

   }

 

   //-----------------------------------------------------------------

   //  Rolls the die and returns the result.

   //-----------------------------------------------------------------

   public int roll()

   {

      faceValue = (int)(Math.random() * MAX) + 1;

 

      return faceValue;

   }

 

   //-----------------------------------------------------------------

   //  Face value mutator.

   //-----------------------------------------------------------------

   public void setFaceValue (int value)

   {

      faceValue = value;

   }

 

   //-----------------------------------------------------------------

   //  Face value accessor.

   //-----------------------------------------------------------------

   public int getFaceValue()

   {

      return faceValue;

   }

 

   //-----------------------------------------------------------------

   //  Returns a string representation of this die.

   //-----------------------------------------------------------------

   public String toString()

   {

      String result = Integer.toString(faceValue);

 

      return result;

   }

}

 

import java.util.Scanner;

 

public class DiceStats

{

  private static final int MIN_ROLL = 2;         // minimum value of a roll

  private static final int MAX_ROLL = 12;        // maximum value of a roll

  private static final int MAX_LENGTH = 36;      // maximum length of bars

 

   /**

   * main method:

   * ask the user how many dice rolls to make during this run

   * count occurances for each value of the throw of the dice

   * and draw a horizontal and vertical histogram of the counts

   */

 

  public static void main(String[] args)

  {

    // step 1:

    // declare an int array named "counts" to count dice roll occurences

    // ignore the 0th and 1st array elements (those values can't occur)

 

 

    // step 2:

    // initialize all of the values in the array to 0

 

 

 

    // instantiate two dice: myDie1 and myDie2

 

    Die myDie1 = new Die();

    Die myDie2 = new Die();

 

    // ask user for the number of rolls to make during this run

 

    Scanner scan = new Scanner(System.in);

    System.out.println("How many dice rolls do you want?");

    int total = scan.nextInt();

 

    // step 3:

    // roll the two dice and count the total of the two rolls n times

 

   

 

    // step 4:

    // print out the estimated probabilities of win, lose, and roll again

   

   

   

    

    // step 5:

    // instantiate an object of the Histogram class and

    // call its two draw methods to draw the two histograms

    // with the array to be drawn, the indices of valid data,

    // and the maximum length of the bars in the histogram

   

   

   

   

  }

}

 

 

Histogram.java: see project files