123 - 长方形长方体类import java.util.Scanner; public class Main{ public static void main(String[] args)

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;
	}
	
}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值