In: Computer Science
Write the program to analyze whether a die is fair by counting how often the values 1, 2, ..., 6 appear. • The input is a sequence of die toss values. • The output is a table with the frequencies of each die value, as shown in the following figure.
in java
//TestCode.java
import java.util.Scanner;
public class TestCode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input a sequence of die toss values: ");
String s = scanner.nextLine();
String[] splits = s.split(" ");
int counts[] = new int[6];
int n;
for(int i = 0;i<counts.length;i++){
counts[i] = 0;
}
for(int i = 0;i<splits.length;i++){
n = Integer.parseInt(splits[i]);
counts[n-1]++;
}
System.out.println("Toss value\t\tCount");
System.out.println("----------------------");
for(int i = 0;i<counts.length;i++){
System.out.println((i+1)+"\t\t\t\t"+counts[i]);
}
}
}

