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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With