KiKi设计类的继承

题目描述

KiKi理解了继承可以让代码重用,他现在定义一个基类shape,私有数据为坐标点x,y,  由它派生Rectangle类和Circle类,它们都有成员函数GetArea()求面积。派生类Rectangle类有数据:矩形的长和宽;派生类Circle类有数据:圆的半径。Rectangle类又派生正方形Square类,定义各类并测试。输入三组数据,分别是矩形的长和宽、圆的半径、正方形的边长,输出三组数据,分别是矩形、圆、正方形的面积。圆周率按3.14计算。

输入描述:

 

输入三行,

第一行为矩形的长和宽,

第二行为圆的半径,

第三行为正方形的边长。

输出描述:

三行,分别是矩形、圆、正方形的面积。

示例1

输入

7 8
10
5

输出

56
314
25

 

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
            
            Rectangle rectangle=new Rectangle(cin.nextInt(),cin.nextInt());
            Circle circle=new Circle(cin.nextDouble());
            Square square=new Square(cin.nextInt());
            System.out.println(rectangle.GetArea());
            double rad=circle.GetArea();
            double k = rad-(int)rad;
            if (k == 0.0)
                System.out.println((int)rad);
            else if(k==0.5)
                System.out.println((int)rad+0.5);
            else
                System.out.printf("%.2f\n",rad);
            //System.out.println(circle.GetArea());
            System.out.println(square.GetArea());
            
}
        cin.close();
}
}
    class Shape{
        private int x;
        private int y;
}
    class Rectangle extends Shape{
        int length;
        int width;
        public Rectangle(int length,int width){
            this.length=length;
            this.width=width;
        }
        public int GetArea(){
            return length*width;
}
        
}
class Circle extends Shape{
    double radius;
    public Circle(double radius){
        this.radius=radius;
}
    public double GetArea(){
        return 3.14*radius*radius;
}
}
class Square extends Rectangle{
    public Square(int width){
       super(width,width);
}
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值