设计一个名为rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getarea( )的函数返回矩形的面积;一个名为getperimeter( )的函数返回矩形的周长。请实现这个类。编写一个测试程序,创建一个rectangle对象,从键盘输入矩形的宽和高,然后输出矩形的面积和周长。
时间: 2023-05-31 08:18:52 浏览: 267
### 回答1:
class Rectangle:
def __init__(self, width=1, height=1):
self.width = width
self.height = height
def getarea(self):
return self.width * self.height
def getperimeter(self):
return 2 * (self.width + self.height)
width = float(input("请输入矩形的宽:"))
height = float(input("请输入矩形的高:"))
rect = Rectangle(width, height)
print("矩形的面积为:", rect.getarea())
print("矩形的周长为:", rect.getperimeter())
### 回答2:
设计一个名为Rectangle的矩形类,这个类包括两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea()的函数返回矩形的面积;一个名为getPerimeter()的函数返回矩形的周长。
具体实现如下:
```java
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 this.width * this.height;
}
public double getPerimeter() {
return 2 * (this.width + this.height);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入矩形的宽:");
double width = scanner.nextDouble();
System.out.print("请输入矩形的高:");
double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle(width, height);
System.out.println("矩形的面积为:" + rectangle.getArea());
System.out.println("矩形的周长为:" + rectangle.getPerimeter());
}
}
```
测试程序会提示用户输入矩形的宽和高,然后创建一个Rectangle对象,并且通过调用Rectangle类中的getArea()和getPerimeter()方法来计算矩形的面积和周长。最后输出计算结果。
设计类是面向对象编程的基础,一个好的设计能让代码易读、易扩展、易维护。在编写测试程序时,我们通过Scanner类读取用户从命令行输入的数据,并将输入的宽和高作为参数传递给Rectangle类的构造函数来创建一个Rectangle对象。这样,我们能够方便地使用Rectangle类中定义的getArea()和getPerimeter()方法来计算矩形的面积和周长,并将结果输出到控制台中。
### 回答3:
设计一个名为rectangle的矩形类,包括两个名为width和height的double数据域,它们分别表示矩形的宽和高,width和height的默认值都为1。实现以下代码:
```java
public class Rectangle {
double width = 1;
double height = 1;
// 无参构造函数
public Rectangle() {
}
// 宽和高为指定值的矩形构造函数
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);
}
}
```
接下来,编写一个测试程序,创建一个rectangle对象,从键盘输入矩形的宽和高,然后输出矩形的面积和周长。代码如下:
```java
import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入矩形的宽:");
double width = input.nextDouble();
System.out.print("请输入矩形的高:");
double height = input.nextDouble();
Rectangle rect = new Rectangle(width, height);
System.out.println("矩形的面积为:" + rect.getArea());
System.out.println("矩形的周长为:" + rect.getPerimeter());
}
}
```
运行程序,输入矩形的宽和高,输出矩形的面积和周长。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)