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("Welcom
e to Nikese's dice throwing simulator!\n");
do
{
System.out.println("Option
s: (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.newSimulati
on();
break;
case 'a': case 'A':
diceSimulation.additionalR
olls();
break;
case 'p': case 'P':
diceSimulation.printReport
();
break;
case 'q': case 'Q':
done = true;
break;
default:
System.out.println("Invali
d 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(inputStri
ng);
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.getMe
ssage( ));
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(inputSt
ring);
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.equalsIgnoreC
ase("true"
)
|| inputString.equalsIgnoreCa
se("t"))
{
result = true;
done = true;
}
else if (inputString.equalsIgnoreC
ase("false
")
|| inputString.equalsIgnoreCa
se("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