How can I find the distance between two points using only two variables and after that store all points and obtain the shape?

Viewed 294

I tried to declare two variable x and y, then create constructor for them and getters with setters. So, for this I used class Distance, while for the obtaining shape I need another class.

package com.company;
import java.lang.Math;

public class Point {
    //fields
    private int x;
    private int y;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //method
        //getters
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
        //setters
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }

    public int getPoint(){

    }

    //function distance
    public void distance() {
        //**here I need somehow use only two variables instead of four**
        double res = Math.sqrt((Math.pow(getX1(), 2) - Math.pow(getX2(), 2))
                + (Math.pow(getY1(), 2) - Math.pow(getY2(), 2)));
        System.out.println(res);
    }
}

1 Answers

Create a function that accepts object of type Point. The function returns the distance between the original point and passed point

public void distance(Point po) {
    //**here I need somehow use only two variables instead of four**
    double res = Math.sqrt(
                     Math.pow(getX() - po.getX(), 2) +
                     Math.pow(getY() - po.getY(), 2)
    );
    System.out.println(res);
}

Also your function to calculate distance was wrong.

Related