**
7-1 graphic card sorting game (40 points)
**
Master the inheritance of classes, the use of polymorphism and the application of interfaces. See [operation instruction] for details Operation instruction v1.07-2020 0.pdf
Input format:
First, input a string of numbers (1 ~ 4, integers) 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
Then, according to the card graphic type represented by the first line of numbers, input the relevant parameters of each graphic in turn. For example, the radius of the circle needs to be input for the circular card, the width and length of the rectangle need to be input for the rectangular card, the length of three sides of the triangle needs to be input for the triangular card, and the upper bottom, lower bottom and height of the trapezoid need to be input for the trapezoid. The data is separated by one or more spaces.
Output format:
If the number of graphics is illegal (less than 0) or the value of graphics attribute is illegal (the value is less than 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;
The type and area of each figure after sorting are in the same format as the output before sorting;
The total area of all drawings, in the format of Sum of area: total area value.
shopping spree
1. Application of inheritance and polymorphism;
2. Application method of ArrayList generics
3. Application of comparable interface and generics
4. Application of the principle of single responsibility
5. Application of "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 sorted list: Circle:98.52 Triangle:4.02
Rectangle:3.22 Trapezoid:1.14 Sum of area:106.91
Input example 3:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
Output example 3:
Wrong Format
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<>();//Storage side length 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"); } case 2 -> { Card b1 = new Card(new Rectangle(Main.x.nextDouble(), Main.x.nextDouble())); cardList.add(b1); b1.getShape().setName("Rectangle"); } case 3 -> { Card c1 = new Card(new Triangle(Main.x.nextDouble(), Main.x.nextDouble(), Main.x.nextDouble())); cardList.add(c1); c1.getShape().setName("Triangle"); } case 4 -> { Card d1 = new Card(new Trapezoid(Main.x.nextDouble(), Main.x.nextDouble(), Main.x.nextDouble())); cardList.add(d1); d1.getShape().setName("Trapezoid"); } } } } } public boolean pdsfhf(){//Judge whether the data is legal boolean kp=true; for (Card a:cardList){ if (!a.getShape().pdsfhf()){ kp=false; break; } } return kp; } public void cardSort(){//sort TreeSet<Card> kp = new TreeSet<>(cardList); for (Card a : kp) { System.out.print(a.getShape()); } } public double getZmj(){//Calculated total area double zmj=0; for (Card a : cardList) { zmj=zmj+a.getShape().Mj(); } return zmj; } public void show(){//output System.out.println("The original list:"); for (Card a : cardList) { System.out.print(a.getShape()); } System.out.println("\nThe sorted list:"); cardSort(); System.out.printf("\nSum of area:%.2f\n",getZmj()); } } //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(){ return Math.PI*r*r; } } //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(); 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(); } }