Results 1 to 11 of 11
 02142012, 02:10 PM #1Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
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; 02142012 at 02:15 PM.
 02142012, 02:27 PM #2
Re: Distance Formula and Random numbers code
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 02142012, 02:47 PM #3Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
Re: Distance Formula and Random numbers code
alright, I just did some more research and completely rewrote 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.
 02142012, 02:56 PM #4Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
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(x2x1,2);
double secondvalue= Math.pow(y2y1,2);
double distance=Math.sqrt(firstvalue+secondvalue);
System.out.println(firstvalue);
System.out.println(secondvalue);
System.out.println(answer.format (distance));
}}
 02142012, 03:01 PM #5Moderator
 Join Date
 Apr 2009
 Posts
 12,826
 Rep Power
 22
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?
 02142012, 03:03 PM #6Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
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.
 02142012, 03:46 PM #7
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?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 02142012, 04:02 PM #8Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
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)?
 02142012, 04:46 PM #9Moderator
 Join Date
 Apr 2009
 Posts
 12,826
 Rep Power
 22
Re: Distance Formula and Random numbers code
You could just concatenate a String together.
Java Code:String someString = someVariable + " lots of text " + someOtherVariable;
 02142012, 06:21 PM #10Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
Re: Distance Formula and Random numbers code
Thanks a lot i got it!
 09262013, 07:27 PM #11Member
 Join Date
 Sep 2013
 Posts
 4
 Rep Power
 0
Similar Threads

Two random numbers
By kath09 in forum Java AppletsReplies: 2Last Post: 11132011, 07:11 PM 
Is Random() Only For Numbers?
By Salamander in forum New To JavaReplies: 2Last Post: 02072011, 11:02 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
By jithan in forum Advanced JavaReplies: 3Last Post: 06142008, 02:04 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM
Bookmarks