参考链接: 了解Java中“ public static void main”中的“ static”
123 - 长方形长方体类
Time Limit: 1000 Memory Limit: 65535 Submit: 288 Solved: 118
Description
定义一个长方形类Rectangle,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类长方体类,拥有长、宽、高属性,提供构造函数,getPerimeter函数计算所有边的周长,getArea函数计算表面积,新增getVolume函数,计算体积
在main函数中,分别构造长方形类和长方体类的对象,并输出他们的周长、面积、体积,保留两位小数
Input
长方形类的长、宽
长方体类的长、宽、高
Output
长方形的周长和面积
长方体的周长,表面积,体积
Sample Input
1 2
1 2 3
Sample Output
6.00 2.00
24.00 22.00 6.00
HINT
Pre Append Code
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
double wide = scan.nextDouble();
Rectangle r = new Rectangle(length,wide);
System.out.printf("%.2f ",r.getPerimeter());
System.out.printf("%.2f",r.getArea());
System.out.println();
length = scan.nextDouble();
wide = scan.nextDouble();
double height = scan.nextDouble();
Cuboid c = new Cuboid (length, wide, height);
System.out.printf("%.2f ",c.getPerimeter());
System.out.printf("%.2f ",c.getArea());
System.out.printf("%.2f",c.getVolume());
scan.close();
}
}
class Rectangle
{
double length;
double width;
Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public double getPerimeter()
{
return 2*(length+width);
}
public double getArea()
{
return length*width;
}
}
class Cuboid extends Rectangle
{
double height;
Cuboid(double length, double width,double height)
{
super(length, width);
this.height = height;
}
public double getPerimeter()
{
return 4*(length+width+height);
}
public double getArea()
{
return 2*(length * width + length * height + height * width);
}
public double getVolume()
{
return length * height * width;
}
}
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有