HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Pass an array of numbers into a method that's overloaded

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
overloadedmethodpassarrayintonumbersthat

Problem

I'm doing my homework and it says to write a test program that prompts the user to enter ten double values, invokes this method, and displays the average value.

I was able to get this program working with an overloaded method... but I'm curious to hear your feedback on where I can improve and what I'm not understanding 100%. Please feel free to review this and let me know your constructive feedback as to my code and where you think I can do better.

import java.util.Scanner;

public class PP68 {

    public static void main(String [] args) {//start main

        Scanner input = new Scanner(System.in); //new object of scanner type

        double avg =0, total =0;

        double[] average = new double[10];      //double array

        for(int i = 0; i < 10; i++){             //enter 10 numbers to average
            System.out.print("Enter a number to average.");
            average[i] = input.nextDouble();
        }

        for(int j = 0; j < average.length; j++) {
            total = total + average[j];
            avg = total / average.length;
        }
        System.out.println("The average of your arrays numbers is: " + avg);

    }//end main

    public static int average(int[] array){ //1st method
        int total =0;
        int avg = total / array.length;
        return avg;

    }//end 1st method
    public static double average(double[] array) {//Second method - overloaded
        double total = 0;
        double avg = total / array.length;
        return avg;

    }//end 2nd method

}


New code follows below:

```
import java.util.Scanner;

public class PP68v2 {

public static void main(String [] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter 1 to average an INT or hit 2 to average a DOUBLE");
int entry = input.nextInt();

if (entry == 2) { //choose double and call double method
double[] array = new double[10];
for(int i = 0; i <

Solution

The code produces the right output, which is good. It has some problems, though.

The average() functions

As @ChrisW points out, the two average() functions you defined are not being used. Since your inputs are doubles, you only need one of them: the double average(double[] array) function.

The average() functions are currently buggy:

  • total = 0, so of course they will each always return 0. Copy the summation code from main() to fix that.



  • The int version uses integer arithmetic, so division rounds the result to an integer (towards 0). In general, the average of a set of integers is not an integer. You should use double to store the total, even in the integer version. (You did it correctly in main().)



In case you're wondering, if you want to be able to handle integral and floating-point input arrays, you do need to write out both the int[] and double[] versions explicitly.

main()

If you open a Scanner, it's good etiquette to close it. The idiom for doing that since Java 7 is to use a try-with-resources block:

try (Scanner input = new Scanner(System.in)) {
    …
}


Avoid declaring and defining variables avg and total until you need them.

The average array is poorly named. It's an array of ten user input numbers, not an array of ten averages.

You hard-coded 10 twice. The first for-loop should be more like the second, which uses average.length.

The average only needs to be computed once, not ten times. Pull the division operation out of the loop.

Of all the comments in the class, the only one worth keeping is // enter 10 numbers to average. All of the others are redundant noise.

Code Snippets

try (Scanner input = new Scanner(System.in)) {
    …
}

Context

StackExchange Code Review Q#45718, answer score: 4

Revisions (0)

No revisions yet.