0
点赞
收藏
分享

微信扫一扫

图形卡片分组游戏

天使魔鬼 2022-04-29 阅读 14

7-2 图形卡片分组游戏

单位 南昌航空大学

掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。

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:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
  2. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为The max 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 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

代码长度限制

10 KB

时间限制

400 ms

内存限制

64 MB

我滴代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
//给定的主函数

public class Main{
	public static Scanner input = new Scanner(System.in);
	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		int num = input.nextInt();
        
        if(num == 0) {
			System.out.println("Wrong Format");
			System.exit(0);
		}
		
		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();
	}
}
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.Comparator;

class DealCardList {

	ArrayList<Card> cardList = new ArrayList<Card>();

	public DealCardList() {
		super();
	}

	public DealCardList(ArrayList<Integer> list) {
		for(int i:list) {
			if(i==1) {
				Card a = new Circle(Main.input.nextDouble());
				cardList.add(a);
			}
			else if(i==2) {
				Card a =new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
				cardList.add(a);
			}
			else if(i==3) {
				Card a =new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
				cardList.add(a);
			}else if(i==4) {
				Card a = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
				cardList.add(a);
			}
		}
	}

	public boolean validate() {
		for(Card i:cardList) {
			if(!i.getShape().validate()) {
				return false;
			}
		}
		return true;
	}

	public void showResult() {
		System.out.println("The original list:");
		System.out.print("[");
		for(Card i : cardList) {
			System.out.print(i);
		}
		System.out.print("]");
		
		System.out.println("\nThe Separated List:");
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Circle")) {
				System.out.print(i);
			}
		}
		System.out.print("]");
		
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Rectangle")) {
				System.out.print(i);
			}
		}
		System.out.print("]");
		
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Triangle")) {
				System.out.print(i);
			}
		}
		System.out.print("]");
		
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Trapezoid")) {
				System.out.print(i);
			}
		}
		System.out.print("]");
		
		System.out.println("\nThe Separated sorted List:");
		Collections.sort(cardList,new Comparator<Card>() {
            public int compare(Card a, Card b) {
                return b.compareTo(a);
            }
        });
		double suma=0,sumb = 0;
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Circle")) {
				System.out.print(i);
				suma += i.getShape().getArea();
			}
		}
		System.out.print("]");
		
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Rectangle")) {
				System.out.print(i);
				sumb += i.getShape().getArea();
			}
		}
		if(suma < sumb) {
			suma = sumb;
		}
		sumb = 0;
		System.out.print("]");
		
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Triangle")) {
				System.out.print(i);
				sumb += i.getShape().getArea();
			}
		}
		if(suma < sumb) {
			suma = sumb;
		}
		sumb = 0;
		System.out.print("]");
		
		System.out.print("[");
		for(Card i : cardList) {
			if(i.getShape().getName().equals("Trapezoid")) {
				System.out.print(i);
				sumb += i.getShape().getArea();
			}
		}
		if(suma < sumb) {
			suma = sumb;
		}
		sumb = 0;
		System.out.print("]");
		
		System.out.print("\nThe max area:"+String.format("%.2f",suma));
		
	}
	
}

abstract class Card {

	Shape shape;

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

	public Card() {
		super();
	}

	public Shape getShape() {
		return shape;
	}

	public void setShape(Shape shape) {
		this.shape = shape;
	}
	
	public int compareTo(Card card) {
		if(this.shape.getArea()>card.shape.getArea()) {
			return 1;
		}else if(this.shape.getArea()==card.shape.getArea()) {
			return 0;
		}else {
			return -1;
		}
	}
}

abstract class Shape extends Card{

	String name;

	public Shape(String name) {
		super();
		this.name = name;
		this.shape = this;
	}

	public Shape() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	abstract public double getArea();
	
	abstract public boolean validate();
	
	@Override
	public String toString() {
		
		return name+":"+String.format("%.2f",this.getArea())+" ";
	}
}

class Circle extends Shape {

	double radius;

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

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

	@Override
	public boolean validate() {
		if(radius<=0) {
			return false;
		}
		return true;
	}

}

class Rectangle extends Shape {

	double width,length;
	
	public Rectangle(double width, double length) {
		super("Rectangle");
		this.width = width;
		this.length = length;
	}

	public Rectangle() {
		super();
		// TODO Auto-generated constructor stub
	}

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

	@Override
	public boolean validate() {
		if(width<=0||length<=0) {
			return false;
		}
		return true;
	}

}

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

	

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

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

	@Override
	public boolean validate() {
		if(bottomSide<=0||height<=0||topSide<=0) {
			return false;
		}
		return true;
	}

}

class Triangle extends Shape {

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

	public Triangle() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public double getArea() {
		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) {
			return false;
		}
		if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) {
			return false;
		}
		return true;
	}

}

我滴总结:

这道题是上一题的升级版本。新增了几个功能。

通过调用父类的抽象方法来调用让子类按照各自的规则排序。

按照类图写就好,没什么难度。

需要注意的就是排序比较部分可能需要多花点时间。

举报

相关推荐

0 条评论