Question

Please do in a basic Java program and comment so that I understand what is going...

Please do in a basic Java program and comment so that I understand what is going on.

Write a program that gives the statistics of a die being rolled 1000 times! Use a random number generator that picks only the number 1 through 6 and keep tally on each of each time the number is rolled. Store the number of times each number is rolled in the corresponding array index 1-6. For example, you will count the number of times the number 1 is rolled and store that value in index 1.

Note 1: You will not store anything in index 0.
Note 2:
import java.util.Random; //import Random class
Random rand = new Random(); //create instance of Random class
1 + rand.nextInt(10); //generates a random number between 1 and 10

Print the statistics at the end of the program.

Example output
1-563
2-105
3-52
4-201
5-70
6-9

Homework Answers

Answer #1
import java.util.Random;

public class DiceStatistics {

    public static void main(String[] args) {
        Random rand = new Random(); //create instance of Random class
        int[] counts = {0, 0, 0, 0, 0, 0, 0};
        int num;
        for (int i = 0; i < 1000; i++) {
            num = 1 + rand.nextInt(6);  // rolls a number from 1 to 6
            counts[num]++;  // increase the count for num
        }
        for (int i = 1; i <= 6; i++) {
            System.out.println(i + "-" + counts[i]);    // display count for i
        }
    }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please use basic Java program and comment so that I understand what is going on.   write...
Please use basic Java program and comment so that I understand what is going on.   write a class called lab1. (This will be your program.) In that class: Write the main method that calls multiConcat. Write a method called multiConcat that takes a String and an integer as parameters and prints a String that is the parameter string concatenated with itself n number of times (where n is the second parameter). For example, if the parameters are “hi” and 4,...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Please provide commenting of code so I can understand how to solve this problem. Please provide...
Please provide commenting of code so I can understand how to solve this problem. Please provide text files. Using C++ Write a program that reads a given file, and then outputs the contents of it to another file. It should also print out the number of lines and the number of times each alphabetic character appears (regardless of case) in the input file at the end of the output file. It should prompt the user for the input and output...
Need .java programme Write a simulation of the Craps dice game. Craps is a dice game...
Need .java programme Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules:First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...
Please use java language in an easy way with comments! Thanks! Expand your below program, so...
Please use java language in an easy way with comments! Thanks! Expand your below program, so it can include cursed items (weapons that break, armor that causes more damage when worn). In order to do that, write an interface called "CursedItem". Then create subclasses of the armor, and weapon class that implement the CursedItem interface => CursedWeapon: using a random number generator, there is a 4 in 10 chance that the weapon breaks during combat. CursedArmor: this item amplifies the...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
Basic probability for Discrete math problem: (I am mostly interested in part c,d,and e if you...
Basic probability for Discrete math problem: (I am mostly interested in part c,d,and e if you can't do them all. will rate asap) Imagine a fictional scenario where a new area code 229 has just been assigned to Los Angeles. To request a phone number with this new area code, a computer program randomly generates a 7-digit number s1s2s3 − s4s5s6s7. That is for i = 1, 2, ... , 7, a random number generator picks a number from 0...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT