Advertisement

03.24.2005 at 07:39PM PST, ID: 21364266
[x]
Attachment Details

How to write a frequency array

Asked by nikese in Miscellaneous Programming

Tags: dice

I have three parts to a program in which one of them I need help figureing out how to write the frequency array. The first two parts that I don't have a probelm with are as follows:

public class DriverProgram
{

public static void main(String[] args)
  {
    char choice;           // user's choice of what he/she wants to do
    boolean done = false;  // flag that tells whether user wants to quit

    DiceSimulation diceSimulation = new DiceSimulation();

    System.out.println("Welcome to Nikese's dice throwing simulator!\n");

    do
    {
      System.out.println("Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit");
      System.out.print("Enter n, a, p, or q ==> ");
      choice = NikeseSwift.readChar();

      switch (choice)
      {
        case 'n': case 'N':
          diceSimulation.newSimulation();
          break;
        case 'a': case 'A':
          diceSimulation.additionalRolls();
          break;
        case 'p': case 'P':
          diceSimulation.printReport();
          break;
        case 'q': case 'Q':
          done = true;
          break;
        default:
          System.out.println("Invalid selection.");
      } // end switch
    } while (!done);
  } // end main
}

*******************************************8

import java.io.*;
import java.util.*;

public class NikeseSwift

{

      //The following is for input of type string

          /**
           Reads a line of text and returns that line as a String
           value. The end of a line must be indicated either by a
           new-line character '\n' or by a carriage return '\r'
           followed by a new-line character '\n'. (Almost all systems
           do this automatically. So you need not worry about this
           detail.) Neither the '\n', nor the '\r' if present, are
           part of the string returned. This will read the rest of a
           line if the line is already partially read.
          */
          public static String readLine( )
          {
              char nextChar;
              String result = "";
              boolean done = false;

              while (!done)
              {
                  nextChar = readChar( );
                  if (nextChar == '\n')
                     done = true;
                  else if (nextChar == '\r')
                  {
                      //Do nothing.
                      //Next loop iteration will detect '\n'.
                  }
                  else
                     result = result + nextChar;
              }

              return result;
    }


      //The following is for input of type integer

          /**
           Precondition: The user has entered a number of type int
           on a line by itself, except that there may be
           whitespace before and/or after the number.
           Action: Reads and returns the number as a value of type
           int. The rest of the line is discarded. If the input is
           not entered correctly, then in most cases, the user will
           be asked to reenter the input. In particular, this
           applies to incorrect number formats and blank lines.
          */
          public static int readLineInt( )
          {
              String inputString = null;
              int number = -9999;//To keep the compiler happy.
                                 //Designed to look like a garbage value.
              boolean done = false;

              while (! done)
              {
                  try
                  {
                      inputString = readLine( );
                      inputString = inputString.trim( );
                      number = Integer.parseInt(inputString);
                      done = true;
                  }
                  catch (NumberFormatException e)
                  {
                      System.out.println(
                               "Your input number is not correct.");
                      System.out.println(
                               "Your input number must be");
                      System.out.println(
                               "a whole number written as an");
                      System.out.println(
                                "ordinary numeral, such as 42");
                      System.out.println("Minus signs are OK,"
                                + "but do not use a plus sign.");
                      System.out.println("Please try again.");
                      System.out.println("Enter a whole number:");
                  }
              }

              return number;
    }

    //The following is for input of type char

        /**
           Reads the next input character and returns that character.
           The next read takes place on the same line where this
           one left off.
          */
          public static char readChar( )
          {
              int charAsInt = -1; //To keep the compiler happy.
              try
              {
                  charAsInt = System.in.read( );
              }
              catch(IOException e)
              {
                  System.out.println(e.getMessage( ));
                  System.out.println("Fatal error. Ending program.");
                  System.exit(0);
              }

              return (char)charAsInt;
    }

    //The following is for input of type double

    /**
           Precondition: The user has entered a number of type
           double on a line by itself, except that there may be
           whitespace before and/or after the number.
           Action: Reads and returns the number as a value of type
           double. The rest of the line is discarded. If the input
           is not entered correctly, then in most cases, the user
           will be asked to reenter the input. In particular, this
           applies to incorrect number formats and blank lines.
          */
          public static double readLineDouble( )
          {
              String inputString = null;
              double number = -9999;//To keep the compiler happy.
                            //Designed to look like a garbage value.
              boolean done = false;

              while (! done)
              {
                  try
                  {
                      inputString = readLine( );
                      inputString = inputString.trim( );
                      number = Double.parseDouble(inputString);
                      done = true;
                  }
                  catch (NumberFormatException e)
                  {
                      System.out.println(
                               "Your input number is not correct.");
                      System.out.println(
                               "Your input number must be");
                      System.out.println(
                                "an ordinary number either with");
                      System.out.println(
                                 "or without a decimal point,");
                      System.out.println("such as 42 or 9.99");
                      System.out.println("Please try again.");
                      System.out.println("Enter the number:");
                  }
              }

              return number;
    }

    //The following is for input of type boolean

    /**
           Input should consist of a single word on a line, possibly
           surrounded by whitespace. The line is read and discarded.
           If the input word is "true" or "t", then true is returned.
           If the input word is "false" or "f", then false is
           returned. Uppercase and lowercase letters are considered
           equal. If the user enters anything else (e.g., multiple
           words or different words), the user is asked
           to reenter the input.
          */
          public static boolean readLineBoolean( )
          {
              boolean done = false;
              String inputString = null;
              boolean result = false;//To keep the compiler happy.

              while (! done)
              {
                  inputString = readLine( );
                  inputString = inputString.trim( );
                  if (inputString.equalsIgnoreCase("true")
                         || inputString.equalsIgnoreCase("t"))
                  {
                      result = true;
                      done = true;
                  }
                  else if (inputString.equalsIgnoreCase("false")
                              || inputString.equalsIgnoreCase("f"))
                  {
                      result = false;
                      done = true;
                  }
                  else
                  {
                      System.out.println(
                               "Your input is not correct.");
                      System.out.println("Your input must be");
                      System.out.println("one of the following:");
                      System.out.println("the word true,");
                      System.out.println("the word false,");
                      System.out.println("the letter T,");
                      System.out.println("or the letter F.");
                      System.out.println(
                               "You may use either upper-");
                      System.out.println("or lowercase letters.");
                      System.out.println("Please try again.");
                      System.out.println("Enter input:");
                  }
               }

              return result;
    }

}

*****************************

This is the third one that I am missing most parts of because I need some help in writing the frequency arrays:

import java.util.*;

public class DiceSimulation //DiceSimulation class
{
      int die1, die2, sum, rolls, countRolls;

public void newSimulation() //newSimulation method
{
      System.out.println("How many rolls would you like to simulate?"); //outputs sentence to screen
      rolls = NikeseSwift.readLineInt();

      
      for (int i=0; i<rolls; i++)
      {
            die1 = (int)(Math.random()*6) + 1;  //die1 rolls
            die2 = (int)(Math.random()*6) + 1;      //die2 rolls
            countRolls++;                       //counts the roll
      }
}

public void additionalRolls() //additionalRolls method
{
      System.out.println("How many dice rolls would you like to add to the current simulation?"); //outputs sentence to screen
      rolls = NikeseSwift.readLineInt();

      
      for (int i=0; i<rolls; i++)
      {
            die1 = (int)(Math.random()*6) + 1;  //die1 rolls
            die2 = (int)(Math.random()*6) + 1;      //die2 rolls
            countRolls++;                       //counts the roll
      }
}

public void printReport() //printReport method
{
      System.out.println("DICE ROLLING SIMULATION RESULTS");
      System.out.println("Each \"*\" represents 1% of the total number of dice rolls.");
      System.out.println("Total number of dice rolls =" + sum);
}

}Start Free Trial
[+][-]03.24.2005 at 10:12PM PST, ID: 13628154

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.24.2005 at 11:59PM PST, ID: 13628395

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Miscellaneous Programming
Tags: dice
Sign Up Now!
Solution Provided By: sapbucket
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.25.2005 at 12:00AM PST, ID: 13628399

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 10:39AM PST, ID: 13632139

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 03:09PM PST, ID: 13634053

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 03:12PM PST, ID: 13634068

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 03:14PM PST, ID: 13634074

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 03:24PM PST, ID: 13634110

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 03:34PM PST, ID: 13634158

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 03:59PM PST, ID: 13634240

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.25.2005 at 04:01PM PST, ID: 13634244

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.15.2005 at 03:16AM PDT, ID: 14219343

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]06.15.2005 at 12:10PM PDT, ID: 14224806

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.20.2005 at 09:06AM PDT, ID: 14257954

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]06.24.2005 at 01:17AM PDT, ID: 14291810

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32