Only want to read variables declared within certain constructors
For a programming problem, my textbook asks me to create four constructors to represent 4 different ways in which a line in a plane can be specified. Those 4 ways being, by giving a point and the slope, by giving two points, as an equation in slope-intercept form and an equation x = a if the line is vertical.
Here is my code:
Code:
public class Line
{
private double x;
private double y;
private double slope;
private double x1;
private double x2;
private double y1;
private double y2;
private double m;
private double yInt;
private double a;
private double aSlope;
public Line(double xVal, double yVal, double slopeVal)
{
x = xVal;
y = yVal;
slope = slopeVal;
}
public Line(double x1Val, double y1Val, double x2Val, double y2Val)
{
x1 = x1Val;
x2 = x2Val;
y1 = y1Val;
y2 = y2Val;
}
public Line(double mVal, double yIntVal)
{
m = mVal;
yInt = yIntVal;
}
public Line(double aVal)
{
a = aVal;
aSlope = 0;
}
//Need to find a way to only read, or use, values from two lines that are being compared
public boolean isParallel(Line other)
{
boolean parallel = false;
if(slope == other.m || (other.y2-other.y1)/(other.x2-other.x1) == slope || m == (other.y2-other.y1)/(other.x2-other.x1)
)
{
parallel = true;
}
return parallel;
}
public boolean equals(Line other)
{
boolean equal = false;
if(isParallel(other) && (yInt == other.a || ((-(other.y2-other.y1)/(other.x2-other.x1) * other.x1)+other.y1) == ((-slope * x)+ y)
|| yInt == ((-other.slope * other.x)+ other.y)) || ((-(other.y2-other.y1)/(other.x2-other.x1) * other.x1)+other.y1) == yInt)
{
equal = true;
}
return equal;
}
public boolean intersects(Line other)
{
boolean intersect = false;
if(equals(other) || !isParallel(other))
{
intersect = true;
}
return intersect;
}
}
And the tester class:
Code:
public class LineTester
{
public static void main(String[] args)
{
Line line1 = new Line(1, 1, 0.5);
Line line2 = new Line(1, 1, 1, 2);
Line line3 = new Line(0.5, -1);
Line line4 = new Line(1);
System.out.println(line1.equals(line2));
System.out.println("Expected: false");
System.out.println(line2.equals(line4));
System.out.println("Expected: true");
System.out.println(line1.intersects(line2));
System.out.println("Expected: true");
System.out.println(line1.intersects(line3));
System.out.println("Expected: false");
System.out.println(line1.isParallel(line3));
System.out.println("Expected: true");
System.out.println(line1.isParallel(line4));
System.out.println("Expected: true");
System.out.println(line1.isParallel(line2));
System.out.println("Expected: false");
}
}
So, it's buggy and I think I know the reason. When my methods run, they aren't comparing just the variables of the lines that are called to check if the lines are parallel, equal, etc. (i.e. only slope and m if comparing lines where one is constructed using three explicit parameters and the other is constructed using two explicit parameters), but they are checking if any of the conditions are true, regardless of whether the two lines being compared include variables that are defined in the other constructors or not.
I can't change certain parts of the code because the book says that I must use them. Those are:
Implement a class Line with four constructors, corresponding to the four cases above (4 different ways to specify a line in a plane).
Implement methods
boolean intersects(Line other)
boolean equals(Line other)
boolean isParallel (Line other)
Use the following class as your tester class:
public class LineTester
{
public static void main(String[] args)
{
Line line1 = new Line(1, 1, 0.5);
Line line2 = new Line(1, 1, 1, 2);
Line line3 = new Line(0.5, -1);
Line line4 = new Line(1);
System.out.println(line1.equals(line2)) ;
System.out.println("Expected: false");
System.out.println(line2.equals(line4)) ;
System.out.println("Expected: true") ;
System.out.println(line1.intersects(line2)) ;
System.out.println("Expected: true") ;
System.out.println(line1.intersects(line3)) ;
System.out.println("Expected: false");
System.out.println(line1.isParallel(line3)) ;
System.out.println("Expected: true") ;
System.out.println(line2.isParallel(line4)) ;
System.out.println("Expected: true") ;
System.out.println(line1.isParallel(line2)) ;
System.out.println("Expected: false");
}
}
Any help would be greatly appreciated.