-->
当前位置:首页 > 题库

PROGRAMMING:Two points form a line

Luz5年前 (2021-05-10)题库472
It is said that two points determine a line, so the design of a line class line needs to be determined by two point objects. The specific requirements of line class are as follows: < br / >
1) Two point objects P1 and P2 are defined< br/>
2) Write out the parametric construction method and pass two object values to P1, P2 < br / >
3) Write setters and getters methods for P1 and P2 < br / >
4) Write a getlength method for line to find the length of two points in the line < br / >
5) Write a toString method for line as follows: < br / >
public String toString() {
return "Line [p1=" + p1 + ", p2=" + p2 + "]";
}

In the main method of the main class, a line array is defined. The length of the array is given by the keyboard, and then the coordinates of the two point objects of each line segment are assigned by the keyboard. The corresponding line objects are generated and put into the array. The circular array outputs the information of each line and the distance between the two points< br/>
The point class is as follows: < br / >

public class Point {
private int x, y;// x. Y is the coordinate of the point
//Find the distance between two points
public double distance(Point p1) {
return Math.sqrt((p1.x -this.x)*(p1.x -this.x)+(p1.y-this.y)*(p1.y-this.y));
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point() {
super();
x = y =0;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}

###Input format:
The first line is the length of the input array n
Enter the X, y coordinates of two point objects of a line object in each line, separated by spaces
###Output format:
Loop output, output line information, as well as the distance between two points of each line, retain a decimal place.
###Input example:
Here is a set of inputs. For example:
```in
two
0 0 2 3
1 3 2 5
```
###Output example:
The corresponding output is given here. For example:
```out
Line [p1=Point [x=0, y=0], p2=Point [x=2, y=3]]
The length of this line segment is: 3.6
Line [p1=Point [x=1, y=3], p2=Point [x=2, y=5]]
The length of this line segment is: 2.2
```







answer:If there is no answer, please comment