用java编写,要求如下:设计一个Point类,包含两个int型成员变量x和y,给出此类的2个构造方法,一个不带参数,一个带两个参数;给出对应的set和get方法;给出一个计算当前点和参数所给点之间距离的方法,格式如下:public double distance(Point another);最后重写equals方法用于判断两个点对象是否相同,重写toString方法,返回用于显示点坐标的字符串信息。 在测试类Test3_5中编写main方法测试,main方法代码如下: public static void main(String args[]){ Point a=new Point(); Point b=new Point(50,60); System.out.println(“b: [” +b.getX()+”,”+b.getY()+”]”); a.setX(100); a.setY(200); System.out.println(“a==b? ” +a.equals(b)); System.out.println(“a---->b :” +a.distance(b)); }
时间: 2023-06-10 16:03:49 浏览: 327
好的,您想问关于Java编写Point类的问题,我会根据您的要求给您提供相应的答案。以下是关于Java编写Point类的介绍:
Point类是描述一个点的类,其中包含两个int型成员变量x和y,用于表示点的横纵坐标。下面是Point类的定义:
public class Point{
private int x; //点的横坐标
private int y; //点的纵坐标
//构造方法1:不带参数
public Point(){
this.x = 0;
this.y = 0;
}
//构造方法2:带两个参数
public Point(int x, int y){
this.x = x;
this.y = y;
}
//set方法:设置点的横坐标
public void setX(int x){
this.x = x;
}
//get方法:获取点的横坐标
public int getX(){
return this.x;
}
//set方法:设置点的纵坐标
public void setY(int y){
this.y = y;
}
//get方法:获取点的纵坐标
public int getY(){
return this.y;
}
//计算当前点和参数所给点之间的距离
public double distance(Point another){
int dx = this.x - another.getX();
int dy = this.y - another.getY();
return Math.sqrt(dx*dx + dy*dy);
}
//重写equals方法用于判断两个点对象是否相同
@Override
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj instanceof Point){
Point p = (Point)obj;
return (this.x == p.getX()) && (this.y == p.getY());
}
return false;
}
//重写toString方法,返回用于显示点坐标的字符串信息
@Override
public String toString(){
return "[" + this.x + "," + this.y + "]";
}
}
在上述代码中,我们提供了两个构造方法,一个不带参数,一个带两个参数。同时,我们还提供了set和get方法用于设置和获取点的横纵坐标。在计算两个点之间距离的方法中,我们使用勾股定理求解。
在测试类Test3_5中编写main方法进行测试,代码如下:
public class Test3_5{
public static void main(String args[]){
Point a=new Point();
Point b=new Point(50,60);
System.out.println("b: " + b.toString());
a.setX(100);
a.setY(200);
System.out.println("a==b? " + a.equals(b));
System.out.println("a---->b :" + a.distance(b));
}
}
在上述代码中,我们首先创建了两个Point对象a和b,然后分别对其进行了横纵坐标的设置和获取,并且计算了a和b之间的距离。最后,我们使用equals方法比较了a和b两个点对象是否相同,同时又使用toString方法返回了点坐标的字符串信息。
希望可以帮到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)