Option 1:
The "garbage" values you're getting are the memory locations where object is located. Instead call the methods:
System.out.println(object.getAverage(testScores));
System.out.println(object.getHighest(testScores));
And I'll add a modification to your code so it won't print lots of "The highest value is:"
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];
//Removed from here
//System.out.println("The highest score=" + highest);
}
//Moved to here
System.out.println("The highest score=" + highest);
return highest;
}
Option 2:
But as you are already printing inside the methods, I'd change them to void and take off return statements. As follows:
public class ArrayOperations {
public void 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);
}
public void 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);
}
}
And call methods this way:
public class ArrayOperationDriver {
public static void main(String[] args) {
int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
ArrayOperations object = new ArrayOperations();
object.getAverage(testScores);
object.getHighest(testScores);
}
}
I guess it's a cleaner way to do it.
Option 3:
And one more option to do it would be, returning numbers from your methods but removing S.o.p calls from inside them.
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;
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];
}
return highest;
}
}
And putting them inside main method:
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("Average number is: " + object.getAverage(testScores));
System.out.println("Highest number is: " + object.getHighest(testScores));
}
}
Edit
To print all numbers in array testScores you can do it in a simple for loop or a for-each loop, I used the for-each one, but you can try it with the for if you want to know how.
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("Average number is: " + object.getAverage(testScores));
System.out.println("Highest number is: " + object.getHighest(testScores));
System.out.print("The test scores are: ");
for (int score : testScores)
System.out.print(score + " ");
System.out.println("");
}
}
System.out.printlnwith an input object for which you have not overriddentoString(). You also have not called either have the methods you defined onArrayOperationstoString()method of your object (which is basically thetoString()method ofObjectclass ) to see why..