Okay, so for another assignment I have to find the distance of two points. I understand how that works but for some odd reason, when the program the professor set up tests it, the numbers are way off.

This is what i have done:

public class Distance { 
public static void main(String[] args) { 
double x1=0,x2=2; 
double y1=2,y2=0; 

double X= Math.pow((x2-x1),2); 
double Y= Math.pow((y2-y1),2); 

double d = Math.sqrt(X + Y); 

System.out.println("Distance is: "+d); 
} 
}

If the program tests with 2 points, my results would be like 2.8284271247461903 but the proper answer the program is expecting is 514.03. What did i miss? Could it be because of this line, double d = Math.sqrt(X + Y);

Edited 2 Years Ago by <M/>

the proper answer the program is expecting is 514.03. What did i miss?

You missed gathering the correct co-ordinates. sqrt(2^2 + 2^2) is indeed sqrt(8) => 2.828427 ...

Oh okay, i see what you mean but where would i make the change? would i have to change line 6-7?

Oh, lines 3-4 were a problem? I didn't think those would be the problem... how would i change the inputs in order to get them to work? Would i just remove their values?

Here is what i got so far.

import java.util.Scanner;


public class Exercise2_6
{
    static double distance(double x1, double y1, double x2, double y2)
                {
                return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                }
    public static void main(String[] args)
    {                
        double x2, x1, y2, y1;
        double distance;

                Scanner scan = new Scanner (System.in);

                    System.out.println("Enter the x coordinate for point 1: ");
                    x1 = scan.nextDouble();

                    System.out.println("Enter the y coordinate for point 1: ");
                    y1 = scan.nextDouble();

                    System.out.println("Enter the x coordinate for point 2: ");
                    x2 = scan.nextDouble();

                    System.out.println("Enter the y coordinate for point 2: ");
                    y2 = scan.nextDouble();

                    distance = distance(x1,y1,x2,y2);

                    System.out.println("The distance between the two points is " + distance + " .");

    }

}

Any corrections and how to make decimal point show only 2 digits after?

Edited 2 Years Ago by <M/>

@~s.o.s~, I think i understand that part from another thread of mine and someone (mike) said to use DecimalFormat.

But what about the rest of the code? What do you think about it? Any corrections that you can make?

This is what I got (there are still calculation errors):

import java.text.DecimalFormat;
import java.util.Scanner;
public class Distance {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
        System.out.print("Enter two points: ");
            double x1 = input.nextDouble();
            double y1 =input.nextDouble();
            double x2 = input.nextDouble();
            double y2 = input.nextDouble();
            double X= Math.pow((x2-x1),2);
            double Y= Math.pow((y2-y1),2);
            double z= X + Y;
    DecimalFormat MYformat = new DecimalFormat(",###.00");
        System.out.println("The distance between the two points is " + MYformat.format(z));
  }
}

Here is one of the error logs i get:

First Difference Occurs at: byte 58, line 1
On Line 1 its column 58
Line 1 Wrong: Enter two points: The distance between the two points is 264,228.56
Line 1 Right: Enter two points: The distance between the two points is 514.03 
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter two points: The distance between the two points is 264, | Enter two points: The distance between the two points is 514.

Octal Dump Difference
0000060   o   i   n   t   s       i   s       2   6   4   ,   | 0000060   o   i   n   t   s       i   s       5   1   4   .  
0000100   .   5   6  \n                       | 0000100

Octal Dump Your output...
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :       T   h   e       d   i   s   t   a   n   c   e       b
0000040   e   t   w   e   e   n       t   h   e       t   w   o       p
0000060   o   i   n   t   s       i   s       2   6   4   ,   2   2   8
0000100   .   5   6  \n
0000104

Octal Dump Solution..
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :       T   h   e       d   i   s   t   a   n   c   e       b
0000040   e   t   w   e   e   n       t   h   e       t   w   o       p
0000060   o   i   n   t   s       i   s       5   1   4   .   0   3  \n
0000100

And here is another:

First Difference Occurs at: byte 58, line 1
On Line 1 its column 58
Line 1 Wrong: Enter two points: The distance between the two points is 31,340,181.19
Line 1 Right: Enter two points: The distance between the two points is 5,598.23 
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter two points: The distance between the two points is 31,3 | Enter two points: The distance between the two points is 5,59

Octal Dump Difference
0000060   o   i   n   t   s       i   s       3   1   ,   3   | 0000060   o   i   n   t   s       i   s       5   ,   5   9  
0000100   1   8   1   .   1   9  \n               | 0000100   3  \n

Octal Dump Your output...
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :       T   h   e       d   i   s   t   a   n   c   e       b
0000040   e   t   w   e   e   n       t   h   e       t   w   o       p
0000060   o   i   n   t   s       i   s       3   1   ,   3   4   0   ,
0000100   1   8   1   .   1   9  \n
0000107

Octal Dump Solution..
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :       T   h   e       d   i   s   t   a   n   c   e       b
0000040   e   t   w   e   e   n       t   h   e       t   w   o       p
0000060   o   i   n   t   s       i   s       5   ,   5   9   8   .   2
0000100   3  \n
0000102

I tried changing the way i declared my doubles for the points, it didn't do anything...

First calculate the distance manually.If your coordinates are(0,2) and(2,0) then your answer 2.82... is correct.
So what you are expecting is wrong and what you are getting as an output is correct.

Alright so here is what i ended up with, and now it doesn't collect inputs :( anyone able to fix it?

import java.util.Scanner;
public class Distance
{
    static double distance(double x1, double y1, double x2, double y2)
                {
                return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                }
    public static void main(String[] args)
    {
        double x2, x1, y2, y1;
        double distance;

                Scanner scan = new Scanner (System.in);

                    System.out.println("Enter two points: ");
                    x1 = scan.nextDouble();
                    y1 = scan.nextDouble();
                    x2 = scan.nextDouble();
                    y2 = scan.nextDouble();     
                    distance = distance(x1,y1,x2,y2);
                    System.out.println("The distance between the two points is " + distance + " .");

    }

}

and now it doesn't collect inputs

Yes it does - from lines 16-19. I tested your code. It worked fine.

This is currently my code:

import java.util.Scanner;


public class Distance
{
    static double distance(double x1, double y1, double x2, double y2)
                {
                return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                }
    public static void main(String[] args)
    {
        double x2, x1, y2, y1;
        double distance;

                Scanner scan = new Scanner (System.in);

                    System.out.println("Enter two points: ");
                    x1 = scan.nextDouble();
                    y1 = scan.nextDouble();
                    x2 = scan.nextDouble();
                    y2 = scan.nextDouble();



                    distance = distance(x1,y1,x2,y2);

                    System.out.println("The distance between the two points is " + distance + " .");

    }

}

and this is the error log:

First Difference Occurs at: byte 19, line 1
On Line 1 its column 19
Line 1 Wrong: Enter two points: 
Line 1 Right: Enter two points: The distance between the two points is 514.03 

sdiff side by side difference your output versus solution....
Enter two points:                         | Enter two points: The distance between the two points is 514.
The distance between the two points is 514.0316721759467 .    <

Octal Dump Difference
0000020   :      \n   T   h   e       d   i   s   t   a   n   | 0000020   :       T   h   e       d   i   s   t   a   n   c  
0000040   b   e   t   w   e   e   n       t   h   e       t   | 0000040   e   t   w   e   e   n       t   h   e       t   w  

Octal Dump Your output...
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :      \n   T   h   e       d   i   s   t   a   n   c   e    
0000040   b   e   t   w   e   e   n       t   h   e       t   w   o    
0000060   p   o   i   n   t   s       i   s       5   1   4   .   0   3
0000100   1   6   7   2   1   7   5   9   4   6   7       .  \n
0000116

Octal Dump Solution..
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :       T   h   e       d   i   s   t   a   n   c   e       b
0000040   e   t   w   e   e   n       t   h   e       t   w   o       p
0000060   o   i   n   t   s       i   s       5   1   4   .   0   3  \n
0000100

Help On Intpreting Output Better luck next time!

Why isn't it displaying the points I entered nor the distance between the points itself?

Can someone help me with this one? I think i am getting close to the solution.

which IDE or platform you are using? As I tested your code and it's working correctly without error.[I have used notepad.]
And the answer i am getting with input (0,2) and (2,0) is 2.82...

I am compiling this using the teacher's grading compiler... Could something be wrong with the teacher's compiler? Or is there a better way to write my code that maybe acceptable for the teacher's compiler?

This article has been dead for over six months. Start a new discussion instead.