Write a program that evaluates the random distribution of a die(singular for dic
ID: 3532613 • Letter: W
Question
Write a program that evaluates the random distribution of a die(singular for dice). The game should first prompt the client for a number of die throws that he would like. The game then throws the die that many times. The game records the number of times each die number appears on top and calculates the average for the six values. The program then computes the percent difference between the average and the number for each die face value. Do not use arrays. Guarantee the number entered is not less than zero. Use at least three methods (you might find more than three useful): One for input, one for processing, and one for output. I already have a GameDemo that uses the method play() as displayed here:
public void play()
{
input();
process();
output();
}
A sample run of the game would look like this:
This game requests a number from the player.
The game then throws the die that number of times.
The game then displays the percentage difference between the number for each die value and the average.
Please enter the number of times that you would like the computer to throw the die. 1200
1200 divided by 6 is 200 which is the average for each die face.
The percentage from the average for each die number is:
Die number 1 landed 206 times and is 3% above the average.
Die number 2 landed 204 times and is 2% above the average.
Die number 3 landed 194 times and is 3% below the average.
Die number 4 landed 192 times and is 4% below the average.
Die number 5 landed 193 times and is 3.5% below the average.
Die number 6 landed 211 times and is 5.5% above the average.
Explanation / Answer
import java.util.*;
public class Mukesh {
public static void Input()
{
System.out.println("This game requests a number from the player.");
System.out.println("The game then throws the die that number of times.");
System.out.println("The game then displays the percentage difference between the number for each die value and the average.");
System.out.println("Please enter the number of times that you would like the computer to throw the die.");
}
public static void Process()
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
while(true)
{
if(n>=1)
{
break;
}
System.out.println("Please enter the value greater than zero");
n=s.nextInt();
}
float average=n/6;
Random rand=new Random();
int a1=0,a2=0,a3=0,a4=0,a5=0,a6=0;
for(int i=0;i<n;i++)
{
int randomNumber=rand.nextInt(6)+1;
if(randomNumber==1)
{
a1=a1+1;
}
else if(randomNumber==2)
{
a2=a2+1;
}
else if (randomNumber==3)
{
a3=a3+1;
}
else if (randomNumber==4)
{
a4=a4+1;
}
else if(randomNumber==5)
{
a5=a5+1;
}
else if (randomNumber==6)
{
a6=a6+1;
}
}
Output(a1,a2,a3,a4,a5,a6,average);
}
public static void ComputeAndOutput(int a1,float average, int g)
{
if(a1>average)
{
System.out.println("Die number " +g+" landed "+a1+" times and is "+((a1-average)*100/average)+"% above the average.");
}
else if(a1 < average)
{
System.out.println("Die number " +g+" landed "+a1+" times and is "+((a1-average)*100/average)*(-1)+"% below the average.");
}
else
{
System.out.println("Die number "+g+" landed "+a1+" times and is equal to average");
}
}
public static void Output(int a1,int a2,int a3,int a4,int a5,int a6,float average )
{
ComputeAndOutput(a1,average,1);
ComputeAndOutput(a2,average,2);
ComputeAndOutput(a3,average,3);
ComputeAndOutput(a4,average,4);
ComputeAndOutput(a5,average,5);
ComputeAndOutput(a6,average,6);
}
public static void Play()
{
Input();
Process();
}
public static void main(String[] args)
{
Play();
}
}
THIS IS THE CORRECT CODE!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.