java课后习题point类
完成教材P116 三 编程题 3 (设计Point类…)
梨花几月份开花
题⽬描述:
Demo类⽤于测试,通过键盘输⼊两个点的坐标,求它们的距离
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
Point p1=new Point();
System.out.println("plea input the first point's (x,y):");
p1.Double());
p1.Double());
Point p2=new Point();
System.out.println("plea input the Second point's (x,y):");
p2.Double());
p2.Double());
忙的成语System.out.println("The distance is "+p1.getDistance(p2));
Point3D p3=new Point3D();
System.out.println("plea input the first point's (x,y,z):");
p3.Double());
p3.Double());
p3.Double());
Point3D p4=new Point3D();
System.out.println("plea input the Second point's (x,y,z):");
p4.Double());
p4.Double());
p4.Double());
System.out.println("The distance is "+p3.getDistance(p4));
资治通鉴白话文sc.clo();
}
}
眉毛简笔画
这是我们所需要的Point类
Point和Point3D类
public class Point {
double x;
double y;
幼儿教育知识
public void tX(double x){
this.x = x;
}
public void tY(double y){托班安全教育教案
this.y = y;
}
public double getX(){
return x;
}
顾亚龙public double getY(){
return y;
}
public double getDistance(Point p2){
return Math.sqrt((this.x-p2.x)*(this.x-p2.x)+(this.y-p2.y)*(this.y-p2.y));
}
}
class Point3D extends Point{
double z;
public void tZ(double z){
this.z = z;
}
public double getZ(){
return z;
}
//⽅法重写
public double getDistance(Point3D p3){
return Math.sqrt((this.x-p3.x)*(this.x-p3.x)+(this.y-p3.y)*(this.y-p3.y)+(this.z-p3.z)*(this.z-p3.z)); }
}
证婚人致辞