7-2 graphic card group game (60 points)

**

7-2 graphic card group game (60 points)

**
Master the inheritance of classes, the use of polymorphism and the application of interfaces. Refer to the operation instruction for specific requirements 2021-OO operation-2 instruction for the 7th time v1 0.pdf

Input format:

Enter a string of numbers (1 ~ 4, integer) on one line, where 1 represents circular card, 2 represents rectangular card, 3 represents triangular card and 4 represents trapezoidal card. Numbers are separated by one or more spaces and end with "0". For example: 1 3 4 2 1 3 4 2 1 3 0
According to the card pattern type represented by the number in the first line, input the relevant parameters of each pattern in turn. For example, for circular card, you need to input the radius of the circle, for rectangular card, you need to input the width and length of the rectangle, for triangular card, you need to input the length of three sides of the triangle, and for trapezoid, you need to input the upper bottom, lower bottom and height of the trapezoid. The data is separated by one or more spaces.

Output format:

If the number of graphics is illegal (< = 0) or the value of graphics attribute is illegal (the value is < 0 and the three sides of a triangle cannot form a triangle), Wrong Format is output.
If the input is legal, it will be output normally, and all values can be kept to two decimal places after calculation. The output contents are as follows:
The format of each graphic type and area before sorting is [graphic name 1: area value 1, graphic name 2: area value 2... Graphic name n: area value n]. Note that each graphic output is separated by a space, and there is a space for separation at the end of the output, before the closing character "];
Output the graphic type and area after grouping. The format is [graphic type and area of circular grouping] [graphic type and area of rectangular grouping] [graphic type and area of triangular grouping] [graphic type and area of trapezoidal grouping]. The format in each group is graphic Name: area value. Output in the order of "Circle, Rectangle, Triangle and Trapezoid";
The type and area of each graph after sorting in each group are the same as the output of each group before sorting;
Output the maximum value of the sum of areas in each group in the format of The max area: area value.

shopping spree
1. Further improvement and perfection must be made on the basis of operation 7-1.
2. Consider the "single responsibility principle" of object-oriented design and whether the program can comply with the "open close" principle.

Input example 1:

1 5 3 2 0

Output example 1:

Wrong Format

Input example 2:

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

Output example 2:

The original list: [Trapezoid:1.14 Rectangle:3.22 Circle:98.52
Triangle:4.02 ] The Separated List: [Circle:98.52 ][Rectangle:3.22
][Triangle:4.02 ][Trapezoid:1.14 ] The Separated sorted List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ] The
max area:98.52

Input example 3:

2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65

Output example 3:

The original list: [Rectangle:8.05 Circle:19.63 Rectangle:9.45
Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80
Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25
Circle:120.81 Rectangle:16.00 Circle:1092.72 ] The Separated List:
[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25
Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45
Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49
Trapezoid:175.56 ] The Separated sorted List: [Circle:1092.72
Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63
Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05
][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
The max area:1601.31

Input example 4:

1 1 3 0
6.5 12.54 3.6 5.3 6.4

Output example 4:

The original list: [Circle:132.73 Circle:494.02 Triangle:9.54 ] The
Separated List: [Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][] The
Separated sorted List: [Circle:494.02 Circle:132.73 ][][Triangle:9.54
][] The max area:626.75

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

//Chulishuju class
class Chulishuju{
    ArrayList<Card> cardList=new ArrayList<>();
    double[] zs=new double[4];

    public Chulishuju(ArrayList<Integer> c){
        for (Integer a:c){
            if (a==0){
                break;
            }
            else {
                switch (a) {
                    case 1 :
                        Card a1 = new Card(new Circle(Main.x.nextDouble()));
                        cardList.add(a1);
                        a1.getShape().setName("Circle");
                        break;
                    case 2 :
                        Card b1 = new Card(new Rectangle(Main.x.nextDouble(), Main.x.nextDouble()));
                        cardList.add(b1);
                        b1.getShape().setName("Rectangle");
                        break;
                    case 3 :
                        Card c1 = new Card(new Triangle(Main.x.nextDouble(), Main.x.nextDouble(), Main.x.nextDouble()));
                        cardList.add(c1);
                        c1.getShape().setName("Triangle");
                        break;
                    case 4 :
                        Card d1 = new Card(new Trapezoid(Main.x.nextDouble(), Main.x.nextDouble(), Main.x.nextDouble()));
                        cardList.add(d1);
                        d1.getShape().setName("Trapezoid");
                        break;
                }
            }
        }
    }
    //Judge whether the data is legal
    public boolean pdsfhf(){
        boolean kp=true;
        for (Card a:cardList){
            if (!a.getShape().pdsfhf()){
                kp=false;
                break;
            }
        }
        return kp;
    }
    //Output the data of the circle and calculate the total area
    public void yuan(){
        zs[0]=0;
        System.out.print("[");
        for (Card a:cardList){
            if(a.getShape().getName().equals("Circle")){
                System.out.print(a.getShape());
                zs[0]=zs[0]+a.getShape().Mj();
            }
        }
        System.out.print("]");
    }
    //Output rectangular data and calculate the total area
    public void juxing(){
        zs[1]=0;
        System.out.print("[");
        for (Card a:cardList){
            if(a.getShape().getName().equals("Rectangle")){
                System.out.print(a.getShape());
                zs[1]=zs[1]+a.getShape().Mj();
            }
        }
        System.out.print("]");
    }
    //Output triangle data and calculate the total area
    public void sanjiaoxing(){
        zs[2]=0;
        System.out.print("[");
        for (Card a:cardList){
            if(a.getShape().getName().equals("Triangle")){
                System.out.print(a.getShape());
                zs[2]=zs[2]+a.getShape().Mj();
            }
        }
        System.out.print("]");
    }
    //Output the data of trapezoid and calculate the total area
    public void tixing(){
        zs[3]=0;
        System.out.print("[");
        for (Card a:cardList){
            if(a.getShape().getName().equals("Trapezoid")){
                System.out.print(a.getShape());
                zs[3]=zs[3]+a.getShape().Mj();
            }
        }
        System.out.print("]");
    }
    //Output circle data from large to small
    public void ypx(){
        TreeSet<Card> kp = new TreeSet<>(cardList);
        System.out.print("[");
        for (Card a:kp){
            if(a.getShape().getName().equals("Circle")){
                System.out.print(a.getShape());
            }
        }
        System.out.print("]");
    }
    //Output rectangular data from large to small
    public void jxpx(){
        TreeSet<Card> kp = new TreeSet<>(cardList);
        System.out.print("[");
        for (Card a:kp){
            if(a.getShape().getName().equals("Rectangle")){
                System.out.print(a.getShape());
            }
        }
        System.out.print("]");
    }
    //Output triangle data from large to small
    public void sjxpx(){
        TreeSet<Card> kp = new TreeSet<>(cardList);
        System.out.print("[");
        for (Card a:kp){
            if(a.getShape().getName().equals("Triangle")){
                System.out.print(a.getShape());
            }
        }
        System.out.print("]");
    }
    //Output trapezoidal data from large to small
    public void txpx(){
        TreeSet<Card> kp = new TreeSet<>(cardList);
        System.out.print("[");
        for (Card a:kp){
            if(a.getShape().getName().equals("Trapezoid")){
                System.out.print(a.getShape());
            }
        }
        System.out.print("]");
    }
    //Find the maximum total area
    public double getMax(){
        double max=0;
        int i;
        for (i=0;i<4;i++){
            if(max<zs[i]){
                max=zs[i];
            }
        }
        return max;
    }
    public void show(){//output
        System.out.println("The original list:");
        System.out.print("[");
        for (Card a : cardList) {
            System.out.print(a.getShape());
        }
        System.out.print("]");
        System.out.println("\nThe Separated List:");
        yuan();juxing();sanjiaoxing();tixing();
        System.out.println("\nThe Separated sorted List:");
        ypx();jxpx();sjxpx();txpx();
        System.out.printf("\nThe max area:%.2f\n",getMax());
    }
}
//Card class
class Card implements Comparable<Card>{
    private Shape shape;
    //Create a parameterless construction method
    public Card() {
    }
    //Create a construction method with parameters
    public Card(Shape shape) {
        this.shape = shape;
    }
    //getter
    public Shape getShape() {
        return shape;
    }
    //setter
    public void setShape(Shape shape) {
        this.shape = shape;
    }
    public int compareTo(Card card) {
        return -(int)(shape.Mj()-card.getShape().Mj());
    }
}
//Shape class
abstract class Shape {
    private String name;
    //Create a parameterless construction method
    public Shape() {
    }
    //Create a construction method with parameters
    public Shape(String name) {
        this.name = name;
    }
    //getter
    public String getName() {
        return name;
    }
    //setter
    public void setName(String name) {
        this.name = name;
    }
    //the measure of area
    public abstract double Mj();
    //Judge whether the data is legal
    public abstract boolean pdsfhf();

    public String toString() {
        return getName()+":"+String.format("%.2f ",Mj());
    }
}
//Circle class
class Circle extends Shape{
    private double r;
    //Create a parameterless construction method
    public Circle(){
    }
    //Create a construction method with parameters
    public Circle(double r){
        this.r=r;
    }
    //getter
    public double getR(){
        return r;
    }
    //setter
    public void setR(double r){
        this.r = r;
    }
    //Judge whether the data is legal
    public boolean pdsfhf() {
        return r>0;
    }
    //Calculate circle area
    public double Mj(){
        double mj=Math.PI*r*r;
        return mj;
    }
}
//Rectangle class
class Rectangle extends Shape{
    private double a,b;
    //Create a parameterless construction method
    public Rectangle(){
    }
    //Create a construction method with parameters
    public Rectangle(double a, double b){
        this.a=a;
        this.b=b;
    }
    //getter
    public double A(){
        return a;
    }
    public double B(){
        return b;
    }
    //setter
    public void setA(double a){
        this.a=a;
    }
    public void setB(double b){
        this.b=b;
    }
    //Judge whether the data is legal
    public boolean pdsfhf() {
        return a>0&&b>0;
    }
    //Calculate rectangular area
    public double Mj(){
        return a*b;
    }
}
//Triangle class
class Triangle extends Shape{
    private double a;
    private double b;
    private double c;
    //Create a parameterless construction method
    public Triangle() {
    }
    //Create a construction method with parameters
    public Triangle(double a, double b, double c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    //getter
    public double A(){
        return a;
    }
    public double B(){
        return b;
    }
    public double C(){
        return c;
    }
    //setter
    public void setA(double a){
        this.a=a;
    }
    public void setB(double b){
        this.b=b;
    }
    public void setC(double c){
        this.c=c;
    }
    //Judge whether the data is legal
    public boolean pdsfhf(){
        Double[] bc=new Double[3];
        bc[0]=a;bc[1]=b;bc[2]=c;
        boolean z=true;
        Arrays.sort(bc);
        if(a<=0&&b<=0&&c<=0){
            z=false;
        }
        else{
            if(!(a+b>c)){
                z=false;
            }
        }
        return z;
    }
    //Calculate triangle area
    public double Mj(){
        double s1,s2;
        s1=(a+b+c)/2;
        s2=Math.sqrt(s1*(s1-a)*(s1-b)*(s1-c));
        return s2;
    }
}
//Tradezoid class
class Trapezoid extends Shape{
    private double c,b,h;
    //Create a parameterless construction method
    public Trapezoid() {
    }
    //Create a construction method with parameters
    public Trapezoid(double c, double b, double h) {
        this.c = c;
        this.b = b;
        this.h = h;
    }
    //getter
    public double C(){
        return c;
    }
    public double B(){
        return b;
    }
    public double H(){
        return h;
    }
    //setter
    public void setC(double a){
        this.c=c;
    }
    public void setB(double b){
        this.b=b;
    }
    public void setH(double c){
        this.h=h;
    }
    //Judge whether the data is legal
    public boolean pdsfhf() {
        return b>0&&c>0&&h>0;
    }
    //Calculate trapezoidal area
    public double Mj() {
        return (c+b)*h/2;
    }
}

//Main class
public class Main{
    public static Scanner x = new Scanner(System.in);//Define a static Scanner object in the Main class, so that if you want to use this object for input in other classes, you can directly use Main input. next... Just enough (avoid pit)
    public static void main(String[] args){
        ArrayList<Integer> xzlist = new ArrayList<>();
        int xz;
        xz=x.nextInt();
        if (xz==0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        while (xz!=0){//Loop in the first row of data
            if(xz>4||xz<0){//If the input data is illegal
                System.out.println("Wrong Format");
                System.exit(0);
            }
            xzlist.add(xz);
            xz=x.nextInt();
        }
        //Check whether the data is legal
        Chulishuju chulishuju=new Chulishuju(xzlist);
        if(!chulishuju.pdsfhf()){
            System.out.println("Wrong Format");
            System.exit(0);
        }

        chulishuju.show();
        x.close();
    }
}

Keywords: Java

Added by slands10 on Thu, 10 Feb 2022 03:24:12 +0200