Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Method to return an average

Greenhorn
Posts: 3
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: 59092
180
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

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: 12624
50
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: 59092
180
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: 59092
180

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: 59092
180
How often do you ever get “interaction”? I can see an “out‑by‑one” error.

 roses are red, violets are blue. Some poems rhyme and some are a tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss