Path: In this homework, you are to write a method (not a program!) and call it g
ID: 3725345 • Letter: P
Question
Path: In this homework, you are to write a method (not a program!) and call it getPathLength. It receives 6 (omigod 6!) arguments:
the x-coordinate and the y-coordinate of the first point in the path (2 values)
the x-coordinate and the y-coordinate of the second point in the path (2 values)
the x-coordinate and the y-coordinate of the third point in the path (2 values)
The method returns the length of a path that starts at the first point and goes to the second point and then goes on to the third point. The method computes the length of the path by figuring out the distance between the first and second points and then the distance between the second and third points.
The method you write must make use of the method distance2p. (You implemented that method in an earlier Lab.)
SIDE NOTE(the following code is the question prior that needs to be used in this question called distance 2p first here is the question for distance 2p(Distance from one point to another point:Define a method named distance2p that receives as arguments the x- and y- coordinates of one point followed by the x- and y- coordinates of another point (four arguments in all). The method returns the distance between the two points. Use the sqr method that you wrote in an earlier exercise to simplify the code.)
NOW HERE IS THE CODE USED FOR distance 2p(public static double distance2p(double x1, double y1, double x2, double y2){
return Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
}
public static double sqr(double a){
return a*a;
}
))
Explanation / Answer
public static double getPathLength(double x1, double y1, double x2, double y2,double x3, double y3){
return (distance2p(x1,y1,x2.y2)+distance2p(x2,y2,x3.y3));
}
I am assuming as it is mentioned it is done in earlier labs, the two function have already been implemented.
What I am doing I am calling distance between points 1 and 2 by distance2p(x1,y1,x2.y2) and between 2 and 3 by distance2p(x2,y2,x3.y3) and getting the final value of the paths. Hope it is clear. If any doubt is left ask it in comments.