Results 1 to 5 of 5
Thread: Why do I get 0 in the outputs?
- 10-30-2012, 03:44 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
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.
Java 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; } }Java 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()); } }
- 10-30-2012, 03:55 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
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.Last edited by JBelg; 10-30-2012 at 03:57 PM.
- 10-30-2012, 04:02 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
- 10-30-2012, 04:12 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Why do I get 0 in the outputs?
Java Code://constructor public DataSet() {} //method public void DataSet() {}Java 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
- 10-30-2012, 04:13 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Similar Threads
-
same code different outputs
By nikhil_me in forum New To JavaReplies: 4Last Post: 01-20-2012, 07:35 AM -
Same inputs, different outputs.
By werner291 in forum New To JavaReplies: 4Last Post: 12-15-2011, 07:36 PM -
I want to put text outputs in the GUI console.
By illinit in forum New To JavaReplies: 2Last Post: 10-19-2011, 11:21 PM -
Putting multiple outputs into one JOptionPane Dialog Box?
By pwe9109 in forum New To JavaReplies: 8Last Post: 09-12-2011, 01:43 PM -
logging class outputs two text files when i only want one!!
By scr106 in forum AWT / SwingReplies: 0Last Post: 07-22-2011, 12:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks