请帮忙用JAVA程序编写!在线等。急求马上解决!十万火急!!!!!
编写一个类Rect(矩形类),定义两个私有属性w(表示宽)、h(表示高),定义一个带参数的构造函数对宽和高进行初始化,定义一个公有成员函数doublegetArea(),...
编写一个类Rect(矩形类),定义两个私有属性w(表示宽)、h(表示高),定义一个带参数的构造函数对宽和高进行初始化,定义一个公有成员函数double getArea( ),用于计算矩形的面积,定义一个公有成员函数void display( ),用以显示该矩形的面积。在main( )函数中键盘输入宽和高的值,创建矩形对象,并显示该矩形的面积。
展开
1个回答
展开全部
/**
* 矩形类
*/
class Rect {
private double width = 0;
private double height = 0;
public Rect(double width , double height) {
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public void display(){
System.out.println("矩形面积为:" + getArea());
}
}
====================================================================
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();
new Rect(width, height).display();
}
* 矩形类
*/
class Rect {
private double width = 0;
private double height = 0;
public Rect(double width , double height) {
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public void display(){
System.out.println("矩形面积为:" + getArea());
}
}
====================================================================
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();
new Rect(width, height).display();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询