7-9

一. 前言

首先,总结一下这三次题目集的我的个人感受及做题情况。从题量上看,三次题目集的题量都只有1到2题;从题目质量和难度上看,题目质量个人感觉是整体质量不错,尤其是有些测试点的位置比较巧妙,难度相对较大,但只要设计的巧妙就可以避免很多不必要的繁琐;从知识点看,这三次题目集,第一次题目集主要考察的是继承和多态的使用,以及Arraylist类的灵活应用,第二次题目集和第三次题目集都是编写一个银行ATM机的模拟程序,我个人感觉更多的考验自己设计程序结构的能力以及多态继承方面的灵活应用。

二. 设计与分析

  • 题目集7(7-1)、(7-2)俩道题目的递进式设计分析总结

  • 7-1
  • 输入格式:

    • 首先,在一行上输入一串数字(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:总面积值
  • import java.util.ArrayList;
    import java.util.Scanner;
    import java.lang.Math;
    
    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();
    		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{
     		protected ArrayList<Card> cardList = new ArrayList<Card>();
     		public DealCardList() {
     		}
     		public DealCardList(ArrayList<Integer> list) {
     			int i = 0;
     			while(i<list.size()) {
     				if(list.get(i).intValue()==1) {
     					Circle circle = new Circle(Main.input.nextDouble());
     					circle.setShapeName("Circle");
     					Card card = new Card(circle);
     					cardList.add(card);
     				}
     				else if(list.get(i).intValue()==2) {
     					Rectangle rectangle = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
     					rectangle.setShapeName("Rectangle");
     					Card card = new Card(rectangle);
     					cardList.add(card);
     				}
     				else if(list.get(i).intValue()==3) {
     					Triangle triangle = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
     					triangle.setShapeName("Triangle");
     					Card card = new Card(triangle);
     					cardList.add(card);
     				}
     				else {
     					Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
     					trapezoid.setShapeName("Trapezoid");
     					Card card = new Card(trapezoid);
     					cardList.add(card);
     				}
     				i++;
     			}
     		}
     		public boolean validate() {
     			int i = 0;
     			while(i<cardList.size()) {
     				if(!cardList.get(i).getShape().validate()) {
     					return false;
     				}
     				i++;
     			}
     			return true;
     		}
     		public void cardSort(){
     			Card c = new Card();
     			for(int i = 0;i<cardList.size()-1;i++) {
     				int Max = i;
     				for(int j=i+1;j<cardList.size();j++) {
     					if(cardList.get(Max).compareTo(cardList.get(j))==1) {
     						Max = j;
     					}
     				}
     				c = cardList.get(i);
     				cardList.set(i,cardList.get(Max));
     				cardList.set(Max, c);
     			}
     		}
     		public double getAllArea() {
     			int i = 0;
     			double area = 0;
     			while(i<cardList.size()) {
     				area = area + cardList.get(i).getShape().getArea();
     				i++;
     			}
     			return area;
     		}
     		public void showResult() {
     			int i = 0;
     			System.out.println("The original list:");
     			while(i<cardList.size()) {
     				System.out.printf(cardList.get(i).getShape().toString());
     				i++;
     			}
     			System.out.println("");
     			System.out.println("The sorted list:");
     			cardSort();
     			i = 0;
     			while(i<cardList.size()) {
     				System.out.printf(cardList.get(i).getShape().toString());
     				i++;
     			}
     			System.out.println("");
     			System.out.println("Sum of area:"+String.format("%.2f",this.getAllArea()));
     		}
     	}
     	
     	class Card implements Comparable{
     		private Shape shape;
    		public Card() {
     		}
     		public Card(Shape shape) {
     			this.setShape(shape);
     		}
     		public Shape getShape() {
    			return shape;
    		}
    		public void setShape(Shape shape) {
    			this.shape = shape;
    		}
     		public int compareTo(Card card) {
    			if(card.getShape().getArea()>=this.shape.getArea()) {
    				return 1;
    			}
    			return 0;
     		}
     	}
     	
     	interface Comparable{
     		abstract int compareTo(Card card);
     	}
     	
     	abstract class Shape{
     		private String shapeName;
    		public Shape() {
     		}
     		public Shape(String shapeName) {
     			this.setShapeName(shapeName);
     		}
     		public String getShapeName() {
     			return this.shapeName;
     		}
     		public void setShapeName(String shapeName) {
     			this.shapeName=shapeName;
    		}
     		public abstract double getArea();
     		public abstract boolean validate();
     		public String toString() {
     			String s = this.shapeName+":"+String.format("%.2f",getArea()).toString()+" ";
     			return s;
     		}
     	}
     	
     	class Circle extends Shape{
     		private double radius;
    		public Circle() {
     		}
     		public Circle(double radius) {
     			this.setRadius(radius);
     		}
     		public double getRadius() {
    			return radius;
    		}
    		public void setRadius(double radius) {
    			this.radius = radius;
    		}
     		public double getArea() {
     			return Math.PI*radius*radius;
     		}
     		public boolean validate() {
     			if(this.radius<=0) {
     				return false;
     			}
     			return true;
     		}
     	}
     	
     	class Rectangle extends Shape{
     		private double width;
    		private double length;
     		public Rectangle() {
     		}
     		public Rectangle(double width,double length) {
     			this.setLength(length);
     			this.setWidth(width);
     		}
     		public double getWidth() {
    			return width;
    		}
    		public void setWidth(double width) {
    			this.width = width;
    		}
    		public double getLength() {
    			return length;
    		}
    		public void setLength(double length) {
    			this.length = length;
    		}
    		public double getArea() {
     			return width*length;
     		}
     		public boolean validate() {
     			if(this.length<=0|this.width<=0) {
     				return false;
     			}
     			return true;
     		}
     	}
     	
     	class Triangle extends Shape{
     		private double side1;
     		private double side2;
     		private double side3;
     		public Triangle() {
     		}
     		public Triangle(double side1,double side2,double side3) {
     			this.side1=side1;
     			this.side2=side2;
     			this.side3=side3;
     		}
     		public double getArea() {
     			double a = ((side1 * side1) + (side2 * side2) - (side3 * side3)) / (2 * side1 * side2);
    			double b = (side1 * side2) * Math.sin((Math.acos(a))) / 2;
    			return b;
     		}
     		public boolean validate() {
     			if(this.side1<=0|this.side2<=0|this.side3<=0) {
     				return false;
     			}
     			if(side1+side2<=side3|side2+side3<=side1|side1+side3<=side2) {
     				return false;
     			}
     			return true;
     		}
     	}
     	
     	class Trapezoid extends Shape{
     		private double topSide;
     		private double bottomSide;
     		private double height;
     		public Trapezoid() {
     		}
     		public Trapezoid(double topSide,double bottomSide,double height) {
     			this.bottomSide=bottomSide;
     			this.height=height;
     			this.topSide=topSide;
     		}
     		public double getArea() {
     			return (topSide+bottomSide)*height/2;
     		}
     		public boolean validate() {
     			if(topSide<=0|bottomSide<=0|height<=0) {
     				return false;
     			}
     			return true;
     		}
     	}
    

      7-2

    • 在一行上输入一串数字(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:面积值
    5. 代码
    6. import java.util.ArrayList;
      import java.util.List;
      import java.util.Scanner;
      import java.lang.Math;
      
      public class Main{
      	public static Scanner input = new Scanner(System.in);
      	public static void main(String[] args) {
      		List<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.getResult();
      		input.close();
      	}
      
      	static class DealCardList {// 卡片链表
      		protected List<Card> cardList = new ArrayList<Card>();
      		protected List<Card> circlelist = new ArrayList<Card>();
      		protected List<Card> rectanglelist = new ArrayList<Card>();
      		protected List<Card> trianglelist = new ArrayList<Card>();
      		protected List<Card> trapezoidList = new ArrayList<Card>();
      
      		public DealCardList() {
      		}
      
      		public DealCardList(List<Integer> list) {// 处理加分类
      			int i = 0;
      			while (i < list.size()) {
      				if (list.get(i).intValue() == 1) {
      					Shape circle = new Circle(Main.input.nextDouble());
      					circle.setShapeName("Circle");
      					Card card = new Card(circle);
      					cardList.add(card);
      					circlelist.add(card);
      				} else if (list.get(i).intValue() == 2) {
      					Rectangle rectangle = new Rectangle(Main.input.nextDouble(), Main.input.nextDouble());
      					rectangle.setShapeName("Rectangle");
      					Card card = new Card(rectangle);
      					cardList.add(card);
      					rectanglelist.add(card);
      				} else if (list.get(i).intValue() == 3) {
      					Triangle triangle = new Triangle(Main.input.nextDouble(), Main.input.nextDouble(),Main.input.nextDouble());
      					triangle.setShapeName("Triangle");
      					Card card = new Card(triangle);
      					cardList.add(card);
      					trianglelist.add(card);
      				} else {
      					Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(), Main.input.nextDouble(),Main.input.nextDouble());
      					trapezoid.setShapeName("Trapezoid");
      					Card card = new Card(trapezoid);
      					cardList.add(card);
      					trapezoidList.add(card);
      				}
      				i++;
      			}
      		}
      
      		public boolean validate() {
      			int i = 0;
      			while (i < cardList.size()) {
      				if (!cardList.get(i).getShape().validate()) {
      					return false;
      				}
      				i++;
      			}
      			return true;
      		}
      
      		public double getAllArea(List<Card> list) {
      			int i = 0;
      			double area = 0;
      			while (i < list.size()) {
      				area = area + list.get(i).getShape().getArea();
      				i++;
      			}
      			return area;
      		}
      
      		public void show(List<Card> cardList) {
      			System.out.print("[");
      			for (Card c : cardList) {
      				System.out.printf(c.shape.shapeName + ":%.2f" + " ", c.shape.getArea());
      			}
      			System.out.print("]");
      		}
      
      		public void cardSort(List<Card> cardList) {
      			Card c = new Card();
      			for (int i = 0; i < cardList.size() - 1; i++) {
      				int Max = i;
      				for (int j = i + 1; j < cardList.size(); j++) {
      					if (cardList.get(Max).compareTo(cardList.get(j)) == 1) {
      						Max = j;
      					}
      				}
      				c = cardList.get(i);
      				cardList.set(i, cardList.get(Max));
      				cardList.set(Max, c);
      			}
      		}
      
      		public void getResult() {
      			System.out.println("The original list:");
      			this.show(cardList);
      			System.out.println("");
      			System.out.println("The Separated List:");
      			this.show(circlelist);
      			this.show(rectanglelist);
      			this.show(trianglelist);
      			this.show(trapezoidList);
      			System.out.println("");
      			this.cardSort(circlelist);
      			this.cardSort(rectanglelist);
      			this.cardSort(trianglelist);
      			this.cardSort(trapezoidList);
      			System.out.println("The Separated sorted List:");
      			this.show(circlelist);
      			this.show(rectanglelist);
      			this.show(trianglelist);
      			this.show(trapezoidList);
      			System.out.println("");
      			double a = Math.max(this.getAllArea(circlelist), this.getAllArea(rectanglelist));
      			double b = Math.max(this.getAllArea(trianglelist), this.getAllArea(trapezoidList));
      			System.out.printf("The max area:%.2f", Math.max(a, b));
      		}
      	}
      
      	static class Card implements Comparable {
      		private Shape shape;
      
      		public Card() {
      		}
      
      		public Card(Shape shape) {
      			this.setShape(shape);
      		}
      
      		public Shape getShape() {
      			return shape;
      		}
      
      		public void setShape(Shape shape) {
      			this.shape = shape;
      		}
      
      		@Override
      		public int compareTo(Card card) {
      			if (card.shape.getArea() > this.shape.getArea()) {
      				return 1;
      			} else if (card.shape.getArea() < this.shape.getArea()) {
      				return -1;
      			} else {
      				return 0;
      			}
      		}
      	}
      
      	interface Comparable {
      		abstract int compareTo(Card card);
      	}
      
      	abstract static class Shape {
      		private String shapeName;
      
      		public Shape() {
      		}
      
      		public Shape(String shapeName) {
      			this.setShapeName(shapeName);
      		}
      
      		public String getShapeName() {
      			return this.shapeName;
      		}
      
      		public void setShapeName(String shapeName) {
      			this.shapeName = shapeName;
      		}
      
      		public abstract double getArea();
      
      		public abstract boolean validate();
      
      		public String toString() {
      			String s = this.shapeName + ":" + String.format("%.2f", getArea()).toString() + " ";
      			return s;
      		}
      	}
      
      	static class Circle extends Shape {
      		private double radius;
      
      		public Circle() {
      		}
      
      		public Circle(double radius) {
      			this.setRadius(radius);
      		}
      
      		public double getRadius() {
      			return radius;
      		}
      
      		public void setRadius(double radius) {
      			this.radius = radius;
      		}
      
      		public double getArea() {
      			return Math.PI * radius * radius;
      		}
      
      		public boolean validate() {
      			if (this.radius <= 0) {
      				return false;
      			}
      			return true;
      		}
      	}
      
      	static class Rectangle extends Shape {
      		private double width;
      		private double length;
      
      		public Rectangle() {
      		}
      
      		public Rectangle(double width, double length) {
      			this.setLength(length);
      			this.setWidth(width);
      		}
      
      		public double getWidth() {
      			return width;
      		}
      
      		public void setWidth(double width) {
      			this.width = width;
      		}
      
      		public double getLength() {
      			return length;
      		}
      
      		public void setLength(double length) {
      			this.length = length;
      		}
      
      		public double getArea() {
      			return width * length;
      		}
      
      		public boolean validate() {
      			if (this.length <= 0 | this.width <= 0) {
      				return false;
      			}
      			return true;
      		}
      	}
      
      	static class Triangle extends Shape {
      		private double side1;
      		private double side2;
      		private double side3;
      
      		public Triangle() {
      		}
      
      		public Triangle(double side1, double side2, double side3) {
      			this.side1 = side1;
      			this.side2 = side2;
      			this.side3 = side3;
      		}
      
      		public double getArea() {
      			double a = ((side1 * side1) + (side2 * side2) - (side3 * side3)) / (2 * side1 * side2);
      			double b = (side1 * side2) * Math.sin((Math.acos(a))) / 2;
      			return b;
      		}
      
      		public boolean validate() {
      			if (this.side1 <= 0 | this.side2 <= 0 | this.side3 <= 0) {
      				return false;
      			}
      			if (side1 + side2 <= side3 | side2 + side3 <= side1 | side1 + side3 <= side2) {
      				return false;
      			}
      			return true;
      		}
      	}
      
      	static class Trapezoid extends Shape {
      		private double topSide;
      		private double bottomSide;
      		private double height;
      
      		public Trapezoid() {
      		}
      
      		public Trapezoid(double topSide, double bottomSide, double height) {
      			this.bottomSide = bottomSide;
      			this.height = height;
      			this.topSide = topSide;
      		}
      
      		public double getArea() {
      			return (topSide + bottomSide) * height / 2;
      		}
      
      		public boolean validate() {
      			if (topSide <= 0 | bottomSide <= 0 | height <= 0) {
      				return false;
      			}
      			return true;
      		}
      	}
      }
      

        

      设计思路分析总结:

      ​ 这俩个题目都是设计ATM仿真系统,题目集9相较于题目集8不仅可以取钱还可以存钱,而题目集9相较于题目8支持跨行取钱并且贷款:

      存储类这俩题使用的都是ArrayList类,便于随时扩充代码内容,提高代码的简洁性,提高代码的复用性;

      ​ 俩题的MAIN类功能差不多,都是要对数据进行处理和检验正确性,处理数据用到了正则表达式去判断ATM机号是否正确,判断卡号是否存在使用到findid()方法去找卡号,密码检验则是简单的字符串判断是否相等,然后是根据输入的数据,先逐行截取(split),再根据空格截取每一行每一段字,对其进行判断操作,并输出相应的内容,出现不合法问题停止当前程序输出Wrong Format。

      三. 改进建议

      针对继承与多态这方面的使用,我觉得自己还需要多去看一些相关书籍,让自己更好的去使用,提高代码的复用性,在设计类方面,感觉自己需要提高一下自己的逻辑思维能力,思考需要缜密一点,不要停留在表面,多考虑吧指责单一原则,开闭原则。在日后的日常解决题目需求的地方,我觉得可以多去使用接口,提高代码的简洁度,利用好JAVA语言的特性,而不是一成不变按照以前的编程思想去复杂地完成自己想要的效果。

      四. 总结

      我需要学习的点还有很多,首先就是需要提高一下自己的代码结构设计能力,不要自己给自己埋坑,然后就是需要多去了解一下JAVA的接口,哪些接口可以让我们在日常使用中更为方便简便。我觉得这三次题目集重在指导意义,通过前一次和后一次的对比,可以让自己很清晰的知道自己的不足,也让自己对犯错的印象更为深刻,对于继承、多态、抽象类、接口以及代码结构设计,自己都还需要进一步去了解他们更深的概念,从根本上去掌握他们。总之,继续努力,加油!

posted @ 2021-12-18 23:14  三块一毛二  阅读(267)  评论(0)    收藏  举报