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.

Re: Distance Formula and Random numbers code

Quote:

Originally Posted by

**granslime** ... 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

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.

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));

}}

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?

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.

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

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

Re: Distance Formula and Random numbers code

You could just concatenate a String together.

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.

Re: Distance Formula and Random numbers code

Re: Distance Formula and Random numbers code

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