-5

This is my code for the class where I declared all the methods that will be called in the main class. I am having difficulties writing the getAverageValue code since I do not know how.

import java.util.Random;

public class MyCalculator
{
    private int min;
    private int max;
    private int sum;
    private int average;

    public int[] generatedRandomData()
    {
        int  inputData[]  =  new int[10];
        Random  generatedRandom  =  new Random();

        for(int i=0; i<10; i++)
        {
            inputData[i]  =  generatedRandom.nextInt(100);
        }
        return  inputData;
    }

    public int getMaxValue (int[] inputData)
    {
        int max;
        max = inputData[0];

        for (int i = 1; i <inputData.length; i++) 
        {
            if(max < inputData[i])
                max = inputData[i];
        }

        return max;
    }

    public int getMinValue (int[] inputData)
    {
        int min;
        min =inputData[0];

        for (int i = 1; i <inputData.length; i++) 
        {
            if(min > inputData[i])
                min = inputData[i];
        }

        return min;
    }

    public int getSumValue (int[] inputData)
    {
        int sum;
        sum =inputData[0];

        for (int i = 1; i <inputData.length; i++) 
        {
            sum+=inputData[i];
        }

        return sum;
    }

    public float getAverageValue (int[] inputData, int getSumValue)
    {
        int average= 0;

        for (int i = 1; i <inputData.length; i++) 
        {
            average = getSumValue/inputData.length;
        }

        return average;

    }

}

While this is my code for my main class:

public class MyApp 
{
    public static void main (String[] args)
    {
        MyCalculator calculator = new MyCalculator();
        int [] inputData = calculator.generatedRandomData();
        System.out.println("The generated values are: ");
        for (int value: inputData)
            System.out.print(value + ", ");
        System.out.println();
        System.out.println();

        System.out.println("Max Value: " + calculator.getMaxValue(inputData));
        System.out.println("Min Value: " + calculator.getMinValue(inputData));
        System.out.println("Sum Value: " + calculator.getSumValue(inputData));
        System.out.println("Avg Value: " + calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length));

    }
}

When compiling this code, I get the following error:

incompatible types: int cannot be converted to int[]
                System.out.println("Avg Value: " + calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length));
                                                                                                    ^

Note: Some messages have been simplified;
Marvin
  • 13,325
  • 3
  • 51
  • 57
mastaxx
  • 29
  • 5

3 Answers3

1

This is your original statement:

System.out.println("Avg Value: " + calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length));

If we split that apart to only do one thing at a time, we get:

sum = calculator.getSumValue(inputData);
len = inputData.length;
average = calculator.getAverageValue(sum, len);
text = "Avg Value: " + average;
System.out.println(text);

Both accomplish the same thing, but split apart, it is just so very verbose.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

The signature of getAverageValue is

public float getAverageValue (int[] inputData, int getSumValue)

i.e. the method expects the input data as int[] array and then the value of the sum.

You are calling it as

calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length)

i.e. with the value of the sum (which is an int - the "incompatible type" your compiler is complaining about) and the number of inputs.

Correct would be:

calculator.getAverageValue(inputData, calculator.getSumValue(inputData))

As a side note: Your implementation probably does not what you think it does. See Integer division: How do you produce a double? and think about whether you really need a loop there.

Marvin
  • 13,325
  • 3
  • 51
  • 57
0

The correct code:

System.out.println("Avg Value: " + 
    calculator.getAverageValue(inputData, calculator.getSumValue(inputData)));
Leone
  • 3,258
  • 3
  • 22
  • 28