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

 

你可能感兴趣的:(Java)