Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Getting lines added up and averaged

mike statham
Greenhorn
Posts: 23

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
Posts: 12196
35
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

dennis deems
Ranch Hand
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!