Results 1 to 11 of 11
  1. #1
    granslime is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Distance Formula and Random numbers code

    I'm doing an assignment which asks me to create two sets of (x,y) points to be used in the distance formula. So far this is what I have,

    import java.util.Random;
    public class ScannerTester {;

    public static void main(String[] args) {


    Random x1 = new Random();
    System.out.println(x1.nextInt(10)+1);

    Random x2 = new Random();
    System.out.println(x2.nextInt(10)+1);

    Random y1 = new Random();
    System.out.println(y1.nextInt(10)+1);

    Random y2 = new Random();
    System.out.println(y2.nextInt(10)+1);




    }}

    My problem is when I try to use the random points I've created in the distance formula, an error comes up. Are these values not considered integers? How would I use the values I receive from this code in the distance formula? Thanks a lot.
    Last edited by granslime; 02-14-2012 at 02:15 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Distance Formula and Random numbers code

    Quote Originally Posted by granslime View Post
    ... an error comes up
    Post the entire stack trace when seeking help with an error. We don't like to play guessing games here.

    I don't see where you assign any of the random ints to a variable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    granslime is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Distance Formula and Random numbers code

    alright, I just did some more research and completely re-wrote the code. I'll post it when I'm done, I feel like I'm taking the long way to things. Thanks for replying though.

  4. #4
    granslime is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Distance Formula and Random numbers code

    Okay here's my new code. I was able to generate random numbers and use them in the distance formula. The problem is my teacher wants us to display the coordinates as (x,y) and (x,y). I'm not quite sure on how to do that with decimal format. The answer comes out fine though, she wanted it 4 decimal spaces to the right.

    import java.util.Random;
    import java.text.*;
    public class ScannerTester {;

    public static void main(String[] args) {

    Random rand = new Random();
    DecimalFormat cords = new DecimalFormat("0,0");
    DecimalFormat answer = new DecimalFormat("##.0000");


    int x1 = rand.nextInt(10);
    int x2 = rand.nextInt(10);
    int y1 = rand.nextInt(10);
    int y2 = rand.nextInt(10);
    System.out.println(cords.format(x1));
    System.out.println(y1);
    System.out.println(y2);

    double firstvalue= Math.pow(x2-x1,2);
    double secondvalue= Math.pow(y2-y1,2);
    double distance=Math.sqrt(firstvalue+secondvalue);
    System.out.println(firstvalue);
    System.out.println(secondvalue);
    System.out.println(answer.format (distance));


    }}

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Distance Formula and Random numbers code

    So what does that code produce?
    You've got the formatter, what is the problem with producing the desired output?

  6. #6
    granslime is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Distance Formula and Random numbers code

    I already produced the correct output as in the answer which i labeled as distance. I just need a way to list the x1, x2, y1, y2 as (x1,y1) and (x2,y2), with the commas. I printed out the firstvalue and secondvalue as a check for myself. I don't know how to use the formatter for commas between to integers.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Distance Formula and Random numbers code

    Check out the API for PrintStream#printf(...) and String#format(...), and the documentation linked from the latter.

    I suppose you know that System.out is a PrintStream?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    granslime is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Distance Formula and Random numbers code

    I'm not too sure, this is my first semester in Java. So string#format and printstream both display things, but the string#format allows the use of commas? Does this mean that I do not need the decimal format to list the coordinates (x,y)?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Distance Formula and Random numbers code

    You could just concatenate a String together.
    Java Code:
    String someString = someVariable + " lots of text " + someOtherVariable;
    The "proper" way would be to use the printf/format methods, but the above will build a String out of other Strings/objects.

  10. #10
    granslime is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Distance Formula and Random numbers code

    Thanks a lot i got it!

  11. #11
    fscj is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Distance Formula and Random numbers code

    You should separate your Squiggly braces, one on top of the other.

Similar Threads

  1. Two random numbers
    By kath09 in forum Java Applets
    Replies: 2
    Last Post: 11-13-2011, 07:11 PM
  2. Is Random() Only For Numbers?
    By Salamander in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 11:02 AM
  3. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  4. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 03:04 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
  •