**
7-2 图形卡片分组游戏 (60 分)
**
掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书2021-OO第07次作业-2指导书V1.0.pdf
输入格式:
在一行上输入一串数字(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 ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
各组中面积之和的最大值输出,格式为The max area:面积值。
解题报告
1.必须在作业7-1的基础上进行进一步改进和完善。
2.考虑面向对象设计的“单一职责原则”,思考该程序是否能够符合“开-闭”原则。
输入样例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.52Triangle: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
输入样例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
输出样例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
输入样例4:
1 1 3 0
6.5 12.54 3.6 5.3 6.4
输出样例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 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;
}
}
}
}
//判断数据是否合法
public boolean pdsfhf(){
boolean kp=true;
for (Card a:cardList){
if (!a.getShape().pdsfhf()){
kp=false;
break;
}
}
return kp;
}
//输出圆的数据并计算总面积
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("]");
}
//输出矩形的数据并计算总面积
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("]");
}
//输出三角形的数据并计算总面积
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("]");
}
//输出梯形的数据并计算总面积
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("]");
}
//从大到小输出圆的数据
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("]");
}
//从大到小输出矩形的数据
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("]");
}
//从大到小输出三角形的数据
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("]");
}
//从大到小输出梯形的数据
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("]");
}
//找出最大总面积
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(){//输出
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 Card implements Comparable<Card>{
private Shape shape;
//创建无参构造方法
public Card() {
}
//创建带参构造方法
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类
abstract class Shape {
private String name;
//创建无参构造方法
public Shape() {
}
//创建带参构造方法
public Shape(String name) {
this.name = name;
}
//getter
public String getName() {
return name;
}
//setter
public void setName(String name) {
this.name = name;
}
//面积
public abstract double Mj();
//判断数据是否合法
public abstract boolean pdsfhf();
public String toString() {
return getName()+":"+String.format("%.2f ",Mj());
}
}
//Circle类
class Circle extends Shape{
private double r;
//创建无参构造方法
public Circle(){
}
//创建带参构造方法
public Circle(double r){
this.r=r;
}
//getter
public double getR(){
return r;
}
//setter
public void setR(double r){
this.r = r;
}
//判断数据是否合法
public boolean pdsfhf() {
return r>0;
}
//计算圆面积
public double Mj(){
double mj=Math.PI*r*r;
return mj;
}
}
//Rectangle类
class Rectangle extends Shape{
private double a,b;
//创建无参构造方法
public Rectangle(){
}
//创建带参构造方法
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;
}
//判断数据是否合法
public boolean pdsfhf() {
return a>0&&b>0;
}
//计算矩形面积
public double Mj(){
return a*b;
}
}
//Triangle类
class Triangle extends Shape{
private double a;
private double b;
private double c;
//创建无参构造方法
public Triangle() {
}
//创建带参构造方法
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;
}
//判断数据是否合法
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;
}
//计算三角形面积
public double Mj(){
double s1,s2;
s1=(a+b+c)/2;
s2=Math.sqrt(s1*(s1-a)*(s1-b)*(s1-c));
return s2;
}
}
//Trapezoid类
class Trapezoid extends Shape{
private double c,b,h;
//创建无参构造方法
public Trapezoid() {
}
//创建带参构造方法
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;
}
//判断数据是否合法
public boolean pdsfhf() {
return b>0&&c>0&&h>0;
}
//计算梯形面积
public double Mj() {
return (c+b)*h/2;
}
}
//主类
public class Main{
public static Scanner x = new Scanner(System.in);//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接使用Main.input.next...即可(避免采坑)
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){//循环输入第一行数据
if(xz>4||xz<0){//如果输入的数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
xzlist.add(xz);
xz=x.nextInt();
}
//检查数据是否合法
Chulishuju chulishuju=new Chulishuju(xzlist);
if(!chulishuju.pdsfhf()){
System.out.println("Wrong Format");
System.exit(0);
}
chulishuju.show();
x.close();
}
}