Results 1 to 5 of 5

Thread: Use a snippet ?

  1. #1
    huuduy216 is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Use a snippet ?

    For my homework, my teacher has provided us with the following code to generate a random number in the range a,b.
    Java Code:
             private static Random r = new Random ();
            
            /**
           * Returns a random number in the range [a, b].
            */
            public static double
            boundedRandomNumber (double  a,double  b) {
            if(a >= b) {
            System .err. println (" ERROR : empty interval .");
            return 0.0;
            }
            double x = r. nextDouble ();
            double range = Math . abs (a - b);
            return x * range + a;
    }
    my question is How can I use this piece of code in my code ? :-? Im new to Java and programming, sorry for my ignorance
    Last edited by huuduy216; 09-23-2013 at 06:46 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Use a snippet ?

    Well, it depends on what your program needs to do. But I have a different question. If the teacher checks for an empty interval, why does s/he use the absolute value method? Why not just calculate range as b-a?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default Re: Use a snippet ?

    If the type of the parameters is supposed to be double, the code should say so; Java needs the type of everything, including parameters; now the (lousy) code doesn't even compile.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Use a snippet ?

    I completely missed that.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    huuduy216 is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Use a snippet ?

    This is the homework that I'm working on. I find the method a little bit weird though. I mean the range is very large, chances that none of the points are in the curve is pretty big.
    Use a snippet ?-hw1.jpg


    About the code, I might have mistyped it. so here is a screenshot of it.
    Use a snippet ?-hw2.jpg

    Please help :(

Similar Threads

  1. what this snippet will do?
    By fredfunk in forum New To Java
    Replies: 4
    Last Post: 11-04-2012, 09:07 PM
  2. Code snippet pls?
    By Srinivas0615 in forum New To Java
    Replies: 1
    Last Post: 07-29-2011, 10:32 PM
  3. what are the problems of the snippet
    By QkrspCmptPop in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-26-2010, 01:21 AM
  4. J2ME MIDlet snippet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-21-2007, 11:41 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
  •