This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting lines added up and averaged Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting lines added up and averaged" Watch "Getting lines added up and averaged" New topic
Author

Getting lines added up and averaged

mike statham
Greenhorn

Joined: Feb 24, 2012
Posts: 16


Whic prints out this:
Students with grades 69 and below:
Connie 27
95 26
James 32
99 26
Susan 5
67 25
Jake 24
65 20
Phil 29
66 32

Students with grades between 70 and 79:

But should print this:

Connie has an average grade of 85%. You will receive a B in this class.
James has an average grade of 92%. You will receive a A in this class.
Susan has an average grade of 52%. You will receive a F in this class.
Jake has an average grade of 66%. You will receive a D in this class.
Karen has an average grade of 77%. You will receive a C in this class.

Overall Class Average is 74%.

The following program generates 3 random numbers, then averages them.

The average of 12 34 24 is 23
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

Was there a question in here somewhere?

mike statham wrote:

Whic prints out this:
Students with grades 69 and below:
Connie 27
95 26
James 32
99 26
Susan 5
67 25
Jake 24
65 20
Phil 29
66 32

Students with grades between 70 and 79:

But should print this:

Connie has an average grade of 85%. You will receive a B in this class.
James has an average grade of 92%. You will receive a A in this class.
Susan has an average grade of 52%. You will receive a F in this class.
Jake has an average grade of 66%. You will receive a D in this class.
Karen has an average grade of 77%. You will receive a C in this class.

Overall Class Average is 74%.

The following program generates 3 random numbers, then averages them.

The average of 12 34 24 is 23


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You have written procedural code using an object-oriented language. Your main method is entirely -- I might say, single-mindedly -- focused on producing Strings to print out. How could you expect to produce the required output this way?

You need to break up your task into smaller bits. What will you do when you are asked for more information about the grades? Say we desire the mode or median scores? You'll have to add more code to the main method. As requirements are added the main method will quickly become impossible to manage.

Use objects. Maybe a Student object that contains a set of scores? Then make a method that can calculate the average of a bunch of scores, given a Student object. Something like that. One simple object can simplify your task a great deal. Don't write procedural code!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting lines added up and averaged