Results 1 to 7 of 7
 09262013, 07:20 PM #1Member
 Join Date
 Sep 2013
 Posts
 4
 Rep Power
 0
How to you write a java program using the distance formula?
import java.util.Scanner;
public class Distance
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first two points (x1, y1) :");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter second two points (x2, y2) :");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.pow(((x2  x1)(x2  x1) + (y2  y1)(y2  y1)), 0.5);
System.out.print("The distance of the two points is " + distance); //I'm using JGRASP AND my error is here!
}
}
 09262013, 07:39 PM #2
Re: How to you write a java program using the distance formula?
What exact error are you getting?
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 09262013, 07:48 PM #3Member
 Join Date
 Sep 2013
 Posts
 4
 Rep Power
 0
Re: How to you write a java program using the distance formula?
import java.util.Random;
public class Distance {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first two points (x1, y1) :");
double firstPoints = input.nextDouble();
System.out.print("Enter second two points (x2, y2) :");
double secondPoints = input.nextDouble();
double distance = Math.pow((x2  x1)(x2  x1) + (y2  y1)(y2  y1), 0.5;
System.out.print("The distance of the two points is " + distance);
}
}Last edited by fscj; 09262013 at 07:53 PM. Reason: Wrong program
 09262013, 07:51 PM #4Member
 Join Date
 Sep 2013
 Posts
 4
 Rep Power
 0
Re: How to you write a java program using the distance formula?
Distance.java:14: error: ')' expected
double distance = Math.pow((x2  x1)(x2  x1) + (y2  y1)(y2  y1), 0.5;
^
1 error
 09262013, 08:13 PM #5
Re: How to you write a java program using the distance formula?
Count your opening and closing parenthesis. You should have the same number of each.
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 09262013, 08:40 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: How to you write a java program using the distance formula?
It has to do with the fact hat you can't multiply in Java like (x2x1)(x2x1). It has to be (x2x1)*(x2x1).
And BTW, there is a square root method in the Math class.
Regards,
JimLast edited by jim829; 09272013 at 04:58 AM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09272013, 09:33 AM #7Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
Similar Threads

Distance Formula and Random numbers code
By granslime in forum New To JavaReplies: 10Last Post: 09262013, 07:27 PM 
Distance formula
By cost63 in forum New To JavaReplies: 2Last Post: 07092013, 03:35 AM 
diving distance program
By maximumammo in forum New To JavaReplies: 4Last Post: 04052012, 08:09 PM 
program for a quadratic formula
By bbtgirl in forum New To JavaReplies: 9Last Post: 01302009, 06:06 PM
Bookmarks