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:
publicclassDistance{publicstaticvoidmain(String[] args){double x1=0,x2=2;double y1=2,y2=0;doubleX=Math.pow((x2-x1),2);doubleY=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);
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?
importjava.util.Scanner;publicclassExercise2_6{staticdoubledistance(double x1,double y1,double x2,double y2){returnMath.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));}publicstaticvoidmain(String[] args){double x2, x1, y2, y1;double distance;Scanner scan =newScanner(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?
~s.o.s~2,560 Failure as a human Team ColleagueFeatured Poster
Any corrections and how to make decimal point show only 2 digits after?
This is what I got (there are still calculation errors):
importjava.text.DecimalFormat;importjava.util.Scanner;publicclassDistance{publicstaticvoidmain(String[] args){Scanner input =newScanner(System.in);System.out.print("Enter two points: ");double x1 = input.nextDouble();double y1 =input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();doubleX=Math.pow((x2-x1),2);doubleY=Math.pow((y2-y1),2);double z=X+Y;DecimalFormatMYformat=newDecimalFormat(",###.00");System.out.println("The distance between the two points is "+MYformat.format(z));}}
Here is one of the error logs i get:
FirstDifferenceOccurs at:byte58, line 1OnLine1 its column 58Line1Wrong:Enter two points:The distance between the two points is 264,228.56Line1Right:Enter two points:The distance between the two points is 514.03You have only a FirstLineError
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.OctalDumpDifference0000060 o i n t s i s 264,|0000060 o i n t s i s 514.0000100.56 \n |0000100OctalDumpYour output...0000000E 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 264,2280000100.56 \n
0000104OctalDumpSolution..0000000E 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 514.03 \n
0000100
And here is another:
FirstDifferenceOccurs at:byte58, line 1OnLine1 its column 58Line1Wrong:Enter two points:The distance between the two points is 31,340,181.19Line1Right:Enter two points:The distance between the two points is 5,598.23You have only a FirstLineError
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,59OctalDumpDifference0000060 o i n t s i s 31,3|0000060 o i n t s i s 5,590000100181.19 \n |00001003 \n
OctalDumpYour output...0000000E 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 31,340,0000100181.19 \n
0000107OctalDumpSolution..0000000E 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,598.200001003 \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.
importjava.util.Scanner;publicclassDistance{staticdoubledistance(double x1,double y1,double x2,double y2){returnMath.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));}publicstaticvoidmain(String[] args){double x2, x1, y2, y1;double distance;Scanner scan =newScanner(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:
FirstDifferenceOccurs at:byte19, line 1OnLine1 its column 19Line1Wrong:Enter two points:Line1Right: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.<OctalDumpDifference0000020: \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
OctalDumpYour output...0000000E 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 514.03000010016721759467. \n
0000116OctalDumpSolution..0000000E 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 514.03 \n
0000100HelpOnIntpretingOutputBetter 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?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.