• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Can someone help me out with this code

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

package project03;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Dice extends JApplet implements ActionListener
private static final long serialVersionUID = 1L;

//StringBuffer buffer;
int []diceFrequency = new int [13];
int sumOfDice = 0;
boolean firstRoll = true;

//graphical user interface components

JLabel die1Label, die2Label, sumLabel;
JTextField die1Field, die2Field, sumField;
JButton rollButton;
JTextArea resultArea;

//setup graphical user interface components

public void init( )
//obtain content pane; change layout to FlowLayout

Container container = getContentPane( );
container.setLayout( new FlowLayout( ) );

//create label and text field for die 1

die1Label = new JLabel( "Die 1" );
container.add( die1Label );
die1Field = new JTextField( 10 );
die1Field.setEditable( false );

//create label and text field for die 2

die2Label = new JLabel( "Die 2" );
container.add( die2Label );
die2Field = new JTextField( 10 );
die2Field.setEditable( false );

//create label and text field for sum
sumLabel = new JLabel( "Sum is" );
container.add( sumLabel );
sumField = new JTextField( 10 );
sumField.setEditable( false );
container.add( sumField );

//create butter user clicks to roll dice

rollButton = new JButton( "Roll Dice" );
rollButton.addActionListener( this );
container.add( rollButton );

//Create JText Area for the Tally to be displayed
resultArea = new JTextArea(20, 40);
add(new JScrollPane(resultArea), BorderLayout.SOUTH);


//call method play when button is pressed

public void actionPerformed( ActionEvent actionEvent )

//Calling of the Function Roll Dice
rollDice( );

//roll dice, calculate sum and display results

public int rollDice( ) // roll the dice

int die1, die2, sum;
//pick random die values
die1 = 1 + ( int ) ( Math.random( ) * 6 );
die2 = 1 + ( int ) ( Math.random( ) * 6 );
sum = die1 + die2;

//display results

die1Field.setText( Integer.toString( die1 ) );
die2Field.setText( Integer.toString( die2 ) );
sumField.setText( Integer.toString( sum ) );

//return sum of dice
return sum;

} // end method rollDice

public void sumTally(int n )
for (int i = 0; i & 13; i ++)
diceFrequency[i] = 0;

for (int j = 0; j & n ; j ++)

public void printResults()

StringBuffer buffer = new StringBuffer("Dice/Frequency/%\r\n");
for (int die = 0; die & diceFrequency.length; ++die)
buffer.append(die + "\t" + diceFrequency[die] + "\r\n");

resultArea.setText(resultArea.getText() + buffer.toString());
resultArea.setCaretPosition(resultArea.getText(). length());



village idiot
Posts: 1207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Robert! This ain't the right forum for this question. Don't know if it should go in Java in General, or a forum specific to applets. I will figure out where to move it, and do so. Watch for it!
Posts: 43016
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the whole code? I don't see the printResults method being called.
Robert Laryea
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats where I have the problem and the PrintsResults is supposed to use JTextArea. Can you help please. Thanks.
Ulf Dittmer
Posts: 43016
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, printResults does use a JTextarea, it's just never called. What problem do you have specifically with adding a call to printResults to your program?
Are you here to take over the surface world? Because this tiny ad will stop you!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic