1 定义一个点,包含x,y坐标。包含无参构造函数和有参构造函数。 2 定义一条边,拥有起点、终点,拥有长度属性(长度可计算)。 3 定义一个长方形,由四条边(或四个顶点)组成,拥有面积、周长属性(可计算)。判断其有效性。 4 分别测试点对象、边对象和长方形对象。
时间: 2024-09-28 15:09:35 浏览: 45
在面向对象编程中,我们可以定义如下的类结构:
1. **定义点类 Point**:
```java
public class Point {
private double x;
private double y;
// 无参构造函数
public Point() {
this.x = 0;
this.y = 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 void printCoordinates() {
System.out.println("Point (" + x + ", " + y + ")");
}
}
```
2. **定义边类 Edge**:
```java
public class Edge {
private Point start;
private Point end;
private double length;
public Edge(Point start, Point end) {
this.start = start;
this.end = end;
this.length = Math.sqrt(Math.pow(end.getX() - start.getX(), 2) + Math.pow(end.getY() - start.getY(), 2));
}
// 获取和设置方法省略...
public double getLength() { return length; }
// 测试边对象
public void printEdgeInfo() {
System.out.println("Edge from (" + start.getX(), start.getY()) + " to (" + end.getX(), end.getY()) + ": " + length);
}
}
```
3. **定义长方形类 Rectangle**:
```java
public class Rectangle {
private Edge edge1, edge2, edge3, edge4; // 假设每个边都是由两个点构成的
public Rectangle(Edge[] edges) {
// 验证边的有效性
if (edges.length != 4 || !isValidEdges(edges)) {
throw new IllegalArgumentException("Invalid rectangle edges");
}
// 设置边
this.edge1 = edges[0];
this.edge2 = edges[1];
this.edge3 = edges[2];
this.edge4 = edges[3];
// 计算周长和面积
double perimeter = calculatePerimeter();
double area = calculateArea();
// 设置并获取属性
this.perimeter = perimeter;
this.area = area;
}
// 验证边是否能形成正方形或矩形
private boolean isValidEdges(Edge[] edges) {
// ...实现判断条件
}
// 计算周长和面积的方法省略...
// 测试长方形对象
public void printRectangleInfo() {
System.out.println("Rectangle with perimeter " + perimeter + " and area " + area);
}
}
```
4. **测试对象实例**:
```java
public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(3, 4);
Edge e = new Edge(p1, p2);
e.printEdgeInfo();
Rectangle r = new Rectangle(new Edge[]{e, new Edge(p2, p1), new Edge(p1, new Point(4, 0)), new Edge(p2, new Point(0, 4))});
r.printRectangleInfo();
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)