I was creating this code and I hit a brick wall. Maybe because I have been doing this code for 5 hours and I am burnt out. I just need help to get it working please. Here is the code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class RandomNumbers4 extends JApplet implements ActionListener
{
  FlowLayout flow = new FlowLayout();
  Container con = getContentPane();
  JButton cButton = new JButton ("Compute");
   Font font = new Font("Times Roman",Font.BOLD, 12);
  JLabel ranVal = new JLabel("Random Values: ");
  JLabel lowVal = new JLabel ("Low Value: ");
  JLabel hiVal = new JLabel ("High Value: ");
  JLabel avVal = new JLabel ("Average Value: ");
  int Random[] = new int [5];
  int value = 0;
  int max =0, min = 0;
  int sum = 0;
  public void init()
  {
    con.setLayout (flow);
    con.add(cButton);
    con.add(ranVal);
    con.add(lowVal);
    con.add(hiVal);
    con.add(avVal);
    ranVal.setFont(font);
    lowVal.setFont(font);
    hiVal.setFont(font);
    avVal.setFont(font);
    cButton.addActionListener(this);
  }
  public static  int randomNumber (int  value)
{ 
 int choose=0;
 choose = 60 + (int)(Math.random()*40);
 return choose;
}
//maximum number generator
public static int findMaximum(int maximum)
{
  int max = 0;
  if (maximum > max)
  max = maximum;
  return max;
}

//average generator

public static int findAverage (int sum)
{
  int avg = 0;
  avg = sum/5;
  return avg;
}

public static int findMinimum(int minimum)
{
  
  int min = 0;
  if (minimum < min)
  min = minimum;
  return min;

  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source == cButton)
      ranVal + choose; lowVal + min; hiVal + max; 
  avVal + average;
  }
}

Recommended Answers

All 11 Replies

need help to get it working

What is the program supposed to do? What does it do that you want changed?
My crystal ball is broken again.

Hi
At first glance there is some syntactical errors here:

public static int findMinimum(int minimum)
{
  
  int min = 0;
  if (minimum < min)
  min = minimum;
  return min;

  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source == cButton)
      ranVal + choose; lowVal + min; hiVal + max; 
  avVal + average;
  }
}

1-You are implementing the actionPerformed inside findMinimum function: this is not allowed.
2- and those:

ranVal + choose; lowVal + min; hiVal + max;

are not statements.
Please avoid those errors first and then try to post the semantic of your code.

Hope it helps.

It does help, but this is where I am stuck at. Trying to get the action perform to work the program to run random nums

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class RandomNumbers4 extends JApplet implements ActionListener
{
  FlowLayout flow = new FlowLayout();
  Container con = getContentPane();
  JButton cButton = new JButton ("Compute");
   Font font = new Font("Times Roman",Font.BOLD, 12);
  JLabel ranVal = new JLabel("Random Values: ");
  JLabel lowVal = new JLabel ("Low Value: ");
  JLabel hiVal = new JLabel ("High Value: ");
  JLabel avVal = new JLabel ("Average Value: ");
  int Random[] = new int [5];
  int value = 0;
  int max =0, min = 0;
  int sum = 0;
  public void init()
  {
    con.setLayout (flow);
    con.add(cButton);
    con.add(ranVal);
    con.add(lowVal);
    con.add(hiVal);
    con.add(avVal);
    ranVal.setFont(font);
    lowVal.setFont(font);
    hiVal.setFont(font);
    avVal.setFont(font);
    cButton.addActionListener(this);
  }
  public static  int randomNumber (int  value)
{ 
 int choose=0;
 choose = 60 + (int)(Math.random()*40);
 return choose;
}
//maximum number generator
public static int findMaximum(int maximum)
{
  int max = 0;
  if (maximum > max)
  max = maximum;
  return max;
}

//average generator

public static int findAverage (int sum)
{
  int avg = 0;
  avg = sum/5;
  return avg;
}

public static int findMinimum(int minimum)
{
  
  int min = 0;
  if (minimum < min)
  min = minimum;
  return min;
}
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source == cButton)   
    Random = new int[5];
    int count = 0;
    double average = 0;
    int Max = 0;
    int Min = 100;
    int sum = 0;
    int number = 0;
    for (count = 0; count < 5; count++)
      number = 60 + (int)(Math.random() * 40);
    Random [count] = number;
    sum = sum + number;
    average = sum/5;
  }
}

it compiles, but I just blanked out. (maybe it is because I am working on 2 programs at the same time) I need to place the output next to the label ex: intAverage() + avVal. yet I am getting a error. without it they compile. I am just burned out.

I am just burned out.

take some time to get your head back together.
Can you describe why the output of the program is wrong and what you'd like to change it to?

Hi,
Can you please put the brackets in two places in the actionPerformed this will be helpful to understand the behaviour of the actionPerformed function:
1- Here

if (source == cButton)  [B]{[/B]
//put here what you want to do if it is so!
}

2-And here

for (count = 0; count < 5; count++)[B]{[/B]
//put here the core of iteration
[B]}[/B]

Hope it helps.

Your code here doesn't work the way you want because your count is 5 after it gets out of the loop. As a result, the Random[5] is out of bound.

if (source == cButton)
Random = new int[5];
int count = 0;
double average = 0;
int Max = 0;
int Min = 100;
int sum = 0;
int number = 0;
for (count = 0; count < 5; count++)
  number = 60 + (int)(Math.random() * 40);
Random[count] = number;
sum = sum + number;
average = sum/5;

You may need to do this.

if (source == cButton) {
  Random = new int[5];  // do not need this
  int count = 0;
  double average = 0;
  int Max = 0;
  int Min = 100;
  int sum = 0;
  int number = 0;
  for (count = 0; count < 5; count++) {
    number = 60 + (int)(Math.random() * 40);
    Random[count] = number;
    sum = sum + number;
  }
  average = sum/5;
}

By the way, there is no output written after you are done with it??? Also, please comment your code because it is a good practice whenever you write any code.

Thanks for all of your help. It compiles so now I am trying to figure out the put put. I have tried.

findMinium + lowVal.getText

but that isn't working correctly. I will get it eventually. I just finished my C# final, half way with my Java final, and just started on my python final so it is that overwhelming feeling that just won't let go. At lease after this whirlwind I will have a two week break before the next semester. Again thank you for all of your help so far.

The getText() will return String if I remember correctly. You need to parse it before you use it as integer.

Yea I just tried

minimum = Integer.parseInt(lowVal.getText());

and

findMinimum = Integer.parseInt(lowVal.getText());

and the error I am getting is cannot find symbol
symbol: variable minimum
location: class RandomNumbers4

cannot find symbol
symbol: variable minimum

That means the compiler can not find a definition for the variable: minimum.
Where have you defined that variable? The compiler needs it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.