Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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 < 10; i++){
                    System.out.println("Enter a number to average.");
                    array[i] = input.nextDouble();
                }
                System.out.println("The average of your arrays numbers is: " + average(array));

        } else if (entry == 1) {                           //choose int and call int method
            int[] array = new int[10];
                for(int b = 0; b < 10; b++) {
                    System.out.println("Enter a number to average.");
                    array[b] = input.nextInt();
                }
                System.out.println("The average of your arrays numbers is: " + average(array));
        }

    }

    public static int average(int[] array){
        int total =0;
        int avg1 = 0;
            for(int b=0; b < 10; b++) {
                total = total + array[b];
                avg1 = total / 10;
            }
        return avg1;

    }

    public static double average(double[] array) {
        double total = 0;
        double avg = 0;
            for(int i =0; i < 10; i++) {
                total =  total + array[i];
                avg = total / array.length;
            }
        return avg;

    }

}
share|improve this question

migrated from stackoverflow.com Mar 30 '14 at 0:37

This question came from our site for professional and enthusiast programmers.

    
Have you tried running this code? Looks like it would always return a zero mean, unless I'm missing something –  Graham Savage Mar 29 '14 at 6:39
    
Post rolled back. Please don't modify the original code according to answers. That will invalidate them. Instead, you may post the new code below the original, or as a follow-up question if you'd like review of the new code. –  Jamal Mar 30 '14 at 3:26
    
Please post a follow-up question instead. –  200_success Mar 30 '14 at 3:31

2 Answers 2

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.

share|improve this answer
    
Thanks. I've been working on a revised version and I'd like to post that and reply to some of your comments. Sadly, I'm still having trouble with the syntax. –  Pivo Mar 30 '14 at 2:24
    
@Pivo If the syntax you are having trouble with is calling a method with an array as a parameter, I tried google.com/search?q=java+call+method+array+parameter as a Google query and it found mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/… which you might find useful. –  ChrisW Mar 30 '14 at 2:56

You wrote the average method but you're not calling it.

The average method need not be overloaded because the problem doesn't ask for a version of it which works with int arrays (so you can delete the version which works with int arrays).

The average method is setting double total = 0; but is then not summing the elements in the array (you're currently doing that, calculating the total, in your second for loop inside main: instead that for loop should be moved into the body of your average method, and your main should call that average method instead of running that for loop).

In main the array is named average: it should probably be renamed to input or something like that.

share|improve this answer
    
Ew, that's a great point. How would I make use of the int average method? As this is the first overloaded method I'm trying to write, I'm a bit confused on that. –  Pivo Mar 30 '14 at 1:03
    
To invoke the int average method you would need to pass an array of type int[] as a parameter. Currently you only create one array in main, and that array is of type double[]. –  ChrisW Mar 30 '14 at 1:06
    
Thanks, I'll repost once I've taken into account your help. –  Pivo Mar 30 '14 at 1:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.