定义一个“点”(Point)类用来表示三维空间的点(有三个坐标)。要求如下:1.可以生成具有特定坐标的点对象(带参的构造方法)。
2定义无参的构造方法。
3.提供可以分别设置三个坐标的方法。
4.提供可以计算该“点”到某点距离平方的方法。
5.编写程序验证上述三条。
望采纳,谢谢,代码:
package Test10;
public class Point {
private int x=0;
private int y=0;
private int z=0;
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 int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
public Point(int x, int y, int z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public Point() {
super();
}
public void setPoint(int x , int y ,int z){
this.x=x;
this.y=y;
this.z=z;
}
public double juli(Point point) {
double res = 0;
res= Math.sqrt(Math.pow(Math.abs(this.x-point.getX()), 2)+Math.pow(Math.abs(this.y-point.getY()), 2)+Math.pow(Math.abs(this.z-point.getZ()), 2));
return res;
}
public static void main(String[] args) {
Point point1 = new Point();
Point point2 = new Point(1,1,1);
System.out.println(point1.juli(point2));
}
}
效果:
#include <QCoreApplication>
#include <string>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
struct Point
{
public:
Point(int x,int y,int z)
{
m_x=x;
m_y=y;
m_z=z;
}
Point(){};
void setX(int x){m_x=x;}
void setY(int y){m_y=y;}
void setZ(int z){m_z=z;}
float Distance(Point point)
{
//计算距离公式
int size_x=point.m_x-m_x;
int size_y=point.m_y-m_y;
float value=sqrt(size_x*size_x+size_y*size_y);
return value;
}
private:
int m_x,m_y,m_z;
};
int main()
{
Point p1(1,2,3);
Point p2;
p2.setX(3);
p2.setY(2);
p2.setZ(1);
cout<<"distance = "<<p1.Distance(p2)<<endl;
return 0;
}
效果: