• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Getting lines added up and averaged

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic