My code deals with two classes: Point and Line. Line consists two Points:
import java.util.Scanner;
public class Line {
Scanner scan = new Scanner (System.in);
private Point p1,p2;
public Line(int x1, int x2, int y1, int y2){
p1=new Point(x1,y1);
p2=new Point (x2,y2);
Now I have to make the constructor, but I'm afraid this code is not legal due to Aliasing:
public Line (Point p1, Point p2){
this.p1=p1;
this.p2=p2;
Line a1=new Line (p1.getX(),p1.getY(),(p2.getX()),p2.getY());
string toString method:
public String toString()
{
return "Line between: (" + p1 + p2 + ")";
}
The main is:
Point a=new Point (1,2);
Point b=new Point (2,3);
Line x=new Line(a,b);
System.out.println(a);
System.out.println(b);
System.out.println(x);
The output here comes out as it should, but my question is: 1) The constructor is ok? Does is cause aliasing? 2) the String toString method is legal?
Thank you for answering.
Line a1in the second constructor does not make sense. This creates a second line (and discards it once the constructor is finished).Scannerwith the sameInputStream(System.inin your case) for each instance ofLineis a bad practice, IMO at least.Line a1...;and the end of the constructor, than that line does absolutely nothing.