用java 定义Point类描述屏幕上的一个点。该 类包括点的x和y两个坐标,及对点进行的相 关操作,如:取得点的坐标值,对点的坐标进 行赋值。此类属于MyPackageTwoD包。此包 中另一个Rectangle类可以实现矩形的基本操 作,包含Point. width height等了个变量 及构造方法和其他方法。文件 TestPackage.java中包含maino方法,定义如 T:
时间: 2024-01-22 19:18:22 浏览: 104
下面是定义Point类的代码:
```java
package MyPackageTwoD;
public 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 int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
```
下面是定义Rectangle类的代码:
```java
package MyPackageTwoD;
public class Rectangle {
private Point point;
private int width;
private int height;
public Rectangle(Point point, int width, int height) {
this.point = point;
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getArea() {
return width * height;
}
}
```
下面是TestPackage类的代码:
```java
import MyPackageTwoD.Point;
import MyPackageTwoD.Rectangle;
public class TestPackage {
public static void main(String[] args) {
Point point = new Point(5, 10);
Rectangle rectangle = new Rectangle(point, 20, 30);
System.out.println("Point coordinates: (" + point.getX() + ", " + point.getY() + ")");
System.out.println("Rectangle dimensions: " + rectangle.getWidth() + " x " + rectangle.getHeight());
System.out.println("Rectangle area: " + rectangle.getArea());
point.setX(15);
point.setY(20);
System.out.println("Point coordinates after modification: (" + point.getX() + ", " + point.getY() + ")");
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)