Reputation: 43
I have to have a random number generator that gets a number from the user and then generates 10000 random numbers between 1 and the users number then figures min, max, and mean. Here is what I have so far. I am stuck on the actionPerformed method. I am a total noob so please try to explain your answers.
I have the JApplet coded just can’t figure out the action performed method. I have searched Google high and low for help and the chapter covered in the book this week in appendix c and they don’t explain how to do what we are asked to do. I have completed every other assignment in the class on my own but can’t seem to get this one and have spent 19 hours on it so far.
How to get all of the numbers added to the array list?
package randomNums;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Math.*;
import javax.swing.*;
import java.applet.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
public class RandomNums extends Applet implements ActionListener {
/**
*
*/
// PAINT METHOD
public void paint(Graphics g)
{
Font font = new Font("Arial", Font.BOLD, 18);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString("Enter A Number", 70, 25);
resize(350, 350);
this.setBackground(Color.BLUE);
}
// CREATES OBJECTS
private static final long serialVersionUID = 1L;
TextField text1, text2, text3, text5;
Label label1, label2, label3, label4;
Button button;
Font font = new Font("Arial", Font.BOLD, 11);
private double all;
// INIT METHOD
public void init() {
setLayout(null);
repaint();
// YOUR NUMBER LABEL
label1 = new Label("Your Number ");
label1.setBounds(25, 35, 100, 20);
setFont(font);
add(label1);
// YOUR NUMBER ENTRY
text1 = new TextField(5);
text1.setBounds(150, 30, 100, 25);
add(text1);
// MAXIMUM
label2 = new Label("The Maximum Number Is: ");
label2.setBounds(25, 100, 150, 25);
setFont(font);
add(label2);
// MAXIMUM ANSWER
text2 = new TextField(5);
text2.setBounds(180, 100, 50, 25);
add(text2);
// MINIMUM
label3 = new Label("The Minimum Number Is: ");
label3.setBounds(25, 170, 150, 25);
setFont(font);
add(label3);
// MINIMUM ANSWER
text5 = new TextField(5);
text5.setBounds(180, 170, 50, 25);
add(text5);
// MEAN
label4 = new Label("The Mean is: ");
label4.setBounds(25, 135, 150, 25);
setFont(font);
add(label4);
// MEAN ANSWER
text3 = new TextField(5);
text3.setBounds(180, 135, 50, 25);
add(text3);
// BUTTON
button = new Button("Enter");
button.setBounds(90, 70, 100, 20);
add(button);
// ACTION LISTENER
button.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
Random ran = new Random(10000);
try {
//NOT SURE HOW TO GET ALL OF THE NUMBERS ADDED TO THE ARRAY LIST
double[] arrList = ran();
//THIS IS NOT WORKING RIGHT ONLY STORING ONE VALUE
all = ran.nextDouble();
for (int i = 0; i < arrList.length; i++) {
System.out.println(arrList[i] + " ");
//THIS IS IN THERE FOR MY TESTING PURPOSES NEED TO TAKE OUT BEFORE SUBMITTING
System.out.println(arrList);
final double TIMES = (double) 10000;
final String LIMIT = text1.getText();
Double.parseDouble(LIMIT);
//FOR LOOP
for (int x = 1; x < TIMES; ++x);
//SETS TEXT FOR MIN BOX (NOT SURE IF IT IS DOING THE CALCULATIONS RIGHT)
text5.setText(ran.nextDouble() + "");
//my comment: another variable after for loop to get mean , fix numbers being saved to an array so they can be added and divided to get mean,
}
}
catch (NumberFormatException m) {
if (getText(text1) == 0)
JOptionPane.showMessageDialog(this,
"Please enter a number between 1- 10,000");
}
}
private int getText(TextField text12) {
// TODO Auto-generated method stub
return 0;
}
}
Upvotes: 1
Views: 4554
Reputation:
Take it step-by-step
1- Design a SSCCE that performs the basic operations correctly before implementing it into the applet, here is a quick one I wrote:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class main
{
public static void main(String args[])
{
Random rand = new Random();
ArrayList<Integer> array = new ArrayList<Integer>();
int x ;
int sum = 0;
System.out.print("Input a number: ");
Scanner input = new Scanner(System.in);
int mean;
int temp = input.nextInt();
for(int i = 0;i < 10000;i++)
{
x = Math.abs(rand.nextInt()%temp)+1;
System.out.println(x);
array.add(x); //add random number to array
sum += x; //compute sum ro calculate mean later;
}
int min = array.get(0);
int max = array.get(0);
//find min
for(int i = 0; i < array.size();i++)
{
if(array.get(i) < min)
{
min = array.get(i);
}
}
//find max
for(int i = 0; i < array.size();i++)
{
if(array.get(i) > max)
{
max = array.get(i);
}
}
//find mean
mean = sum/array.size();
//result
System.out.println("Min is : "+min);
System.out.println("Max is : "+max);
System.out.println("Sum is : "+sum);
System.out.println("Mean is : "+mean);
}
}
Now that you are done of the core objective, it's time to integrate it into a JApplet. Look into http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html Start over from there and apply what you need.
Upvotes: 0
Reputation: 168825
To get that code to compile, change:
double[] arrList = ran();
To:
double[] arrList = new double[10000];
java.applet.Applet
. The Swing equivalent is javax.swing.JApplet
. The only Swing component used is a JOptionPane
. On that subject: Why AWT rather than Swing? See this answer on Swing extras over AWT for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see Mixing Heavyweight and Lightweight Components. resize(350, 350);
That is not something that should be done in a paint method, and not something that should ever be done in an applet. The size of an applet is set in the HTML.Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
1.
2.
Upvotes: 2
Reputation: 5490
First off, new Random(10000)
creates a new Random
object with a seed of 10000
, not an object with a range up to 10,000 or one that creates 10,000 numbers. See the javadocs at http://docs.oracle.com/javase/6/docs/api/java/util/Random.html. Re-read it.
double[] arrList = ran()
— this isn't going to work, since it doesn't mean anything. See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html (scroll to Creating, Initializing, and Accessing an Array for the pertinent information).
Next you'll want to use a loop to (10,000 times over) get a random number (see the javadocs for Random
again) and store it in the array.
There are other errors you'll no doubt come across (what is all
?) but this is a starting point. If you show your error messages and what you think is not working, we can be of more help.
Upvotes: 1