3

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);
    }
}
3
  • You are getting "garbage" values because you are calling System.out.println with an input object for which you have not overridden toString(). You also have not called either have the methods you defined on ArrayOperations Commented Sep 21, 2015 at 16:52
  • @tommy You can check the Javadoc of toString() method of your object (which is basically the toString() method of Object class ) to see why.. Commented Sep 21, 2015 at 16:56
  • These answers are all right; however, I would probably make these methods static since they only depend on the input parameter and do not modify any sort of state (these would be called "pure functions"). Commented Sep 21, 2015 at 17:15

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Okay, thank you. I am now able to print the average and highest exactly as I was trying to . Do you have any idea how I would print the testScores in the driver class?
I am trying to print it using System.out.println(The test scores are: " + testScores); but this is not working for me
Thank you Frakcool, you have been a great help. I hope to return the favor when my java skills improve.
If you try to print them as System.out.println("The test scores are: " + testScores);, again you will get the direction where the array is located, so, that's why you need to loop through them. And don't worry, I'm glad to help. Good Luck and keep learning! C:
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.