设计一个类表示平面直角坐标系上的点Point私有属性分别为横坐标x与纵坐标y数据类型均为实型数除构造方法以及属性的getter与setter方法外定义一个用于显示信息的方法display用来输出该坐标点的坐标信息格式如下:xy数值保留两位小数。为简化题目其中坐标点的取值范围设定为0200。若输入有误系统则直接输出Wrong Format设计一个类表示平面直角坐标系上的线Line私有属性除了标识线段
Point类的Java代码如下:
public class Point {
private double x;
private double y;
public Point(double x, double y) {
if (x > 0 && x <= 200 && y > 0 && y <= 200) {
this.x = x;
this.y = y;
} else {
System.out.println("Wrong Format");
}
}
public double getX() {
return x;
}
public void setX(double x) {
if (x > 0 && x <= 200) {
this.x = x;
} else {
System.out.println("Wrong Format");
}
}
public double getY() {
return y;
}
public void setY(double y) {
if (y > 0 && y <= 200) {
this.y = y;
} else {
System.out.println("Wrong Format");
}
}
public void display() {
System.out.printf("(%.2f,%.2f)\n", x, y);
}
}
Line类的Java代码如下:
public class Line {
private Point point1;
private Point point2;
private String color;
public Line(Point point1, Point point2, String color) {
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance() {
double dx = point2.getX() - point1.getX();
double dy = point2.getY() - point1.getY();
return Math.sqrt(dx * dx + dy * dy);
}
public void display() {
System.out.println("The line's color is:" + color);
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
}
}
``
原文地址: https://www.cveoy.top/t/topic/eGkX 著作权归作者所有。请勿转载和采集!