前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >长方形长方体类import java.util.Scanner

长方形长方体类import java.util.Scanner

作者头像
用户7886150
修改2020-12-07 11:50:26
修改2020-12-07 11:50:26
5670
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: 了解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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档