编程题目:利用组成关系,编写线段和点之间的关系,并求线段长度。
创建三个类testpoint为主类
package java1;
public abstract class testpoint {
public static void main(String[] args) {
// TODO Auto-generated method stub
point p1=new point(1,2);
point p2=new point(6,8);
line Line=new line(p1,p2);
Line.length();
}
}
第二个类line
package java1;
public class line {
point p1,p2;
public line(point p1, point p2) {
super();
this.p1 = p1;
this.p2 = p2;
}
void length(){
double length=Math.sqrt(Math.pow((p1.x-p2.x),2)+Math.pow((p1.y-p2.y), 2));
System.out.print(length);
}
}
第三个类:point
package java1;
public class point {
int x;
int y;
public point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";//tostring的改编
}
}