Calculating the distance between 2 given points

Vic Billy

I have to create a class to calculate the distance between 2 given points for class. The instructor gave us the top half of the assignment with all the necessary code without modifying, the problem that im having is creating the class part. This is what i have so far...

class Point{
int x;
int y;

public Point(){
    this.x = 0;
    this.y = 0;

}

public Point(int x, int y){
    this.x = x;
    this.y = y;
}
 public double distance(int x, int y) {
    double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );
    return distance;
}
}

the top half of the assignment looks like this:

import java.util.Scanner;


class Assignment4{
public static void main(String[] args){
    // first and second points
    Point first, second;

    // try parsing points from command line args
    if(args.length==4){
        // new Point(int x, int y) creates a new Point located at position (x,y)
        first = new Point(Integer.valueOf(args[0]), Integer.valueOf(args[1]));
        second = new Point(Integer.valueOf(args[2]), Integer.valueOf(args[3]));
    }

    // if not specified as argument, get points from user
    else{
        Scanner input = new Scanner(System.in);

        System.out.println("Enter first point: ");
        first = new Point(input.nextInt(),input.nextInt());

        System.out.println("Enter second point: ");
        second = new Point(input.nextInt(),input.nextInt());
    }

    //calculate distance
    //double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );
    double d = first.distance(second.x, second.y);

    System.out.println("Distance between " + 
        "(" + first.x + "," + first.y + ")" + 
        " and " + 
        "(" + second.x + "," + second.y + ")" + 
        " is " + d);
    System.out.println();
}
}

When i try and compile the program, it says "cannot find symbol" referring to x2, x1, y2, y1, and distance.

j4rey

Here :

class Point{
     int x;
     int y;

     .....
     .....

     public double distance(int x, int y) {
        double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );  //ERROR IN THIS LINE
        return distance; //ERROR HERE TOO...(2)
     }
}

There is no x1,x2,y1,y2 defined in the class or in the method parameter.

Swap it with the following line:

double d = Math.sqrt( Math.pow(this.x-x, 2) + Math.pow(this.y-y, 2) );

(2) Error 2 Swap with this line:

return d;

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

calculating angular distance between 2 points, w.r.t. to a third point

분류에서Dev

I m calculating distance between 2 points in map in real time as the user walks if destination is fixed

분류에서Dev

Assembly YASM Distance Squared Between 2 Points

분류에서Dev

Calculating the days between two given dates

분류에서Dev

Calculating Time Difference between 2 Dates

분류에서Dev

Calculating floating points as binary

분류에서Dev

Generating php script for calculating points

분류에서Dev

Trying to find distance between two points, but I'm getting wrong answers:(

분류에서Dev

Calculate the Euclidean distance of 3 points

분류에서Dev

Divide points into sets of maximum distance

분류에서Dev

Finding the distance between two objects in a 2D array, not going Diagonal

분류에서Dev

Distance Between Two Geo Coordinates

분류에서Dev

Calculating percentage difference between rows on table

분류에서Dev

Calculating percentage difference between rows on table

분류에서Dev

Differences in distance calculated between javascript function and sql server STD distance

분류에서Dev

urxvt add padding (distance between text and border)

분류에서Dev

Shortest distance between a particular node and a point

분류에서Dev

STDistance – Calculates distance between two objects

분류에서Dev

How to calculate the Euclidean distance between vectors?

분류에서Dev

matplotlib fill between discrete points

분류에서Dev

Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

분류에서Dev

Is there a CGAL function for finding all intersecting points between a 2D line (Line_2) and a set of 2D polygons with holes (Polygon_with_holes_2)?

분류에서Dev

Java Regex match anything that matches given pattern and is not between a given character

분류에서Dev

How to score points based on the answers given in the entity form

분류에서Dev

How do I produce an interpolation function given n data points?

분류에서Dev

Faster way of finding distances between combinations of points

분류에서Dev

Points exchange between users Rails 4

분류에서Dev

Rename file name between two points

분류에서Dev

In R, how to sum the distance between rows (each one is a GPS coordinate)?

Related 관련 기사

  1. 1

    calculating angular distance between 2 points, w.r.t. to a third point

  2. 2

    I m calculating distance between 2 points in map in real time as the user walks if destination is fixed

  3. 3

    Assembly YASM Distance Squared Between 2 Points

  4. 4

    Calculating the days between two given dates

  5. 5

    Calculating Time Difference between 2 Dates

  6. 6

    Calculating floating points as binary

  7. 7

    Generating php script for calculating points

  8. 8

    Trying to find distance between two points, but I'm getting wrong answers:(

  9. 9

    Calculate the Euclidean distance of 3 points

  10. 10

    Divide points into sets of maximum distance

  11. 11

    Finding the distance between two objects in a 2D array, not going Diagonal

  12. 12

    Distance Between Two Geo Coordinates

  13. 13

    Calculating percentage difference between rows on table

  14. 14

    Calculating percentage difference between rows on table

  15. 15

    Differences in distance calculated between javascript function and sql server STD distance

  16. 16

    urxvt add padding (distance between text and border)

  17. 17

    Shortest distance between a particular node and a point

  18. 18

    STDistance – Calculates distance between two objects

  19. 19

    How to calculate the Euclidean distance between vectors?

  20. 20

    matplotlib fill between discrete points

  21. 21

    Why does this line of GLSL for calculating distance produce inconsistent results, when this one works as expected?

  22. 22

    Is there a CGAL function for finding all intersecting points between a 2D line (Line_2) and a set of 2D polygons with holes (Polygon_with_holes_2)?

  23. 23

    Java Regex match anything that matches given pattern and is not between a given character

  24. 24

    How to score points based on the answers given in the entity form

  25. 25

    How do I produce an interpolation function given n data points?

  26. 26

    Faster way of finding distances between combinations of points

  27. 27

    Points exchange between users Rails 4

  28. 28

    Rename file name between two points

  29. 29

    In R, how to sum the distance between rows (each one is a GPS coordinate)?

뜨겁다태그

보관