• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method to return an average  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a method which can be called in main that calculates the average participation of a group of students. In the student class I have a method which will generate and return a random number 1 through 10 when called upon. However I want a method in my group class that will gather and return the total participation of a group. Here is the source code. I feel like this should be so easy from here but I can't understand the logical way to do this in java. Also, if there are any baseball fans out there, don't make fun of my Pirates :P.





 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, how would you do it on a piece of paper? When you have worked that out and got it into words of one syllable, then you can code it.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Rose wrote:I am trying to make a method which can be called in main that calculates the average participation of a group of students.

OK, so what's the formula for calculating an average? I don't see any sign of it in your code.

Tip 1: Use meaningful names for your variables. Names like 'nn' really don't tell you (or us) very much, and will only confuse you when you're trying to debug.

Tip 2:
if (n == 0) b = "reading";
if (n == 1) b = "talking";
...

forces the JVM to check 'n' as many times as you have if statements, regardless of the outcome. Either add an else in front of all subsequent ones, or have a look at the switch statement (or indeed, just return the value as soon as you know it's the one you want).

Tip 3: Take Campbell's advice and StopCoding (←click). The fact is that you should never have got this far without knowing that your code works.

HIH

Winston
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just wretched:


What are you trying to do here? THINK about it - don't just write code. All of this can be replaced with one line.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To continue from what Winston said about those dreadful multiple ifs. What you appear to be doing is assigning x to n. As Winston said, give those variables meaningful names. You don't need thaose multiple ifs at all. They don't do anything at all.
If you want to assign to Strings, as Winston shows, you can put the Strings (which are objects) into an array (which is also an object), and actually do it the object‑oriented way.

I prefer to use a java.util.Random object to Math.random(). It gives a lot more flexibility and you can use it to get an int between 1 and 10 inclusive without messing around with all that casting. Don't use multiple Random objects, in case they return the same sequence of results.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . All of this can be replaced with one line.
I challenge you to replace that with an even shorter bit of code

Maybe you can't get rid of the return statement.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How often do you ever get “interaction”? I can see an “out‑by‑one” error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!