要求如下:
- 可以生成具有特定坐标的点对象;
- 提供可以设置三个坐标的方法;
- 提供可以计算该“点”距原点距离平方的方法。
源代码如下:
import java.util.Scanner;
public class Point {
private int x;//x坐标
private int y;//y坐标
private int z;//z坐标
public Point(int x,int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
void showPoint(){//显示点坐标
System.out.print("点的坐标为: ");
System.out.println("(" + x + "," + y + "," + z + ")");
}
void squareOfDistance(){//计算距离原点的平方
System.out.print("该点距离原点的平方为: ");
System.out.println(x*x + y*y + z*z);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("请输入点的x,y,z坐标: ");
Scanner in = new Scanner(System.in);//键盘输入
Point point1 = new Point(in.nextInt(),in.nextInt(),in.nextInt());
point1.showPoint();
point1.squareOfDistance();
in.close();
}
}