I have created a program that creates an object called "Point" in the class "Point". Now I wonder how to find the distance between two points created in the Main method. The instructions from my teacher are :
Add a new method to the Point class named "distance".
public double distance (Point other)
Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of their x- and y- coordinates. In other words, the distance between two points (x1, y1) and (x2, y2) can be expressed as the square root of (x2 - x1)^2 (y2 - y1) ^2. Two points with the same (x, y) coordinates should return a distance of 0.0.
Below you can see my code and the task is to calculate the distance between point a and point b.
//Dimitar Kapitanov 11/6
using System;
public class PointClassPt2
{
public static void Main(String[] args)
{
//main method
Point a = new Point();
Console.WriteLine("First Point");
Console.Write("X: ");
double x = Convert.ToDouble(Console.ReadLine());
Console.Write("Y: ");
double y = Convert.ToDouble(Console.ReadLine());
a.setCoordinate(x, y);
Point b = new Point(x, y);
Console.WriteLine("\nSecond Point");
Console.Write("X: ");
x = Convert.ToDouble(Console.ReadLine());
Console.Write("Y: ");
y = Convert.ToDouble(Console.ReadLine());
b.setCoordinate(x, y);
Console.WriteLine("\nPoint A: ("
a.getXCoordinate() " , " a.getYCoordinate() ")");
Console.WriteLine("Point B: ("
b.getXCoordinate() " , " b.getYCoordinate() ")");
Point c = new Point();
c.setCoordinate(-x, -y);
Console.WriteLine("Point C: ("
c.getXCoordinate() " , " c.getYCoordinate() ")");
Console.WriteLine("Distance from A to B: ");
}
}
class Point
{
public double _x;
public double _y;
public Point()
{
_x = 0;
_y = 0;
}
public Point(double x, double y)
{
_x = x;
_y = y;
}
public double getXCoordinate()
{
return _x;
}
public double getYCoordinate()
{
return _y;
}
public void setCoordinate(double x, double y)
{
_x = x;
_y = y;
}
}
The thing that I don't understand is how to get the double values of the coordinates of Point a and Point b in order to calculate the distance in the method distance. And what does he mean by the sending the method distance "Point other". Can someone help me what the method distance should look like and what should I send it as parameters in the main method?
CodePudding user response:
Since this is your homework, i am not going to give you the full answer but some pseudo code,
Inside your Point
class, you will create a method distance
public double distance (Point other)
{
var otherX = other.getXCoordinate();
var otherY = other.getYCoordinate();
//you already have access to the current point _x and _y
//now you can do the distance calculation here.
var distance = //your formula
return distance;
}
CodePudding user response:
You could add a method, like below
public double Distance(Point other)
{
if(null == other )
return 0;
//calculate the distance with the formula you already mentioned
//double dist = sqrt( Math.Pow((other.getXcoordinate() - _x),2) ...
return dist;
}