Results 1 to 10 of 10
  1. #1
    NestorPrime is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default 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 non-integral 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; 10-01-2011 at 03:20 AM. Reason: code tags added

  2. #2
    NestorPrime is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default 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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  5. #5
    NestorPrime is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default 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

  6. #6
    NestorPrime is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default 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

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: help with math class and random numbers

    i undertand what n, ct, and val are
    Good. So declare val and assign it the value you understand it should have.

  8. #8
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  9. #9
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

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

    }
    }
    Make a new class with the main method and put all these inside:
    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));
    Finally a way to call the method with your main,
    RootTwo(n);

  10. #10
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: help with math class and random numbers

    Can you declare a double inside an int method?

    Moreover what you will return is an int, but the square root of 2 is not an integer.

Similar Threads

  1. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 02:57 AM
  2. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 02:26 AM
  3. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 10:28 AM
  4. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Posting Permissions

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