Basically we were assigned in java to write a class called Line that essentially
ID: 3633501 • Letter: B
Question
Basically we were assigned in java to write a class called Line that essentially represents an line segment between two Points.
To construct this, the following methods must be used in the newly created class Line:
public Line(Point p1, Point p2){
// Which constructs a new Line that contains the given two Points.
}
public Point getP1(){
// Returns the Line's first endPoint
}
public Point getP2(){
// Returns the Line's second endPoint
}
public String toString(){
// Returns a String representation of this Line, such as "[(12, 2), (3, 6)]".
}
Don't forget to also import java.awt.*, that way you can reference a point class.
Thanks for any help you can provide.
Explanation / Answer
import java.awt.Point; public class Line{ private Point point1, point2; public Line(Point p1, Point p2){ point1 = p1; point2 = p2; } public Point getP1(){ return point1; } public Point getP2(){ return point2; } public String toString(){ return "[(" + point1.x + "," + point1.y + "), (" + point2.x + "," + point2.y +")]"; } //Test so that you can see that this works. public static void main(String args[]){ Line l = new Line(new Point(1,2), new Point(3,4)); System.out.println(l.toString()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.