牛客编程初学者入门训练——KiKi设计类继承java

题目链接

题目描述

在这里插入图片描述

import java.util.Scanner;

abstract class shape {
    private int x;
    private int y;
    abstract double GerArea();
}
class Rectangle extends shape{
    int a;
    int b;
    Rectangle()
    {

    }
    Rectangle(int a,int b)
    {
        this.a=a;
        this.b=b;
    }
    double GerArea(){
        return a*b;
    }
}

class Circle extends shape{
    double r;
    Circle(double r){
        this.r=r;
    }
    double GerArea(){
        return 3.14*r*r;
    }
}

//调用父类构造方法之前 先调用父类构造方法,没有指明,则先调用父类的无参构造方法
//如果父类没有无参构造方法.则会报错
class Square extends Rectangle{
    int c;
    Square(int c){
        this.c=c;
    }
    double GerArea(){
        return c*c;
    }
}

public class Main{
    public static void main(String args[]){
        
        int a,b,c,d;
        Scanner a1=new Scanner(System.in);
        a=a1.nextInt();
        b=a1.nextInt();
        Rectangle rect=new Rectangle(a,b);
       
        c=a1.nextInt();
        Circle cir=new  Circle(c);
        
        d=a1.nextInt();
        Square squ=new Square(d);
        a1.close();
        double s1=rect.GerArea()-(int)rect.GerArea();
         if(s1==0.0)
            System.out.println((int)rect.GerArea());
        else 
            System.out.println(rect.GerArea());
        double s2=cir.GerArea()-(int)cir.GerArea();
        if(s2==0.0)
            System.out.println((int)cir.GerArea());
        else if(s2==0.5)//有一种错误情况是本人程序输出的78.50,标准是78.5 
            System.out.println((int)cir.GerArea()+0.5);
        else 
            System.out.printf("%.2f\n",cir.GerArea());
        
         double s3=squ.GerArea()-(int)squ.GerArea();
        if(s3==0.0)
            System.out.println((int)squ.GerArea());
        else 
            System.out.println(squ.GerArea());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值