RVER12321312
RVER12321312

Reputation: 81

Java constructors and returns

I'm having trouble finishing my assignment; the question asks me to:

  1. Write a program that computes and prints the mean and standard deviation of a list of integers x1 through xn
  2. Assume that there will be no more than 50 input values and the maximal possible value is 100.
  3. Create a driver class, which randomly generates values that are less than or equal to the maximal value 100.

I've written code for the first part which calculates the mean and standard deviation:

    public class Statistics
    {


    public float getMean(int[] numbers){

        int count = 0;
        float output = 0;
           for (int i=0; i<numbers.length; i++)
           { 
               count = count + numbers[i];
           }
           output = ((float)count/numbers.length);
           return output;
    }


    public float standardDeviation(int[] numbers, int count)

    {

                    float mean = getMean(numbers);
                    float output2 = 0;

                    float totalSdvMean = 0.0f;

                    for (int i=0 ; i<numbers.length; i++)
                    { 

                        float dev1 = numbers[i] - totalSdvMean;
                        dev1 = dev1 * dev1;
                        totalSdvMean += dev1;
                    }
                      output2 =(float)Math.sqrt(totalSdvMean/numbers.length);
                      return output2;
        }

    }

And secondly this is currently my driver class:

    import java.util.Random;

    public class StatisticsDriver
    {
    private static final int MAX_COUNT = 50, MAX_VALUE = 100;


    public static void main(String args[]){

          int[] numbers = new int[MAX_COUNT];

          Random generator = new Random();

          for (int i = 0; i < MAX_COUNT; i++)
          {
              numbers[i] = generator.nextInt(MAX_VALUE);
          }

          getMean mean = new getMean(numbers);
          standardDeviation sdv = new standardDeviation(numbers);

          float mean = getMean(numbers);
          System.out.println(mean);

         float sdv = standardDeviation(numbers);
        System.out.println(sdv);
       }
    }

I can't seem to get a return value and I'm completely stumped. I think there is an error in my constructor but I don't know what it is. Any help would be appreciated.

Thanks!

Upvotes: 1

Views: 1403

Answers (1)

Eran
Eran

Reputation: 393771

getMean mean = new getMean(numbers);

getMean is a method, not a class, so you have to create an instance of the class it belongs to in order to call that method :

Statistics stat = new Statistics ();
float mean = stat.getMean(numbers);

The same goes for standardDeviation :

float sdv = stat.standardDeviation(numbers);

Upvotes: 2

Related Questions