定义相应的字段和方法,具体字段和方法见下表。要求:在主方法中创建两个Point类的对象,调用各方法完成设置两点坐标、计算两点间距离、屏幕打印点坐标信息等功能。
2条回答 默认 最新
- threenewbee 2023-04-09 07:00关注
public class Point { private int x; private int y; public Point() { } 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 double getDistance(Point another) { int xDiff = this.x - another.x; int yDiff = this.y - another.y; return Math.sqrt(xDiff * xDiff + yDiff * yDiff); } public void print() { System.out.println("(" + x + "," + y + ")"); } }
主程序
public static void main(String[] args) { Point p1 = new Point(3, 4); Point p2 = new Point(5, 6); System.out.println("点1的坐标为:"); p1.print(); System.out.println("点2的坐标为:"); p2.print(); double distance = p1.getDistance(p2); System.out.println("两点间距离为:" + distance); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用