Results 1 to 10 of 10
 10012011, 02:16 AM #1Member
 Join Date
 Sep 2011
 Posts
 4
 Rep Power
 0
help with math class and random numbers
Java's Math class contains a static method called random.
The RootTwo class below computes an approximation to the the square root of 2. However, there's a missing statement in the code.
In the answer box provided, assign an appropriate random value to the variable val so that the code will calculate the intended approximation.
Hint: the algorithm works this way: the square root of 2 falls between 1 and 2. If you choose 1000 random nonintegral values between 1 and 2, the fraction of these values that fall below the square root of 2 gives a reasonable estimate for the decimal part of that square root. (For example, if 951 of the 1000 fall below the square root of 2, then our estimate would be 1.951). It's easy to check if a number falls below the square root of 2: just multiply it by itself (square it), and compare the result with 2.0.
Java Code:import java.util.Scanner; public class RootTwo { public static void main(String[] args) { System.out.println("Enter the number of iterations:"); Scanner scan = new Scanner(System.in); int ct = 0; int n = scan.nextInt(); double val; for(int j = 0; j < n; j++){ return (1 + (int)(Math.random()*2)); ^ cannot return a value from method whose result type is void if (val * val < 2) ct++; } System.out.println("With " + n + " iterations, my approximation to "); System.out.println("the square root of 2 is " + (1 + (double)ct/n)); } }
Last edited by pbrockway2; 10012011 at 02:20 AM. Reason: code tags added
 10012011, 02:18 AM #2Member
 Join Date
 Sep 2011
 Posts
 4
 Rep Power
 0
Re: help with math class and random numbers
i filled in the "return (1 + (int)(Math.random()*2));" as a guess. idk what im supposed to put in that line to make it work :/ and the computer made the cannot return value mark. im supposed to put something there to appease it
 10012011, 02:26 AM #3Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,615
 Rep Power
 12
Re: help with math class and random numbers
When posting code, please use the code tags. You put [code] at the start of the code and [/code] at the end. This means the code will be formatted correctly. There is a "#" button in the message composition pane that will add the tags around selected text.

main()  as you have declared it  is a void method. That is, the method does not return a value. Hence the compiler tells you that there is a problem when you say "return (1+ etc".
What did you intend that line to do?
 10012011, 02:31 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,615
 Rep Power
 12
Re: help with math class and random numbers
Our posts crossed!
OK, guessing won't work. Well, it might on occasion, but, in general, computer code is not created by guesswork.
So, do you understand the process that the question outlined for finding the square root of 2? In particular do you know what the role of the variables n, ct and val are?
val, in particular, is interesting. Not only what its value supposed to be, but also it should be declared and given that value somewhere.
 10012011, 02:31 AM #5Member
 Join Date
 Sep 2011
 Posts
 4
 Rep Power
 0
Re: help with math class and random numbers
this is just a homework question im not sure how to do and im a huge novice. on the website it gives the directions above the code and the box theyre referring to is where i put my line the computer said wasn't right. i need to put something to make random numbers between 1 and 2 i think to approximate the square root of 2
 10012011, 02:38 AM #6Member
 Join Date
 Sep 2011
 Posts
 4
 Rep Power
 0
Re: help with math class and random numbers
i undertand what n, ct, and val are. i just dont know how to represent all random numbers between 1 and 2 to approximate the square root of 2
 10012011, 02:47 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,615
 Rep Power
 12
Re: help with math class and random numbers
i undertand what n, ct, and val are
 10012011, 09:31 PM #8Member
 Join Date
 Sep 2011
 Posts
 14
 Rep Power
 0
Re: help with math class and random numbers
Return can only be used with non void methods, so either you rewrite your code so it can print that or you make a method (not a void) outside your main method and you will return your value to the main method.
I had an assigment that looked like this, only without math class and the number of iterations wasn't important.
 10012011, 09:42 PM #9Member
 Join Date
 Aug 2011
 Posts
 16
 Rep Power
 0
Re: help with math class and random numbers
Hey I'm not that good, but these might solve some of your problems!
Make your RootTwo look like this, it's a method which means you can call using a main method but it cannot be run by itself:
import java.util.Scanner;
public class RootTwo {
public int RootOfTwo(int iterations) { //The int here states that you'll be returning an integer to the caller
int ct = 0;
double val;
for(int j = 0; j < n; j++){
return (1 + (int)(Math.random()*2));
if (val * val < 2)
ct++;
}
}
}
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Enter the number of iterations:");
System.out.println("With " + n + " iterations, my approximation to ");
System.out.println("the square root of 2 is " + (1 + (double)ct/n));
RootTwo(n);
 10012011, 09:57 PM #10Member
 Join Date
 Sep 2011
 Posts
 14
 Rep Power
 0
Similar Threads

Math.random not zero
By rsvr in forum New To JavaReplies: 3Last Post: 02282010, 02:57 AM 
Math.random
By p0rnstar in forum New To JavaReplies: 9Last Post: 01272010, 02:26 AM 
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09142009, 09:28 AM 
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 without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM
Bookmarks