The source files in this project must be organized in a certain way. The file Te
ID: 3562630 • Letter: T
Question
The source files in this project must be organized in a certain way.
The file TestPointLine.java is a test program for your two classes.
Class Specifications
Create two classes named Point and Line that have the following attributes:
In analytic geometry a line is represented by an equation in x and y. For example, y = 3?x+5. To represent this line as an object of class Line we must convert the equation of the line to a standard form: 3?x + (-1)?y + 5 = 0. Then the Line object would have a = 3, b = -1 and c = 5. Notice that x and y are not represented in the Lineobject, just the coefficients.
The Line and Point classes are going to have several methods.
This method should return the distance from this line to the point p. See the discussion below for formulas.
In applying the formula below, note that x0 is the x attribute of p and similarly for y0The a, b and c for the formula are the attributes of the Line object this.
This method should return the distance from this point to the the point p. See the discussion below for formulas.
The coordinates used in computing the distance formula given below are the attributes of the Point object pand the attributes of the Point object this.
Formulas
Distance between two points
We want to compute the distance between two points (x1,y1) and (x2,y2). This is given by the square root of
So, this method computes the formula and returns it.1
Distance from a point to a line
We want to compute the distance from the point with coordinates (x0 ,y0) and the line given by the equation a?x + b?y + c = 0.
Start by computing the square root of a2 + b2. Call this value denom. Compute the absolute value ofa?x0+b?y0+c and divide it by denom. This is the distance value we want to compute.1
Explanation / Answer
// Point.java
package cs2302.assignment2;
public class Point
{
private double x;
private double y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double distance(Point p)
{
double dx = x - p.x;
double dy = y - p.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
// Line.java
package cs2302.assignment2;
public class Line
{
private double a;
private double b;
private double c;
public Line(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
}
public double getA()
{
return a;
}
public double getB()
{
return b;
}
public double getC()
{
return c;
}
public double distance(Point p)
{
double denom = Math.sqrt(a * a + b * b);
return (a * p.getX() + b * p.getY() + c) /denom;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.