定义一个名为Rectangle的类来表示矩形,这个类包括: (1)两个名为width和height的double类型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。 (2)一个用于创建默认矩形的无参构造方法。 (3)一个创建指定width和height值的矩形的构造方法。 (4)一个名为getArea()的方法,返回该矩形的面积。 (5)一个名为getPerimeter()的方法,返回周长。
时间: 2024-03-16 10:44:00 浏览: 20
好的,以下是你所描述的Rectangle类的代码实现:
```
public class Rectangle {
private double width;
private double height;
public Rectangle() {
this.width = 1;
this.height = 1;
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
```
这个类包含两个私有数据域,即width和height,以及两个构造方法和两个公有方法。其中,无参构造方法用于创建默认矩形,而有参构造方法用于创建指定width和height值的矩形。getArea()方法用于计算该矩形的面积,getPerimeter()方法用于计算周长。
相关问题
1.【矩形类Rectangle】 1)题目要求:设计一个名为Rectangle的类表示矩形。这个类包括: 两个名为width和height的double型private成员变量,它们分别表示矩形的宽和高。width和height的默认值都为1。
一个无参构造方法,创建一个默认宽度和高度都为1的矩形。
一个有参构造方法,接受两个double参数分别表示矩形的宽和高。
一个名为getArea的公共方法,返回矩形的面积。
一个名为getPerimeter的公共方法,返回矩形的周长。
一个名为setWidth的公共方法,接受一个double参数,用于设置矩形的宽度。
一个名为setHeight的公共方法,接受一个double参数,用于设置矩形的高度。
2)示例代码:
public class Rectangle {
private double width;
private double height;
public Rectangle() {
width = 1;
height = 1;
}
public Rectangle(double w, double h) {
width = w;
height = h;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public void setWidth(double w) {
width = w;
}
public void setHeight(double h) {
height = h;
}
}
设计一个名为Rectangle 的类表示矩形。这个类包括: • 两个名为width 和height 的double 型数据域,它们分别表示矩形的宽和高。width 和height 的默认值都为1。还有一个名为number的静态变量。这些数据域均定义为pri
vate。
• 一个无参构造方法,创建一个默认宽和高均为1的矩形。
• 一个带参构造方法,创建一个指定宽和高的矩形。
• 两个访问器方法getWidth 和getHeight,分别返回矩形的宽和高。
• 一个名为getArea 的方法返回矩形的面积。
• 一个名为getPerimeter 的方法返回矩形的周长。
• 一个名为increaseNumber 的静态方法,每次被调用时number的值加1。
下面是这个类的代码实现:
```java
public class Rectangle {
private double width;
private double height;
private static int number = 0;
public Rectangle() {
this.width = 1;
this.height = 1;
increaseNumber();
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
increaseNumber();
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public static void increaseNumber() {
number++;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)