Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-14-2008, 01:13 AM
Member
 
Join Date: Sep 2008
Posts: 15
pjr5043 is on a distinguished road
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:
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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-14-2008, 01:39 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
i'm having trouble adding them all togehter
Where in your code is the problem?
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-14-2008, 01:40 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 883
Fubarable is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-15-2008, 02:18 AM
Member
 
Join Date: Sep 2008
Posts: 15
pjr5043 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-15-2008, 04:20 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
how to i just have it generate a random number then return it.
You can use the Random nextInt() method to get a random number.
What are the requirements for the random number? For example what range of values or what type of variable: int, float
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I generate random numbers in a certain range using the random class? frasifrasi New To Java 7 06-22-2008 07:07 AM
Random numbers jithan Advanced Java 3 06-14-2008 04:04 PM
random numbers without random class` carlos123 New To Java 4 01-18-2008 12:44 AM
random numbers carlos123 New To Java 1 12-22-2007 04:56 AM
random numbers without using java funtions carlos123 New To Java 8 11-16-2007 12:13 PM


All times are GMT +3. The time now is 11:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org