This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes The Art & Science of Java Chapter 4 Exercise 5 AverageList.java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Art & Science of Java Chapter 4 Exercise 5 AverageList.java" Watch "The Art & Science of Java Chapter 4 Exercise 5 AverageList.java" New topic
Author

The Art & Science of Java Chapter 4 Exercise 5 AverageList.java

M Ford
Greenhorn

Joined: Mar 06, 2012
Posts: 10
Hi guys.

Im trying to figure out The AverageList exercise and I think I've got it but just missing that last final piece in the puzzle. I cant think of what to do from here..






I know the int average = total/value; is wrong but i just cant figure out how to get the average. I can add the exam scores together.....

Could anybody help me out with this one? Any input is much appreciated.


[HENRY: Added code tags]
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

For an average, you need to keep a count of the number of exam scores entered.
M Ford
Greenhorn

Joined: Mar 06, 2012
Posts: 10
sorry but as you can gather i am a complete novice at this... can you show me the syntax for that?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
M Ford wrote:sorry but as you can gather i am a complete novice at this... can you show me the syntax for that?


You already have a loop that gathers the total of the numbers entered. The average of them is not the total divided by any of the values, but the total divided by the number of entries. In other words, if the user enters 5 values, it is the total divided by 5. So declare another integer variable outside the loop, set to 0. Each time through the loop, add one to it (count = count + 1). After the loop is done, divide the total by the number of entries to get your average. Or you can do the division after each of the entries, for the average "up to that point".
M Ford
Greenhorn

Joined: Mar 06, 2012
Posts: 10
I understand how to get the average. I've tried different ways to add how many times an integer is entered so I can divide the total by that but I just cant seem to figure it out. Tried creating another integer outside the loop but which ever way I do it, instead of adding how many times the integers are being added, it just adds the value for them instead.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479


I moved the calculation of the average to outside the loop. This doesn't make much difference, but you may as well do things within the loop that depend on being in the loop, and other things outside it. Someone might wonder why you calculate the average every time through the loop, since you don't use it there.

Since your average value is an integer, you will only have an integer value for the average, and I think integer division will truncate (throw away any fractional portion) instead of rounding. If you wanted fractional values, you could use a float for average, or you could call a rounding function on your integer calculation if you wanted to do it that way.
M Ford
Greenhorn

Joined: Mar 06, 2012
Posts: 10
Thank you very much for your help.. finally got it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: The Art & Science of Java Chapter 4 Exercise 5 AverageList.java