7-37 图形卡片排序游戏 (40 分)
掌握類的繼承、多態性使用方法以及接口的應用。
輸入格式:
首先,在一行上輸入一串數字(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:
在這里給出相應的輸出。例如:
Wrong Format
輸入樣例2:
在這里給出一組輸入。例如:
輸出樣例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:
在這里給出一組輸入。例如:
輸出樣例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;}@Overridepublic 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*/@Overridepublic 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;}@Overridepublic double getArea() {return Math.PI*radius*radius;}@Overridepublic 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;}@Overridepublic double getArea() {return height*width;}@Overridepublic 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;}@Overridepublic double getArea() {double p=(side1+side2+side3)/2;return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));}@Overridepublic 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;}@Overridepublic double getArea() {return (topSide+bottomSide)*height/2;}@Overridepublic boolean validate() {return topSide>0&&height>0&&bottomSide>0;} }總結
以上是生活随笔為你收集整理的7-37 图形卡片排序游戏 (40 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《地下城与勇士:起源》荒古的记忆碎片获取
- 下一篇: 海伦堡上个浪漫地产怎么样?海伦堡最后的浪