View Single Post
Old Aug 15th, 2005, 10:37 AM   #4
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Here's a simple implementation of what you want:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class RandomNumber extends JFrame implements ActionListener {

	private JButton button;
	private JTextField valuesTextField, resultTextField, meanTextField;
	private Random random;
	private int total = 0;
	private int count = 0;
	
	public static void main(String[] args) {
		RandomNumber frame = new RandomNumber();
		frame.setSize(200,150);
		frame.createGUI();
		frame.show();
	}

	private void createGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container window = getContentPane();
		window.setLayout(new FlowLayout());

		button = new JButton("Generate");
		window.add(button);
		button.addActionListener(this);

		valuesTextField = new JTextField(14);
		window.add(valuesTextField);

		meanTextField = new JTextField(14);
		window.add(meanTextField);

		random = new Random();
	}

	public void actionPerformed(ActionEvent event) {
		int r;
		count+=1;
		r = 200 + random.nextInt(200); 
		total+=r;
		valuesTextField.setText("Number: " + Integer.toString(r));  
		meanTextField.setText("Mean: " + Double.toString((double)total/count));  
	}
}
You just have to add variables that will keep track of the total and count and add a text box to display the mean.
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote