Timesobserver
29 Sep 2009, 11:29 PM
Hi all,
I decided that I haven't tortured myself enough and decided to try my hand with Java. Here's my problem:
Write an application that reads the (x,y) coordinates for two points. Compute the distance between the two points using the following formula:
Distance= squared (x2 - x1) squared + (y2-y1) squared
This is what I have so far:
(It says PHP down below but it's really Java)
import java.util.* ;
public class distance
{
public static void main( String [] args )
{
Scanner keyboard = new Scanner( System.in) ;
double dblX1;
double dblY1;
double dblX2;
double dblY2;
//double dblDiffX;
//double dblDiffY;
double dblDiffX = dblX2 - dblX1;
double dblDiffY = dblY2 - dblY1;
System.out.println("Please input x: \n" );
double dblDiffX1 = keyboard.nextDouble() ;
System.out.println("Please input y: \n" );
double dblDiffY1 = keyboard.nextDouble() ;
double distance = Math.sqrt( (dblDiffX - dblX2) + (dblY1 - dblDiffY));
//double distance = Math.sqrt( dblX1 * dblX1 + dblY1 * dblY1 ) ; //sqrt() calculates the square root
System.out.println( "The distance is: " + distance ) ;
} //end main
}
I think my biggest problem is not being able to figure out the math formula. Any suggestions or help?
I decided that I haven't tortured myself enough and decided to try my hand with Java. Here's my problem:
Write an application that reads the (x,y) coordinates for two points. Compute the distance between the two points using the following formula:
Distance= squared (x2 - x1) squared + (y2-y1) squared
This is what I have so far:
(It says PHP down below but it's really Java)
import java.util.* ;
public class distance
{
public static void main( String [] args )
{
Scanner keyboard = new Scanner( System.in) ;
double dblX1;
double dblY1;
double dblX2;
double dblY2;
//double dblDiffX;
//double dblDiffY;
double dblDiffX = dblX2 - dblX1;
double dblDiffY = dblY2 - dblY1;
System.out.println("Please input x: \n" );
double dblDiffX1 = keyboard.nextDouble() ;
System.out.println("Please input y: \n" );
double dblDiffY1 = keyboard.nextDouble() ;
double distance = Math.sqrt( (dblDiffX - dblX2) + (dblY1 - dblDiffY));
//double distance = Math.sqrt( dblX1 * dblX1 + dblY1 * dblY1 ) ; //sqrt() calculates the square root
System.out.println( "The distance is: " + distance ) ;
} //end main
}
I think my biggest problem is not being able to figure out the math formula. Any suggestions or help?