class Point{ double x,y; public: double getx() {return x;} double gety() {return y;} void setx(double a) { x = a;} void sety(double a) { y = a;} Point(double a,double b):x(a),y(b){} ~Point(){} };
时间: 2023-12-06 08:04:30 浏览: 43
这是一个 C++ 类 Point,表示二维平面上的一个点。该类有两个私有属性 x 和 y,表示点的横坐标和纵坐标。类中定义了四个公有方法:getx 和 gety 用来获取点的横坐标和纵坐标,setx 和 sety 用来设置点的横坐标和纵坐标。类中还定义了一个构造函数 Point(double a,double b),用来初始化 Point 类的对象,同时还定义了一个析构函数 ~Point()。
相关问题
仔细阅读以下所给的代码,完善Point类,在Point类中覆盖Object的toString()和equals()方法,使得编译执行主类Test输出如下结果: public class Test{ public static void main(String[] args){ Point pt1=new Point(12,24,36); Point pt2=new Point(12,24,36); System.out.println("pt1:"+pt1); System.out.println("pt1.equals(pt2):"+ pt1.equals(pt2)); } } class Point{ private double x; private double y; private double z; public Point(){} public Point(double x,double y,double z){ this.x=x; this.y=y; this.z=z; } public double getX(){return this.x;} public void setX(double x){this.x=x;} public double getY(){return this.y;} public void setY(double y){this.y=y;} }
完善后的代码如下:
```
public class Test{
public static void main(String[] args){
Point pt1=new Point(12,24,36);
Point pt2=new Point(12,24,36);
System.out.println("pt1:"+pt1);
System.out.println("pt1.equals(pt2):"+ pt1.equals(pt2));
}
}
class Point{
private double x;
private double y;
private double z;
public Point(){}
public Point(double x,double y,double z){
this.x=x;
this.y=y;
this.z=z;
}
public double getX(){return this.x;}
public void setX(double x){this.x=x;}
public double getY(){return this.y;}
public void setY(double y){this.y=y;}
public double getZ(){return this.z;}
public void setZ(double z){this.z=z;}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + ", z=" + z + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
return false;
return true;
}
}
```
输出结果为:
```
pt1:Point [x=12.0, y=24.0, z=36.0]
pt1.equals(pt2):true
```
其中,Point类中覆盖了Object的toString()和equals()方法,toString()方法返回一个字符串,表示该点的x、y、z坐标值,equals()方法判断两个Point对象是否相等,如果它们的x、y、z坐标值都相等,则返回true,否则返回false。在测试主类Test中,创建了两个Point对象pt1和pt2,它们的坐标值都相等,因此调用equals()方法返回true。在输出pt1时,调用了Point类的toString()方法,返回了该点的x、y、z坐标值。
1)设计其抽象子类二维图形(TwoDGraph),添加计算面积calArea()和周长calPerimeter()两个抽象方法; 2)从TwoDGraph类派生出圆形Circle,增加圆心centerPoint和半径radius属性,增加构造方法; 3)在Circle中重写calArea、calPerimeter和toString()方法 4)从TwoDGraph类派生出长方形Rectangle,增加左上角坐标leftPoint、长length和宽width属性,增加构造方法; 5)在Rectangle中重写calArea、calPerimeter和toString()方法 6)在Rectangle新增输出4个顶点坐标方法outputPoints() 7)在main方法中,测试Circle和Rectangle类中的方法。 public abstract class Graph { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return ""; } } public class Point { private double x; private double y; public Point() { this(0,0); } public Point (double x,double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public String toString() { return String.format("[%.2f, %.2f]", x,y); } }
好的,这是一个关于Java的面向对象编程的问题。以下是代码实现:
```java
public abstract class TwoDGraph extends Graph {
public abstract double calArea();
public abstract double calPerimeter();
}
public class Circle extends TwoDGraph {
private Point centerPoint;
private double radius;
public Circle(String name, Point centerPoint, double radius) {
setName(name);
this.centerPoint = centerPoint;
this.radius = radius;
}
public double calArea() {
return Math.PI * radius * radius;
}
public double calPerimeter() {
return 2 * Math.PI * radius;
}
public String toString() {
return "Circle " + getName() + ": center = " + centerPoint.toString() + ", radius = " + radius;
}
}
public class Rectangle extends TwoDGraph {
private Point leftPoint;
private double length;
private double width;
public Rectangle(String name, Point leftPoint, double length, double width) {
setName(name);
this.leftPoint = leftPoint;
this.length = length;
this.width = width;
}
public double calArea() {
return length * width;
}
public double calPerimeter() {
return 2 * (length + width);
}
public String toString() {
return "Rectangle " + getName() + ": left point = " + leftPoint.toString() + ", length = " + length + ", width = " + width;
}
public void outputPoints() {
Point rightPoint = new Point(leftPoint.getX() + length, leftPoint.getY());
Point bottomPoint = new Point(leftPoint.getX(), leftPoint.getY() - width);
Point diagonalPoint = new Point(leftPoint.getX() + length, leftPoint.getY() - width);
System.out.println("The four points of " + getName() + " are:");
System.out.println(leftPoint.toString() + ", " + rightPoint.toString() + ", " + bottomPoint.toString() + ", " + diagonalPoint.toString());
}
}
public class Test {
public static void main(String[] args) {
Circle circle = new Circle("c1", new Point(0,0), 5);
System.out.println(circle.toString());
System.out.println("Area: " + circle.calArea());
System.out.println("Perimeter: " + circle.calPerimeter());
System.out.println();
Rectangle rectangle = new Rectangle("r1", new Point(0,0), 5, 3);
System.out.println(rectangle.toString());
System.out.println("Area: " + rectangle.calArea());
System.out.println("Perimeter: " + rectangle.calPerimeter());
rectangle.outputPoints();
}
}
```
在这个代码实现中,我们首先创建了一个抽象的二维图形类TwoDGraph,它包含了计算面积和周长的抽象方法。然后我们从TwoDGraph中派生出了圆形类Circle和长方形类Rectangle。这两个类都实现了抽象方法,同时也重写了toString方法以便于输出信息。在Rectangle类中,我们还新增了输出四个顶点坐标的方法outputPoints。最后,在main方法中,我们对Circle和Rectangle类中的方法进行了测试。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)