Results 1 to 10 of 10
- 02-14-2012, 01:10 PM #1
Member
- 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; 02-14-2012 at 01:15 PM.
- 02-14-2012, 01:27 PM #2
Re: Distance Formula and Random numbers code
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-14-2012, 01:47 PM #3
Member
- 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 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.
- 02-14-2012, 01:56 PM #4
Member
- 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(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));
}}
- 02-14-2012, 02:01 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 02-14-2012, 02:03 PM #6
Member
- 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.
- 02-14-2012, 02: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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-14-2012, 03:02 PM #8
Member
- 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)?
- 02-14-2012, 03:46 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Distance Formula and Random numbers code
You could just concatenate a String together.
The "proper" way would be to use the printf/format methods, but the above will build a String out of other Strings/objects.Java Code:String someString = someVariable + " lots of text " + someOtherVariable;
- 02-14-2012, 05:21 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Two random numbers
By kath09 in forum Java AppletsReplies: 2Last Post: 11-13-2011, 06:11 PM -
Is Random() Only For Numbers?
By Salamander in forum New To JavaReplies: 2Last Post: 02-07-2011, 10: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: 04-19-2009, 05:50 PM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks