创建Point文件,其中有Point类,该类有两个int类型的坐标属性x和y;类中有如下方法; (1) 使用getter方法和setter方法(课上讲了,书上第90页也有介绍)获取和改变属性值; (2)类中有toString方法,能够返回该点的坐标信息; (3)返回类型为boolean类型的equals方法,功能为:将自身的坐标值与作为参数传入的Point对象的坐标值进行比较,坐标相同返回true,坐标不同返回false。 (4)类中编写main方法,在方法中创建两个坐标分别为(10,20)的点m和坐标为(50,100)的点n,创建成功后在控制台打印两个点的坐标信息;点n调用euqals方法自动判断其与点m是否是同一点,并将结果打印在控制台。并根据自己的实现方式,写出预期的运行结果。
时间: 2023-02-12 09:16:52 浏览: 111
下面是一个符合题目要求的Point类的示例代码:
```
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
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 String toString() {
return "(" + x + "," + y + ")";
}
public boolean equals(Point other) {
if (other == null) {
return false;
}
if (x == other.x && y == other.y) {
return true;
}
return false;
}
public static void main(String[] args) {
Point m = new Point(10, 20);
Point n = new Point(50, 100);
System.out.println("m的坐标为:" + m);
System.out.println("n的坐标为:" + n);
System.out.println("m与n是否为同一点:" + n.equals(m));
}
}
```
预期结果:
```
m的坐标为:(10,20)
n的坐标为:(50,100)
m与n是否为同一点:false
```
说明:
- 通过构造方法 Point(int x, int y) 实例化对象时设置坐标
- getter 和 setter 方法可以获取或修改坐标的值
- toString() 方法返回坐标信息
- equals() 方法可以比较两个点的坐标是否相同
- main() 方法中创建了两个点m和n, 并分别打印它们的坐标信息, 最后判断m与n是否为同一点
阅读全文
相关推荐
















