BLOG-2
前言:这两次PTA4与5都是菜单计价系统的续集,并且都是PTA3的不同迭代的代码, 所以需要对PTA3进行不同的修改来完成题目要求。
PTA4:
由于时间等诸多原因,我并没有完成PTA4上的任务,并且没有在PTA4上及时的提交代码,所以我无法附上我PTA4的源码,但是我将对菜单计价系统4的题目进行分析设计,完成代码设计的基本要求

设计与分析:
-
解析输入的菜谱信息和订单信息,将其存储在适当的数据结构中。
-
对每一桌的订单记录进行处理,根据题目要求进行相应的操作。
-
根据处理后的订单记录计算每一桌的菜品总价,并输出结果。
接下来,我们来详细分析每个步骤的解决方法。
-
解析输入的菜谱信息和订单信息:
- 首先,我们可以将菜谱信息存储在一个字典中,菜品名作为键,基础价格作为值。
- 订单信息可以存储在一个列表中,每个元素代表一条订单记录。
-
对每一桌的订单记录进行处理:
- 首先,我们需要按照序号对订单记录进行排序,检查序号是否连续且递增,如果不符合要求,则输出错误信息并忽略当前记录。
- 然后,我们需要根据订单记录的类型进行不同的处理:
- 如果是菜谱信息,根据菜品名在菜谱字典中查找对应的基础价格,并更新菜谱信息。
- 如果是点菜记录,根据菜品名和份额在菜谱字典中查找对应的基础价格,并计算当前记录的价格。
- 如果是删除记录,检查序号是否存在于订单记录列表中,如果存在则删除对应的订单记录,否则输出错误信息。
- 如果是代点菜信息,根据桌号在订单记录列表中查找对应的订单记录,如果不存在则输出错误信息,否则根据菜品名和份额在菜谱字典中查找对应的基础价格,并计算代点菜的价格。
-
计算每一桌的菜品总价,并输出结果:
- 遍历订单记录列表,按桌号进行分组,计算每一桌的菜品总价,并根据是否在有效时间段内进行相应的折扣计算。
- 输出每一桌的原始总价和计算折扣后总价。
以上就是对题目要求的详细分析,根据这个分析,我们可以编写相应的代码来解决问题。
PTA5:
import jdk.nashorn.api.scripting.ScriptObjectMirror; import java.util.*; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Menu menu = new Menu(); Calendar cal = Calendar.getInstance(); Map<Integer, Table> tables = new HashMap<>(); int currentTableCode = 0; Table currentTable = null; while (true) { try { String input = in.nextLine().trim(); if (input.equals("end")){ break; } String[] inputs = input.split(" "); if (inputs.length > 4 && inputs[0].equals("table")) { if (inputs[3].length() > 10||!inputs[4].matches("(^133|^135|^136|^180|^181|^189)\\d{8}$")){ System.out.println("wrong format"); continue; } else if (11 != inputs[4].length()) { if (inputs[4].length() == 11) { } else { System.out.println("wrong format"); } } Table table = new Table(inputs[5].split("/"), inputs[6].split("/")); if (checkTime(table) < 0){ System.out.println("wrong format"); } else if (checkTime(table) == 1) { currentTable = table; currentTable.name = inputs[3]; currentTable.phone = inputs[4]; currentTableCode = Integer.parseInt(inputs[1]); tables.put(currentTableCode, currentTable); System.out.println("table " + currentTableCode + ": "); } else if (checkTime(table) == 0) { table.name = inputs[3]; table.phone = inputs[4]; currentTableCode = Integer.parseInt(inputs[1]); tables.put(currentTableCode, table); } } else if (Pattern.matches("^\\d+\\s\\d+\\s\\S+\\s\\d+\\s\\d+\\s\\d+", input) && currentTable != null) { try { Dish dish = menu.find(inputs[2]); if (dish == null|| !dish.isValidSweetness(Integer.parseInt(inputs[3]))) { System.out.println(inputs[2] + " does not exist"); } else { if (dish.isValidSweetness(Integer.parseInt(inputs[3]))) { int tableNo = Integer.parseInt(inputs[0]); int orderNumber = Integer.parseInt(inputs[1]); int amount = Integer.parseInt(inputs[4]); int sweetness = Integer.parseInt(inputs[5]); Record record = new Record(dish, sweetness, amount, Integer.parseInt(inputs[3])); currentTable.otherMap.put(String.valueOf(orderNumber), record); tables.get(tableNo).DaiMap.put(String.valueOf(orderNumber), record); currentTable.otherTableInfo.put(String.valueOf(orderNumber), tableNo - 1); System.out.println(orderNumber + " table " + currentTableCode + " pay for table " + tableNo + " " + record.price); } } } catch (Exception e) { System.out.println("wrong format"); } } else if (input.contains("T")) { Dish dish; if (input.contains("川")) { dish = new ChuanCai(inputs[0], Integer.parseInt(inputs[2])); dish.type = 3; } else if (input.contains("晋")) { dish = new JingCai(inputs[0], Integer.parseInt(inputs[2])); dish.type = 2; } else if (input.contains("浙")) { dish = new ZheCai(inputs[0], Integer.parseInt(inputs[2])); dish.type = 1; } else { System.out.println("wrong format"); continue; } menu.add(dish); } else if (Pattern.matches("^\\d+\\s\\S+\\s\\d+\\s\\d+\\s\\d+", input) && currentTable != null) { try { Dish dish = menu.find(inputs[1]); if (dish == null) { System.out.println(inputs[1] + " does not exist"); } else { if (dish.isValidSweetness(Integer.parseInt(inputs[2]))) { int no = Integer.parseInt(inputs[0]); Record record = new Record(dish, Integer.parseInt(inputs[4]), Integer.parseInt(inputs[3]), Integer.parseInt(inputs[2])); currentTable.records.put(no, record); currentTable.ownFees += record.price; System.out.println(no + " " + dish.name + " " + record.price); } } } catch (Exception e) { System.out.println("wrong format"); } } else if (inputs.length == 2) { if (inputs[1].equals("delete") && currentTable != null) { //7 delete int i = Integer.parseInt(inputs[0]); if (!currentTable.del(i)) { if (currentTable.otherMap.remove(i) != null) { Integer t = (Integer) currentTable.otherTableInfo.get(i); tables.get(t).DaiMap.remove(i); } } } else if (currentTableCode ==0){ menu.add(inputs[0], Integer.parseInt(inputs[1])); } }else if (inputs.length == 4) if (inputs[0].equals("table")) { } else { if (currentTable != null) { try { Dish dish = menu.find(inputs[1]); if (dish == null) { System.out.println(inputs[1] + " does not exist"); } else if (dish.type != 0) { System.out.println("wrong format"); } else { int no = Integer.parseInt(inputs[0]); Record record = new Record(dish, Integer.parseInt(inputs[3]), Integer.parseInt(inputs[2])); currentTable.records.put(no, record); currentTable.ownFees += record.price; System.out.println(no + " " + dish.name + " " + record.price); } } catch (Exception e) { System.out.println("wrong format"); } } } else if (inputs.length == 5 && currentTable != null) { Dish dish = menu.find(inputs[2]); if (dish == null) { System.out.println(inputs[2] + " does not exist"); } else { try { int tableNo = Integer.parseInt(inputs[0]); int orderNumber = Integer.parseInt(inputs[1]); int amount = Integer.parseInt(inputs[3]); int sweetness = Integer.parseInt(inputs[4]); Record rec = new Record(dish, sweetness, amount); currentTable.otherMap.put(String.valueOf(orderNumber), rec); tables.get(tableNo).DaiMap.put(String.valueOf(orderNumber), rec); currentTable.otherTableInfo.put(String.valueOf(orderNumber), tableNo - 1); System.out.println(orderNumber + " table " + currentTableCode + " pay for table " + tableNo + " " + rec.price); } catch (Exception e) { System.out.println("wrong format"); } } } } catch (Exception e) { System.out.println("wrong format"); } } Map<String, Integer> all = new TreeMap<>(); for (Map.Entry<Integer, Table> entryT : tables.entrySet()) { int ii = entryT.getKey(); Table t = entryT.getValue(); int year = Integer.parseInt(t.day[0]); int month = Integer.parseInt(t.day[1]) - 1; int day = Integer.parseInt(t.day[2]); int hour = Integer.parseInt(t.time[0]); cal.set(year, month, day); int week = cal.get(Calendar.DAY_OF_WEEK); float discount; if (week == 1 || week == 7) { if ((hour >= 10 && hour <= 20) || (hour == 9 && Integer.parseInt(t.time[1]) >= 30) || (hour == 21 && Integer.parseInt(t.time[1]) == 30 && Integer.parseInt(t.time[2]) == 0)) { discount = 1.0F; } else { System.out.println("table " + (ii) + " out of opening hours"); continue; } } else { if ((hour >= 17 && hour <= 19) || (hour == 20 && Integer.parseInt(t.time[1]) <= 30 && Integer.parseInt(t.time[2]) == 0)) { discount = 0.8F; } else if ((hour >= 11 && hour <= 13) || (hour == 10 && Integer.parseInt(t.time[1]) >= 30) || (hour == 14 && Integer.parseInt(t.time[1]) <= 30 && Integer.parseInt(t.time[2]) == 0) ) { discount = 0.6F; } else { System.out.println("table " + (ii) + " out of opening hours"); continue; } } if (all.get(t.name + " " + t.phone) == null) { all.put(t.name + " " + t.phone, t.alltime(discount)); } else { Integer r = all.get(t.name + " " + t.phone); r += t.alltime(discount); all.put(t.name + " " + t.phone, r); } System.out.println("table " + (ii) + ": " + t.allmoney() + " " + t.alltime(discount) + t.getT()); } for (Map.Entry<String, Integer> entry : all.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } public static int checkTime(Table t) { try { Calendar cal = Calendar.getInstance(); int year = Integer.parseInt(t.day[0]); int month = Integer.parseInt(t.day[1]) - 1; int day = Integer.parseInt(t.day[2]); int hour = Integer.parseInt(t.time[0]); int minute = Integer.parseInt(t.time[1]); cal.set(year, month, day); int week = cal.get(Calendar.DAY_OF_WEEK); if ((week == 1 || week == 7) && ((hour >= 10 && hour <= 20) || (hour == 9 && minute >= 30) || (hour == 21 && minute == 30))) { return 1; } else if ((week != 1 && week != 7) && ((hour >= 17 && hour <= 19) || (hour == 20 && minute <= 30))) { return 1; } else if ((hour >= 11 && hour <= 13) || (hour == 10 && minute >= 30) || (hour == 14 && minute == 0)) { return 1; } else { return 0; } } catch (Exception e) { System.out.println("wrong format"); return -1; } } } class Dish { String name; int price; int type; public Dish(String name, int price) { this.name = name; this.price = price; } boolean isValidSweetness(int sweetness) { return true; } } class ChuanCai extends Dish { public ChuanCai(String name, int price) { super(name, price); } @Override boolean isValidSweetness(int sweetness) { if (sweetness < 0 || sweetness > 5) { System.out.println("spicy num out of range :" + sweetness); return false; } return true; } } class JingCai extends Dish { public JingCai(String name, int price) { super(name, price); } @Override boolean isValidSweetness(int sweetness) { if (sweetness < 0 || sweetness > 4) { System.out.println("acidity num out of range :" + sweetness); return false; } return true; } } class ZheCai extends Dish { public ZheCai(String name, int price) { super(name, price); } @Override boolean isValidSweetness(int sweetness) { if (sweetness < 0 || sweetness > 3) { System.out.println("sweetness num out of range :" + sweetness); return false; } return true; } } class Menu { Map<String, Dish> dishes = new HashMap(); public void add(String name, int price) { dishes.put(name, new Dish(name, price)); } public void add(Dish dish) { dishes.put(dish.name, dish); } public Dish find(String name) { return dishes.get(name); } } class Record { Dish dish; int number; int dishTaste; int price; int degree; public Record(Dish dish, int number, int dishTaste) { this(dish, number, dishTaste, -1); } public Record(Dish dish, int number, int dishTaste, int degree) { this.dish = dish; this.degree = degree; this.dishTaste = dishTaste; this.number = number; price = getRecordPrice(); } private int getRecordPrice() { switch (dishTaste) { case 1: return dish.price * number; case 2: return Math.round(dish.price * 1.5f) * number; case 3: return dish.price * 2 * number; default: System.out.println("Invalid GG value"); return 0; } } } class Table { public ScriptObjectMirror otherMap; public int ownFees; public ScriptObjectMirror otherTableInfo; public ScriptObjectMirror DaiMap; Map<Integer, Record> records = new HashMap<>(); String[] day; String[] time; String name; String phone; public Table(String[] day, String[] time) { this.day = day; this.time = time; } public boolean del(int no) { Record record = records.get(no); if (record == null) { return false; } else { records.remove(no); return true; } } public int allmoney() { int sum = 0; for (Record record : records.values()) { if (record != null) sum += record.price; } return sum; } public int alltime(float discount) { if (discount == 0.6F || discount == 0.8F) { int sum = 0; for (Record record : records.values()) { if (record != null) { sum += Math.round((float) record.price * (record.dish.type == 0 ? discount : 0.7f)); } } return sum; } else { return allmoney(); } } public String getT() { List<Record> chuanCai = new ArrayList<>(); List<Record> jinCai = new ArrayList<>(); List<Record> zheCai = new ArrayList<>(); String r = " "; for (Record record : records.values()) { if (record == null) continue; switch (record.dish.type) { case 1: zheCai.add(record); break; case 2: jinCai.add(record); break; case 3: chuanCai.add(record); break; } } if (chuanCai.size() != 0) { int degree = 0; int fs = 0; for (Record record : chuanCai) { degree += record.degree * record.number; fs += record.number; } String[] ll = {"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"}; r = r + " 川菜 " + fs + " " + ll[Math.round((float) degree / (float) fs)]; } if (jinCai.size() != 0) { int degree = 0; int fs = 0; for (Record record : jinCai) { degree += record.degree * record.number; fs += record.number; } String[] ll = {"不酸", "微酸", "稍酸", "酸", "很酸"}; r = r + " 晋菜 " + fs + " " + ll[Math.round((float) degree / (float) fs)]; } if (zheCai.size() != 0) { int degree = 0; int fs = 0; for (Record record : zheCai) { degree += record.degree * record.number; fs += record.number; } String[] ll = {"不甜", "微甜", "稍甜", "甜"}; r = r + " 浙菜 " + fs + " " + ll[Math.round((float) degree / (float) fs)]; } return r.replaceAll(" {2}", " "); } }
设计与分析:
该代码是一个简单的餐厅点餐系统的实现。主要包括以下几个类:
-
Main类:包含主方法,用于启动程序。在主方法中,首先创建了一个Scanner对象in,用于读取用户的输入。然后创建了一个Menu对象menu,用于保存菜单信息。接着创建了一个Calendar对象cal,用于获取当前时间。最后创建了一个HashMap对象tables,用于保存桌子的信息。接下来进入一个死循环,不断读取用户的输入。
-
Table类:表示一个桌子的信息。包含桌子的名称、电话、订单信息等。具体的方法包括checkTime()用于检查时间格式是否正确,del()用于删除订单记录。
-
Menu类:表示菜单的信息。包含一个List对象dishes,用于保存菜单项。具体的方法包括find()用于查找菜单项,add()用于添加菜单项。
-
Dish类:表示一个菜品的信息。包含菜品的名称、甜度等。
-
ChuanCai类、JingCai类、ZheCai类:继承自Dish类,表示川菜、晋菜、浙菜的信息。
-
Record类:表示一条订单记录的信息。包含菜品、甜度、数量等。在循环中,首先使用entrySet()方法获取tables集合中的每个元素(桌子的编号和桌子对象)。然后获取桌子对象的日期和时间信息,并将其解析为整数值。接着使用Calendar对象设置年、月、日,并获取星期几的值。根据星期几和时间判断是否在营业时间内,如果不在营业时间内,则输出提示信息并跳过当前循环。如果在营业时间内,则根据星期几和时间确定折扣值。
-
接下来,使用桌子的名称和电话作为键值,将桌子的消费金额累加到all集合中。然后输出当前桌子的消费金额、消费时间和订单记录信息。循环结束后,使用entrySet()方法遍历all集合中的每个元素,输出消费者的名称、电话和消费金额。checkTime(Table t)方法用于检查桌子的预订时间是否在营业时间内。首先解析桌子的日期和时间信息,然后使用Calendar对象设置年、月、日,并获取星期几的值。根据星期几和时间判断是否在营业时间内,如果在营业时间内,则返回1,否则返回0。如果解析日期和时间的过程中出现异常,则输出错误提示信息,并返回-1。
需要注意的是,代码中可能存在一些输入格式不正确的情况,比如电话号码的格式不正确、时间格式不正确等,对于这些情况都会输出"wrong format"的提示信息。此外,代码中还存在一些可能的逻辑漏洞和错误,可能需要进一步完善和改进。
期中考试:
创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积
输入格式:
输入圆的半径,取值范围为(0,+∞),输入数据非法,则程序输出Wrong Format,注意:只考虑从控制台输入数值的情况
输出格式:
输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)
输入样例:
在这里给出一组输入。例如:
源码:
/*import java.awt.geom.Area; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); //Shape shape = new Shape(); int choice = input.nextInt(); switch (choice) { case 1://Circle double radiums = input.nextDouble(); Shape circle = new Circle(radiums); printArea(circle); break; case 2://Rectangle double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); Point leftTopPoint = new Point(x1, y1); Point lowerRightPoint = new Point(x2, y2); Rectangle rectangle = new Rectangle(leftTopPoint, lowerRightPoint); printArea(rectangle); break; } } private static void printArea(Shape circle) { System.out.printf("%.2f/n",circle); } } abstract class Shape{ abstract void shape(); abstract double getArea(); } class Circle extends Shape{ private double area; public Circle(){} public Circle(double radiums) { this.radiums = radiums; } @Override void shape() { } @Override public double getArea() { return area; } public double setArea(){ this.area = area; } } class Rectangle extends Shape{ public Rectangle(Point leftTopPoint, Point lowerRightPoint) { } public Rectangle() { } @Override void shape() { } @Override double getArea() { return 0; } } class Point extends Rectangle{ private double x; private double y; public Point() { super(); } public Point(double x, double y) { super(); this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } }*/ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); double r=Double.parseDouble(scan.nextLine()); if(r<= 0){ // r为负数或等于0时,输出Wrong Format System.out.println("Wrong Format"); } else{ Circle c=new Circle(); c.r=r;//注意赋值给私有属性r而不是直接修改radius变量 double area=c.getArea();//通过对象c调用方法getArea()得到面积 String s=String.format("%.2f",area);//将double型area保留两位小数赋值给s System.out.println(s); } } } class Circle{ double r;//定义半径 public double getArea() {//计算圆面积 return Math.PI*r*r; } }
设计与分析:
代码主要是实现了一个简单的图形面积计算程序,通过输入不同的选择,计算对应图形的面积。
-
在主函数中,首先获取用户的选择,根据选择的不同进行不同的操作。当选择为1时,表示计算圆的面积,需要输入半径r;当选择为2时,表示计算矩形的面积,需要输入左上角点的坐标和右下角点的坐标。
-
在printArea方法中,使用printf方法打印出图形的面积,保留两位小数。
-
Shape类为抽象类,定义了shape和getArea两个抽象方法,分别用于表示图形的形状和计算图形的面积。
-
Circle类继承自Shape类,实现了shape和getArea方法。在getArea方法中,根据半径r计算圆的面积。
-
Rectangle类继承自Shape类,实现了shape和getArea方法,但是getArea方法中返回的是0,没有实现矩形的面积计算逻辑。
-
Point类继承自Rectangle类,表示矩形的一个顶点,包含了x和y坐标。
-
在Main类中,通过Scanner类获取用户输入的半径r,在Circle类中实例化一个Circle对象c,并将输入的半径r赋值给对象的r属性。然后调用c的getArea方法获取圆的面积,并将结果保留两位小数后输出。
-
在Circle类中,定义了一个r属性和getArea方法,getArea方法根据圆的半径计算圆的面积。
通过分析代码,可以发现存在以下问题:
-
在Shape抽象类中,shape方法没有任何实现,可能是多余的。
-
Circle类中的setArea方法没有返回值。
-
Rectangle类中的shape方法没有任何实现,可能是多余的。
-
Point类继承自Rectangle类,但是没有实现任何新的属性或方法,可能是多余的。
-
在主函数中,当半径r小于等于0时,输出"Wrong Format",但是并没有对应的处理逻辑,应该添加对应的操作。
总结:代码存在一些问题和冗余,需要进行修复和优化。
设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:

输入格式:
分别输入两个坐标点的坐标值x1,y1,x2,y2。
输出格式:
输出该矩形的面积值(保留两位小数)。
输入样例:
在这里给出一组输入。例如:
6 5.8 -7 8.9
输出样例:
在这里给出相应的输出。例如:
40.30
源码:
/*import java.awt.*; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[]args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); ArrayList<Shape> list = new ArrayList<>(); int choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1://Circle double radiums = input.nextDouble(); double area = radiums*radiums*3.1415926; if(radiums>0) { System.out.printf("%.2f", area); } else{System.out.printf("Wrong Format");} break; case 2://Rectangle double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double area1 = (x1-x2)*(y1-y2); System.out.printf("%.2f",area1); break; } choice = input.nextInt(); } } }*/ import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; abstract class Shape implements Comparable<Shape>{ public abstract double getArea(); //对图形进行按面积大小比较 public int compareTo(Shape shape) { if(this.getArea() < shape.getArea()) return -1; else if(this.getArea() == shape.getArea()) return 0; else return 1; } } class Circle extends Shape{ private double radian; public Circle(double radian){ this.radian = radian; } public double getArea(){ return Math.PI * radian * radian; } } class Rectangle extends Shape{ private Point p1;//左上角坐标 private Point p2;//右下角坐标 private double width;//宽 private double height;//高 public Rectangle(Point p1, Point p2){ this.p1 = p1; this.p2 = p2; this.width = Math.abs(p1.getX() - p2.getX()); this.height = Math.abs(p1.getY() - p2.getY()); } public double getArea(){ return width * height; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); double x1,x2,y1,y2; ArrayList<Shape> list = new ArrayList<>(); int choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1://Circle double radiums = input.nextDouble(); Shape circle = new Circle(radiums); list.add(circle); break; case 2://Rectangle x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); Point leftTopPoint = new Point(x1,y1); Point lowerRightPoint = new Point(x2,y2); Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint); list.add(rectangle); break; } choice = input.nextInt(); } Collections.sort(list);//按面积升序排序 for(int i = 0; i < list.size(); i++) { System.out.print(String.format("%.2f", list.get(i).getArea()) + " "); } } } class Point { private double x; private double y; public Point() { } public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public void display() { System.out.printf("(%.2f,%.2f)\n",this.x,this.y); } }
设计与分析:
1. 引入了java.awt包和java.util.ArrayList类,但是并没有用到,可以删除。
2. 在主函数中,创建了一个ArrayList<Shape>对象list,用于存储不同形状的图形。在每次输入一个图形后,将该图形添加到list中。
3. 创建了一个Shape抽象类,包含了一个抽象方法getArea(),用于计算图形的面积。还实现了Comparable<Shape>接口,用于对图形按面积大小进行比较。
4. Circle类和Rectangle类分别继承自Shape类。在Circle类中,定义了一个私有属性radian,表示圆的半径。实现了getArea()方法,根据圆的半径计算圆的面积。在Rectangle类中,定义了两个私有属性p1和p2,分别表示矩形的左上角坐标和右下角坐标。实现了getArea()方法,根据坐标计算矩形的面积。
5. 创建了一个Point类,表示矩形的一个顶点。包含了x和y两个私有属性,表示顶点的坐标。提供了相应的get和set方法。
6. 在主函数中,根据用户的选择进行相应的操作:选择为1时,表示计算圆的面积;选择为2时,表示计算矩形的面积。根据输入的半径或坐标创建相应的图形对象,并将其添加到list中。
7. 在主函数中,通过Collections.sort()方法对list中的图形按面积进行升序排序。然后遍历list,打印出每个图形的面积,保留两位小数。
8. 创建了一个Point类,表示矩形的一个顶点,但是没有实现任何新的属性或方法,可能是多余的。
9. 在Circle类中,定义了一个radian属性和getArea方法,但是getArea方法没有返回值,应该添加返回值。
10. Rectangle类的shape方法没有任何实现,可能是多余的。
11. 在主函数中,当输入的半径或坐标不符合要求时,没有对应的处理逻辑,应该添加相应的操作。
12. 在Circle类的getArea方法和Rectangle类的getArea方法中,对于负值的半径或坐标并没有进行处理,应该添加相应的判断语句。
13. 在Circle类的getArea方法和Rectangle类的getArea方法中,计算面积的公式可能存在错误,应该根据具体图形的计算公式进行修正。
总结:代码进行了一些修改和优化,修复了一些错误和问题。但是还存在一些可优化的地方,比如在输入半径或坐标时没有进行异常处理,可以添加try-catch语句。另外,对于不合法的输入,可以给出相应的提示信息。

浙公网安备 33010602011771号