PTA -- 题附代码(形状继承) (图形继承与多态)

这篇博客介绍了如何使用Java实现图形的继承和多态,包括抽象类Shape、Rectangle和Circle,以及sumAllArea和sumAllPerimeter方法的实现。通过实例展示了如何处理输入输出,以及如何在继承体系中组织方法。

前言

  • 时间:2020.5.13
  • 内容:PTA练习题
  1. 7-2 jmu-Java-03面向对象基础-04-形状-继承 (30分)
  2. 7-1 图形继承与多态 (55分)
  • 备注:7-1是7-2的进阶。回学校做题问同学问题好方便呢,其实大家一开始做这些题目遇上的问题都大同小异,别人走过的弯路错路,一点拨自己就能发现了,这两篇老师说相对有点难,但期末不考,也还是认真写了呢。

7-1 计算年龄 (10分)

介绍

前面题目形状中我们看到,为了输出所有形状的周长与面积,需要建立多个数组进行多次循环。这次试验使用继承与多态来改进我们的设计。

本题描述

  1. 定义抽象类Shape
    属性:不可变静态常量double PI,值为3.14,
    抽象方法:public double getPerimeter(),public double getArea()
  2. Rectangle与Circle类均继承自Shape类。
    Rectangle类(属性:int width,length)、Circle类(属性:int radius)。
    带参构造函数为Rectangle(int width,int length),Circle(int radius)。
    toString方法(Eclipse自动生成)
  3. 编写double sumAllArea方法计算并返回传入的形状数组中所有对象的面积和与
    double sumAllPerimeter方法计算并返回传入的形状数组中所有对象的周长和。
  4. main方法
    4.1 输入整型值n,然后建立n个不同的形状。如果输入rect,则依次输入宽、长。如果输入cir,则输入半径。
    4.2 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。 提示:使用Arrays.toString。
    4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass() //获得类型, shape.getClass().getSuperclass() //获得父类型;
    注意:处理输入的时候使用混合使用nextInt与nextLine需注意行尾回车换行问题。

思考

  1. 你觉得sumAllArea和sumAllPerimeter方法放在哪个类中更合适?
  2. 是否应该声明为static?

输入样例

4
rect
3 1
rect
1 5
cir
1
cir
2

输出样例

38.84
23.700000000000003
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape

代码

import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        Shape[] shape = new Shape[n];
        double sumPeri = 0;
        double sumArea = 0;
        for (int i = 0; i < n; i++) {
            String name = scan.next();
            if(name.equals("rect")){
                int width = scan.nextInt();
                int lenth = scan.nextInt();
                shape[i] = new Rectangle(width,lenth);
            }
            else if(name.equals("cir")){
                int r = scan.nextInt();
                shape[i] = new Circle(r);
            }
            sumPeri += shape[i].getPerimeter();
            sumArea += shape[i].getArea();
        }
        System.out.println(sumPeri + "\n" + sumArea);
        System.out.println(Arrays.toString(shape));
        for (int i = 0; i < n; i++) {
            System.out.println(shape[i].getClass() + "," + shape[i].getClass().getSuperclass());
        }
    }
}
abstract class Shape {
    final double PI = 3.14;
    public abstract double getPerimeter();
    public abstract double getArea();
}
class Rectangle extends Shape{
    int width;
    int length;
    Rectangle(int width,int length){
        this.width = width;
        this.length = length;
    }
    @Override
    public String toString() {
        return "Rectangle [" +
                "width=" + width +
                ", length=" + length +
                ']';
    }
    @Override
    public double getPerimeter() {
        return 2 * (width + length);
    }
    @Override
    public double getArea() {
        return width * length;
    }
}
class Circle extends Shape{
    int radius;
    Circle(int radius){
        this.radius = radius;
    }
    @Override
    public String toString() {
        return "Circle [" +
                "radius=" + radius +
                ']';
    }
    @Override
    public double getPerimeter() {
        return 2 * PI * radius;
    }
    @Override
    public double getArea() {
        return PI * radius *radius;
    }
}

7-1 图形继承与多态 (55分)

介绍

掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。 2020-OO第06次作业-1指导书V1.0.pdf

输入格式

从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

输出格式

  1. 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format。
  2. 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
  • 各个图形的面积;
  • 所有图形的面积总和;
  • 排序后的各个图形面积;
  • 再次所有图形的面积总和。

输入样例1

1 1 1 2.3 3.2 3.2 6.5 3.2 4.2

输出样例1

Original area:
16.62 10.24 5.68 
Sum of area:32.54
Sorted area:
5.68 10.24 16.62 
Sum of area:32.54

输入样例2

0 2 2 2.3 2.5 56.4 86.5 64.3 85.6 74.6544 3.2 6.1 4.5

输出样例2

Original area:
5.75 4878.60 2325.19 7.00 
Sum of area:7216.54
Sorted area:
5.75 7.00 2325.19 4878.60 
Sum of area:7216.54

输入样例3

0 0 1 3 3 6

输出样例3

Wrong Format

代码

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean key = true;
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        if(a < 0 || b < 0 || c<0) {
            key = false;
            System.out.println("Wrong Format");
        } else {
            Shape[] shapes = new Shape[a + b + c];
            double[] areas = new double[a + b + c];
            double sumArea = 0;
            for (int i = 0; i < a; i++) {
                double cirRadius = scan.nextDouble();
                Circle circle = new Circle(cirRadius);
                if (circle.validate()) {
                    shapes[i] = circle;
                    areas[i] = circle.getArea();
                    sumArea += areas[i];
                } else {
                    key = false;
                }
            }
            for (int i = a; i < a + b; i++) {
                double recWidth = scan.nextDouble();
                double recLength = scan.nextDouble();
                Rectangle rectangle = new Rectangle(recWidth, recLength);
                if (rectangle.validate()) {
                    shapes[i] = rectangle;
                    areas[i] = rectangle.getArea();
                    sumArea += areas[i];
                } else {
                    key = false;
                }
            }
            for (int i = a + b; i < a + b + c; i++) {
                double triLength1 = scan.nextDouble();
                double triLength2 = scan.nextDouble();
                double triLength3 = scan.nextDouble();
                Triangle triangle = new Triangle(triLength1,triLength2,triLength3);
                if(triangle.validate()){
                    shapes[i] = triangle;
                    areas[i] = triangle.getArea();
                    sumArea += areas[i];
                } else {
                    key = false;
                }
            }
            if (!key) {
                System.out.println("Wrong Format");
            } else {
                //1
                System.out.println("Original area:");
                for (int i = 0; i < a+b+c; i++) {
                    System.out.print(String.format("%.2f", areas[i]));
                    if(i == a+b+c)
                        break;
                    System.out.print(" ");
                }
                System.out.println();
                //2
                System.out.println("Sum of area:" + String.format("%.2f",sumArea));
                //3
                System.out.println("Sorted area:");
                Arrays.sort(areas);
                for (int i = 0; i < a+b+c; i++) {
                    System.out.print(String.format("%.2f", areas[i]));
                    if(i == a+b+c)
                        break;
                    System.out.print(" ");
                }
                System.out.println();
                //4
                System.out.println("Sum of area:" + String.format("%.2f",sumArea));
            }
        }
    }
}
abstract class Shape {
    public abstract double getArea();
    public abstract boolean validate();
    public abstract String toString();
}
class Circle extends Shape{
    double radius;
    Circle(double radius){
        this.radius = radius;
    }
    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
    @Override
    public boolean validate() {
        if(radius > 0) {
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return null;
    }
}
class Rectangle extends Shape {
    double width;
    double length;
    Rectangle(double width,double length){
        this.width = width;
        this.length = length;
    }
    @Override
    public double getArea() {
        return width * length;
    }
    @Override
    public boolean validate() {
        if(width > 0 && length >0) {
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return null;
    }
}
class Triangle extends Shape {
    double side1;
    double side2;
    double side3;
    Triangle(double side1,double side2,double side3){
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
    @Override
    public double getArea() {
        //(海伦公式)(p=(a+b+c)/2)
        //S=sqrt[p(p-a)(p-b)(p-c)]
        //=sqrt[(1/16)(a+b+c)(a+b-c)(a+c-b)(b+c-a)]
        //=1/4sqrt[(a+b+c)(a+b-c)(a+c-b)(b+c-a)]
        double p = (side1 + side2 + side3) / 2;
        return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
    }
    @Override
    public boolean validate() {
        if(side1 > 0 && side2 >0 && side3 >0 && (side1 + side2) > side3 && (side1 + side3) > side2 && (side2 + side3) > side1 && Math.abs(side1 - side2) < side3 && Math.abs(side1 - side2) < side3 && Math.abs(side2 - side3) < side1) {
            return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值