In this java assignment we will write simple program to calculate distance between two points. We have two points point1 contains coordinate (x1,y1) and point2 contains coordinates (x2,y2) to calculate distance between two points we will use below Math formula
|
In this below example we have created a Static class called Lines which contains coordinates of points.
Add one static method which will calculate distance between two points
static public double calculateDistanceBetweenPoints( |
import static java.lang.Math.sqrt; public static void main (String args[]) public static class Line { private int pointX; //set instance variables
} public Line(int x, int y){ //method to set arguments to instance variables ///////////////////////////////////////////////// public int getPointX() { public void setPointX(int x) { /////////////////////////////////////////////// public int getPointY() { public void setPointY(int y) { /////////////////////////////////////////////// public String toString(){ //toString method to display the values } static public double calculateDistanceBetweenPoints( |
Run the above code on Java online compilers
Article Contributed By :
|
|
|
|
552 Views |