This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Write an applet to simulate the rolling of two dice. The program should use Math.random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. The figure below shows the 36 possible combinations of the two dice.
The program shall ...
Use a one dimensional array called diceFrequency to tally the number of times each possible sum appears. The applet should reset the elements of the one dimensional array to zero upon start up. Display the results in a JTextArea in tabular format (similar to Fig 7.7 of the textbook). The applet should use the GUI techniques introduced in Chapter 6. Provide a Roll JButton to allow the user of the applet to roll the dice another n times. Provide a JTextField for the number of rolls each time the Roll button is pressed. The initial (default) number of rolls should be 1000. (Optional) If you feel ambitious, you may add a JCheckBox to reset the frequency array to zero before rolling the dice if checked or accumulate the results if not checked. In your test plan, verify that the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one-sixth of the rolls should be 7 and so no) and your program produces a similar probability distribution given a sufficient number of rolls. The table below shows the likelihood of each Dice roll. Roughly how many rolls does it take for the program output match the Frequency Distribution table shown below.
Can someone please tell me what I'm doing wrong with this code. Its not displaying the output in the JTextArea.