Results 1 to 5 of 5
  1. #1
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default 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());
    	
    		
    	}
    
    }

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default 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.

  3. #3
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default Re: Why do I get 0 in the outputs?

    Quote Originally Posted by JBelg View Post
    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?

  4. #4
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default 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

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

Similar Threads

  1. same code different outputs
    By nikhil_me in forum New To Java
    Replies: 4
    Last Post: 01-20-2012, 07:35 AM
  2. Same inputs, different outputs.
    By werner291 in forum New To Java
    Replies: 4
    Last Post: 12-15-2011, 07:36 PM
  3. I want to put text outputs in the GUI console.
    By illinit in forum New To Java
    Replies: 2
    Last Post: 10-19-2011, 11:21 PM
  4. Replies: 8
    Last Post: 09-12-2011, 01:43 PM
  5. Replies: 0
    Last Post: 07-22-2011, 12:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •