Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print array average and highest

Tags:

java

arrays

I am trying to call getAverage and getHighest to my driver class and have them printed on the screen. However, I keep getting garbage values. Any idea what is wrong with this program? Thank you

public class ArrayOperations
{
    public double getAverage(int[] array)
    {
        double total = 0;
        double average;

        for (int index = 0; index < array.length; index++)
            total += array[index];

        average = total / array.length;
        System.out.println("The average is: " + average);
        return average;
    }

    public int getHighest(int[] array)
    {
        String output = new String("");
        int highest = array[0];
        for(int i = 1; i < array.length; i++)
        {
            if (array[i] > highest)
                highest = array[i];
            System.out.println("The highest score=" + highest);
        }
        return highest;
    }
}

Driver class:

public class ArrayOperationDriver
{
    public static void main(String[] args)
    {
        int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
        ArrayOperations object = new ArrayOperations();
        System.out.println(object);
    }
}
like image 520
VNrutgib Avatar asked Mar 19 '26 12:03

VNrutgib


1 Answers

You dint call the methods anywhere.Just do

System.out.println(object.getAverage(testScore));
System.out.println(object.getHighest(testScore));

with your code you are just printing the object which gives you the string representation of that object.

like image 56
singhakash Avatar answered Mar 22 '26 01:03

singhakash