-4

I need to Implement the classes Point and Line The toString method of Point Should print a point in the form (x,y) The toString method of Line Should print a point in the form (x1,y1)->(x2,y2)

Then I need to create a Class called LineApp in the main do the following: Create a Line with the points (0,0) and (4,3) Then calculate and display the length of the line.

I have been looking at this and have no clue what to do

4

3 回答 3

0

@override在自定义您自己的toString()方法之前,您必须首先 创建对象类。

于 2013-05-23T20:22:00.907 回答
0

该类的toString()方法Point应如下所示:

public String toString() {
   return "(" + x + "," + y + ")";
}

类的toString()方法Line应该如下所示(假设您Point在类中有两个 type 成员Line):

public String toString() {
   return "(" + point_A.getX() + "," + point_A.getY() + ")->" +
          "(" + point_B.getX() + "," + point_B.getY() + ");
}

在数学上,如果你有两个点:A(x1, y1) 和 B(x2, y2),线 AB 的长度将使用以下公式计算:

AB * AB = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)

因此,您可以在代码中执行以下细化:

Double length = Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
System.out.println(length);
于 2013-05-23T20:07:06.130 回答
0

例如, Line 类的 toString() 方法如下所示:

public String toString() {
    return "(" + x1 + "," + y1 + ")->(" + x2 + "," + y2 + ")";
}
于 2013-05-23T20:06:35.753 回答