1. 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。
文件MyPoint.java
public class MyPoint{
public double x,y;
public MyPoint(){
x = 0.0;
y = 0.0;
}
旁清 public MyPoint(double x,double y){
this.x = x;
this.y = y;
}
public void show(){
System.out.println("("+x+","+y+")");
}
public static MyPoint getMiddle(MyPoint p1,MyPoint p2){
MyPoint p = new MyPoint((p1.x+p2.x)/2,(p1.y+p2.y)/2);
return p;
}
}
文件Test.java
public class Test{
public static void main(String[] args){
族渣 MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint(6.0,8.0);
MyPoint p = MyPoint.getMiddle(p1,p2);
兆启悄 System.out.print("点1:");
p1.show();
System.out.print("点2:");
p2.show();
System.out.print("中点:");
p.show();
}
}
望采纳~
public class Point {
public double x;
public double y;
Point(double x, double y) {
this.x=x;
this.y=y;
}
static Point getMiddle(Point point1,Point point2){
double px=(point1.x+point2.x)/2;
double py=(point1.y+point2.y)/此缺2;
Point p=new Point(px,py);
return p;
}
void show(){
System.out.println("点的坐标:("+x+","+y+")"森斗辩销掘);
}
public static void main(String[] args) {
Point point1=new Point(0,0);
point1.show();
Point point2=new Point(4,9);
point2.show();
Point p=getMiddle(point1,point2);
p.show();
}
}