JAVA定义一个 Retangle类,有长、宽等属性,定义构造方法用来初始化类的这些属性,定义方法输出Retangle的长宽和面积。编写应用程序使用Retangle。 【输入形式】 Please input length: Please input width: 【输出形式】 The lengh is: The width is: The area is: 【输入样例】 4.0 3.0 【输出样例】 The lengh is:4.0 The width is:3.0 The area is:12.0
时间: 2024-02-20 13:57:56 浏览: 81
设计一个矩形类Rectangle(Java)
以下是JAVA代码实现:
```
import java.util.Scanner;
public class Retangle {
private double length;
private double width;
public Retangle(double length, double width) {
this.length = length;
this.width = width;
}
public void print() {
System.out.println("The lengh is:" + length);
System.out.println("The width is:" + width);
System.out.println("The area is:" + length * width);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please input length:");
double length = sc.nextDouble();
System.out.println("Please input width:");
double width = sc.nextDouble();
Retangle r = new Retangle(length, width);
r.print();
}
}
```
运行结果:
```
Please input length:
4.0
Please input width:
3.0
The lengh is:4.0
The width is:3.0
The area is:12.0
```
阅读全文