【PTA】7-11 Abstract and Shape

该代码示例展示了如何在Java中创建一个名为Shape的抽象类,包含抽象方法getArea(),以及两个继承自Shape的子类Circle和Rectangle。Circle类计算圆的面积,Rectangle类计算矩形的面积。主程序读取用户输入的半径、高度和宽度,创建Shape对象数组,分别存储Circle和Rectangle实例,并遍历数组调用getArea()方法打印面积。
摘要由CSDN通过智能技术生成

7-11 Abstract and Shape(分数 5)
我们有抽象类Shape,它有抽象方法getArea,类Circle和Rectangle将从Shape继承,请阅读并完成如下代码:
题意:抽象类Shape含有抽象方法getArea,Circle类与Rectangle类为Shape子类,补全如下代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int radius = input.nextInt();
        int height = input.nextInt();
        int width = input.nextInt();
        //to create an Shape Array whose length is 2
        Shape[] shapes =【】;
        //shapes[0] refer to a Circle object
        shapes[0] =【】;
        //shapes[1]  refer to a Rectangle object
        shapes[1] = 【】;
        //call getArea method with shapes
        for(int i =0;i<shapes.length;i++)
             System.out.printf("the area of shape :%.2f\n",【】);
    }

}
//to declare abstract class
【】 Shape{
 //to declare abstract method  double getArea();
 【】


}
class Circle 【】{
    private int radius;
    //override method getArea
【】
    public Circle(int radius) {
        super();
        this.radius = radius;
    }
}
class Rectangle 【】{
    private int height,width;
    
    //override method getArea
【】


    public Rectangle(int height, int width) {
        super();
        this.height = height;
        this.width = width;
    }
}



请在这里写题目描述。例如:本题目要求读入2个整数A和B,然后输出它们的和。

输入格式:

input radius , height and width at a line.

输出格式:

output the area of circle and rectangle object at different lines.

输入样例:

在这里给出一组输入。例如:

4 5 6

输出样例:

在这里给出相应的输出。例如:

the area of shape :50.27
the area of shape :30.00

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int radius = input.nextInt();
        int height = input.nextInt();
        int width = input.nextInt();
        //定义Shape类数组长度为2
        Shape[] shapes = new Shape[2];
        //shapes[0]为Circle类
        shapes[0] = new Circle(radius);
        //shapes[1]为Rectangle类
        shapes[1] = new Rectangle(height,width);
        //遍历数组调用getArea方法
        for(int i =0;i<shapes.length;i++)
             System.out.printf("the area of shape :%.2f\n",shapes[i].getArea());
    }

}
//定义抽象类
abstract class Shape{
//定义抽象方法
    abstract public double getArea();
}
class Circle extends Shape {
    private int radius;
    //重写getArea方法
    public double getArea() {
        return Math.PI*radius*radius;
    }
    public Circle(int radius) {
        super();
        this.radius = radius;
    }
}
class Rectangle extends Shape {
    private int height,width;
    
    //重写getArea方法
    public double getArea() {
        return height*width;
    }
    public Rectangle(int height, int width) {
        super();
        this.height = height;
        this.width = width;
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值