Results 1 to 5 of 5
 09132008, 11:13 PM #1Member
 Join Date
 Sep 2008
 Posts
 16
 Rep Power
 0
trying to add up random numbers into one number
i have to write a program that is supposed to generate a random number for each student as a participation grade. so for 3 members each member needs to get a random number each time, i then need to add those numbers together to get a final group participation. Right now i can generate random numbers for each member, but i'm having trouble adding them all togehter
here is my code:
Java Code:class group { String groupName; student m1; student m2; student m3; student m4; group(String informedGroupName, student st1 , student st2 , student st3 , student st4) { groupName=informedGroupName; m1=st1; m2=st2; m3=st3; m4=st4; } String getList(){ return "Members= "+m1.firstName+ " " +m1.lastName+ ", " + m2.firstName + " " + m2.lastName + ", " + m3.firstName + " " +m3.lastName+", "+m4.firstName+" "+m4.lastName; } String participate1() { double r = 0; int n=0; String b = "......"; r = (Math.random()*10); n = (int)r; System.out.println( " r="+r+",n="+n); if (n == 0) b = "1"; if (n == 1) b = "2"; if (n == 2) b = "3"; if (n == 3) b = "4"; if (n == 4) b = "5"; if (n == 5) b = "6"; if (n == 6) b = "7"; if (n == 7) b = "8"; if (n == 8) b = "9"; if (n == 9) b = "10"; return b; } String participate2() { double r = 0; int s=0; String j = "......"; r = (Math.random()*10); s = (int)r; System.out.println( " r="+r+",s="+s); if (s == 0) j = "1"; if (s == 1) j = "2"; if (s == 2) j = "3"; if (s == 3) j = "4"; if (s == 4) j = "5"; if (s == 5) j = "6"; if (s == 6) j = "7"; if (s == 7) j = "8"; if (s == 8) j = "9"; if (s == 9) j = "10"; return j; } String participate3() { double r = 0; int m=0; String d = "......"; r = (Math.random()*10); m = (int)r; System.out.println( " r="+r+",m="+m); if (m == 0) d = "1"; if (m == 1) d = "2"; if (m == 2) d = "3"; if (m == 3) d = "4"; if (m == 4) d = "5"; if (m == 5) d = "6"; if (m == 6) d = "7"; if (m == 7) d = "8"; if (m == 8) d = "9"; if (m == 9) d = "10"; return d; } }
 09132008, 11:39 PM #2i'm having trouble adding them all togehter
Single letter variable names except for loop indexes are NOT a good technique. Without any comments it takes too long to figure out what your code is trying to do.
Use a longer variable names that describes what the variable is used for.
I see several methods in your code. What are they supposed to do?
Add the comments to the code, don't post them here outside the code.

There's a lot of redundancy in that code including 3 methods that are exactly alike, and should be compressed into one method.
1) Look at using the Random class to generate your random numbers.
2) Again there is a tremendous amount of redundancy here including 3 methods that are exactly alike and could all be made into one method, and a long 20 line if statement that can be condensed into one line of code using the String.valueOf(...) method.
3) How are you storing the grade for each student? why not an int? why not then add the ints?
4) Where's your main method? Student class?
 09152008, 12:18 AM #4Member
 Join Date
 Sep 2008
 Posts
 16
 Rep Power
 0
yea i thought this way was pretty stupid.. my professor told our class to copy this number generator and start from there. but how else can i call on it to creat a random number and not have it be a string and return a certain value?
for instance how to i just have it generate a random number then return it.
b/c if i could do that, i would run the program 3 times generate a different number each time, then add them together to produce a number. i'm just new to java and dont know how to do that
 09152008, 02:20 PM #5
Similar Threads

How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 05:50 PM 
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06142008, 02:04 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 10:44 PM 
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12222007, 02:56 AM 
random numbers without using java funtions
By carlos123 in forum New To JavaReplies: 8Last Post: 11162007, 10:13 AM
Bookmarks