Results 1 to 5 of 5
  1. #1
    pjr5043 is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default 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;
    	}
    		
    
    
    
      
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  4. #4
    pjr5043 is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  3. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  4. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM
  5. random numbers without using java funtions
    By carlos123 in forum New To Java
    Replies: 8
    Last Post: 11-16-2007, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •