VNrutgib
VNrutgib

Reputation: 45

Print array average and highest

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);
    }
}

Upvotes: 3

Views: 129

Answers (3)

Frakcool
Frakcool

Reputation: 11153

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("");
    }
}

Upvotes: 0

Mureinik
Mureinik

Reputation: 311808

You aren't calling getAverage or getHighest. You are just printing the ArrayOperations, which in fact calls ArrayOperations.toString(). Since you didn't override it, you get the default implementation, which prints the class name and its default hashCode() implementation.

The best practice would be to remove the printing from the ArrayOperations' methods and just return the result. Printing should be handled by the caller (driver) class. This way, if the callers wants to do something else with the result (e.g., display it in a webpage, save it to a database, perform another calculation), it could.

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
            ("The average score is: " + object.getAverage(testScores));
        System.out.println
            ("The highest score is: " + object.getHighest(testScores));
    }
}

Upvotes: 0

singhakash
singhakash

Reputation: 7919

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.

Upvotes: 1

Related Questions