Why do I get 0 in the outputs?
I am trying to generate 100 random numbers between 1 and 1000. Then, I need to find their average and which of them is their maximum. I get a wrong output. How do I fix this?
Thank you very much.
Code:
import java.util.Random;
public class DataSet
{
private Random randomGenerator;
private int randomInt;
private int total;
private double average;
private int highestValue = 1;
public void Dataset()
{
total = 0;
for (int i = 1; i <= 100; i++)
{
randomInt = randomGenerator.nextInt(1000);
total = total + randomInt;
if(highestValue < randomInt)
{
highestValue = randomInt;
}
}
}
public double getAverage()
{
average = total/100;
return average;
}
public int getMaximim()
{
return highestValue;
}
public int getRandomInt()
{
return randomInt;
}
}
Code:
public class DataSetTester
{
public static void main(String[] args)
{
DataSet jean = new DataSet();
System.out.println("The average of the generated numbers is: " + jean.getAverage());
System.out.println("The highest number of the generated random numbers is: " + jean.getMaximim());
}
}
Re: Why do I get 0 in the outputs?
Nothing seems to be wrong.
Could you please tell us what's wrong with the output?
EDIT:
After a closer read I've discovered the problem.
Hint: You do not have a constructor.
Re: Why do I get 0 in the outputs?
Quote:
Originally Posted by
JBelg
Nothing seems to be wrong.
Could you please tell us what's wrong with the output?
EDIT:
After a closer read I've discovered the problem.
Hint: You do not have a constructor.
You mean so that I can input a number? I don't need to input a number. All I need is for the program to randomly generate the numbers when I run it. Or am I wrong?
Re: Why do I get 0 in the outputs?
Code:
//constructor
public DataSet() {}
//method
public void DataSet() {}
Code:
DataSet jean = new DataSet(); // new DataSet() creates a new object of your class and calls the constructor
jean.DataSet(); //calls the method inside the class DataSet
Re: Why do I get 0 in the outputs?