7-37 图形卡片排序游戏 (40 分)

33 篇文章 4 订阅
28 篇文章 1 订阅

掌握类的继承、多态性使用方法以及接口的应用。

输入格式:
首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format。
如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
排序后的各图形类型及面积,格式同排序前的输出;
所有图形的面积总和,格式为Sum of area:总面积值。
输入样例1:
在这里给出一组输入。例如:

1 5 3 2 0

输出样例1:
在这里给出相应的输出。例如:
Wrong Format
输入样例2:
在这里给出一组输入。例如:

4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5

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

The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91
输入样例3:
在这里给出一组输入。例如:

4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4

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

Wrong Format

import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<Integer>();
        int num = input.nextInt();
        while(num != 0){
            if(num < 0 || num > 4){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            list.add(num);
            num = input.nextInt();
        }
        DealCardList dealCardList = new DealCardList(list);
        if(!dealCardList.validate()){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
    }
}

class DealCardList{
    ArrayList<Card> cardList=new ArrayList<>();

    public DealCardList() {
    }

    public DealCardList(ArrayList<Integer> card) {
        for (Integer integer : card) {
            if (integer==0)break;
            switch (integer){
                case 1:
                    Card card1=new Card(new Circle(Main.input.nextDouble()));
                    card1.getShape().setShapeName("Circle");
                    cardList.add(card1);
                    break;
                case 2:
                    Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
                    card2.getShape().setShapeName("Rectangle");
                    cardList.add(card2);
                    break;
                case 3:
                    Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card3.getShape().setShapeName("Triangle");
                    cardList.add(card3);
                    break;
                case 4:
                    Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card4.getShape().setShapeName("Trapezoid");
                    cardList.add(card4);
                    break;
            }

        }
    }
    public boolean validate(){
        boolean ret=true;
        for (Card card : cardList) {
            if (!card.getShape().validate()){
                ret=false;
                break;
            }
        }
        return ret;
    }
    public void cardSort(){
        TreeSet<Card> cards = new TreeSet<>(cardList);
        for (Card card : cards) {
            System.out.print(card.getShape());
        }
    }
    public double getAllArea(){
        double sum=0;
        for (Card card : cardList) {
            sum+=card.getShape().getArea();
        }
        return sum;
    }
    public void showResult(){
        System.out.println("The original list:");
        for (Card card : cardList) {
            System.out.print(card.getShape());
        }
        System.out.println();
        System.out.println("The sorted list:");
        cardSort();
        System.out.println();
        System.out.printf("Sum of area:%.2f\n",getAllArea());
    }
}
class Card implements Comparable<Card>{
    private Shape shape;

    public Card() {
    }

    public Card(Shape shape) {
        this.shape = shape;
    }

    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    @Override
    public int compareTo(Card card) {
        return -(int)(shape.getArea()-card.getShape().getArea());
    }
}

abstract class Shape{
    private String shapeName;

    public Shape() {
    }

    public Shape(String shapeName) {
        this.shapeName = shapeName;
    }

    public String getShapeName() {
        return shapeName;
    }

    public void setShapeName(String shapeName) {
        this.shapeName = shapeName;
    }
    public abstract double getArea();
    public abstract boolean validate();

    /**
     * 重写了toString的方法
     * 利用Shape类的shapeName变量来表示相应的图形,
     * 而不是用getclass方法得到类名
     * @return
     */
    @Override
    public String toString() {
        return getShapeName()+":"+String.format("%.2f ",getArea());
    }
}

class Circle extends Shape{
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI*radius*radius;
    }

    @Override
    public boolean validate() {
        return this.radius>0;
    }
}

class Rectangle extends Shape{
    private double width,height;

    public Rectangle() {
    }

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

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double getArea() {
        return height*width;
    }

    @Override
    public boolean validate() {
        return width>0&&height>0;
    }
}

class Triangle extends Shape{
    private double side1,side2,side3;

    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    @Override
    public double getArea() {
        double p=(side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }

    @Override
    public boolean validate() {
        boolean ret=true;
        if (!(side1>0&&side3>0&&side2>0))ret=false;
        else{
            if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1))ret=false;
        }
        return ret;
    }
}

class Trapezoid extends Shape{
    private double topSide,bottomSide,height;

    public Trapezoid() {
    }

    public Trapezoid(double topSide, double bottomSide, double height) {
        this.topSide = topSide;
        this.bottomSide = bottomSide;
        this.height = height;
    }

    @Override
    public double getArea() {
        return (topSide+bottomSide)*height/2;
    }

    @Override
    public boolean validate() {
        return topSide>0&&height>0&&bottomSide>0;
    }
}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值