(1)前言:
期中考试的题目比较基础,主要涉及到了基本的面向对象思想和基本语法。
pta4与pta5为点菜系列题目难度较大
(2)设计与分析:
1.期中考试第一道题目,代码如下
import java.util.Scanner; class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } } public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double radius = scanner.nextDouble(); if (radius <= 0) { System.out.println("Wrong Format"); } else { Circle circle = new Circle(radius); double area = circle.getArea(); System.out.println(String.format("%.2f", area)); } scanner.close(); } }
这道题太简单了,这里就不赘述了
第二题代码:
import java.util.Scanner; class Rectangle { private double x1; private double y1; private double x2; private double y2; public Rectangle(double x1, double y1, double x2, double y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } public double getArea() { double width = Math.abs(x2 - x1); double height = Math.abs(y2 - y1); return width * height; } } public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); Rectangle rectangle = new Rectangle(x1, y1, x2, y2); double area = rectangle.getArea(); System.out.println(String.format("%.2f", area)); scanner.close(); } }
这道题也比较简单,只不过我没有参考题目给的类图,省略掉了点类,直接用键盘输入的四个数字作为参数。
第三题代码:
import java.util.Scanner; abstract class Shape { public abstract double getArea(); } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } } class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } } class Rectangle extends Shape { private Point leftTopPoint; private Point lowerRightPoint; public Rectangle(Point leftTopPoint, Point lowerRightPoint) { this.leftTopPoint = leftTopPoint; this.lowerRightPoint = lowerRightPoint; } @Override public double getArea() { double width = Math.abs(lowerRightPoint.getX() - leftTopPoint.getX()); double height = Math.abs(lowerRightPoint.getY() - leftTopPoint.getY()); return width * height; } } public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int choice = input.nextInt(); switch(choice) { case 1: // Circle double radius = getValidRadius(input); Shape circle = new Circle(radius); 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; } input.close(); } public static double getValidRadius(Scanner input) { double radius = 0; boolean validInput = false; while (!validInput) { System.out.print(""); if (input.hasNextDouble()) { radius = input.nextDouble(); validInput = true; } else { System.out.println("Invalid input. Please enter a valid radius."); input.nextLine(); // Clear the input buffer } } return radius; } public static void printArea(Shape shape) { double area = shape.getArea(); System.out.println(String.format("%.2f", area)); } }
这道题是将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类。在第一题和第二题的基础上添加了Point
类,突然发现这样会更方便一点。此外,选择结构的使用也使得代码更加具有条理
第四题代码:
import java.util.*; abstract class Shape implements Comparable<Shape> { public abstract double getArea(); @Override public int compareTo(Shape other) { double area1 = this.getArea(); double area2 = other.getArea(); if (area1 < area2) { return -1; } else if (area1 > area2) { return 1; } else { return 0; } } } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } } class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } } class Rectangle extends Shape { private Point leftTopPoint; private Point lowerRightPoint; public Rectangle(Point leftTopPoint, Point lowerRightPoint) { this.leftTopPoint = leftTopPoint; this.lowerRightPoint = lowerRightPoint; } @Override public double getArea() { double width = Math.abs(lowerRightPoint.getX() - leftTopPoint.getX()); double height = Math.abs(lowerRightPoint.getY() - leftTopPoint.getY()); return width * height; } } public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<Shape> list = new ArrayList<>(); int choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1: // Circle double radius = input.nextDouble(); Shape circle = new Circle(radius); list.add(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); 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()) + " "); } input.close(); } }
这道题在测验3的题目基础上,重构类设计,实现列表内图形的排序功能(按照图形的面积进行排序)。此外还用了Arraylist存储图形对象,总体来说还是比较简单的。
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; class Dish { String name; // 菜品名称 int unit_price; // 单价 public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public int getPrice(int portion) { double price; if (portion == 1) { price = unit_price; } else if (portion == 2) { price = unit_price * 1.5; } else if (portion == 3) { price = unit_price * 2; } else { throw new IllegalArgumentException("Invalid portion: " + portion); } return (int) Math.round(price); // 四舍五入取整 } } class Menu { private Map<String, Dish> dishes; // 菜品列表 public Menu() { dishes = new HashMap<>(); } public void addDish(String name, int unit_price) { Dish dish = new Dish(name, unit_price); dishes.put(name, dish); } public Dish searchDish(String dishName) { return dishes.get(dishName); } } class Record { int orderNum; // 序号 Dish dish; // 菜品 int portion; // 份额(1/2/3代表小/中/大份) public Record(int orderNum, Dish dish, int portion) { this.orderNum = orderNum; this.dish = dish; this.portion = portion; } public int getPrice() { return dish.getPrice(portion); } } class Order { List<Record> records; // 订单上的点菜记录 public Order() { records = new ArrayList<>(); } public void addRecord(int orderNum, Dish dish, int portion) { Record record = new Record(orderNum, dish, portion); records.add(record); } public void deleteRecord(int orderNum) { Record record = findRecordByNum(orderNum); if (record != null) { records.remove(record); } else { System.out.println("delete error"); } } public Record findRecordByNum(int orderNum) { for (Record record : records) { if (record.orderNum == orderNum) { return record; } } return null; } public int getTotalPrice() { int totalPrice = 0; for (Record record : records) { totalPrice += record.getPrice(); } return totalPrice; } } class Table { String tableNum; // 桌号 String time; // 时间 Order order; // 订单 public Table(String tableNum, String time) { this.tableNum = tableNum; this.time = time; order = new Order(); } public void addRecord(int orderNum, Dish dish, int portion) { order.addRecord(orderNum, dish, portion); } public void deleteRecord(int orderNum) { order.deleteRecord(orderNum); } public int getTotalPrice() { return order.getTotalPrice(); } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Menu menu = new Menu(); List<Table> tables = new ArrayList<>(); // 读取菜单 String dishInfo = scanner.nextLine(); while (!dishInfo.equals("end")) { String[] info = dishInfo.split(" "); String dishName = info[0]; int unitPrice = Integer.parseInt(info[1]); menu.addDish(dishName, unitPrice); dishInfo = scanner.nextLine(); } // 读取订单 String tableNum = scanner.nextLine(); while (!tableNum.equals("end")) { String time = scanner.nextLine(); Table table = new Table(tableNum, time); String orderInfo = scanner.nextLine(); while (!orderInfo.equals("end")) { if (orderInfo.equals("delete")) { int orderNum = Integer.parseInt(scanner.nextLine()); table.deleteRecord(orderNum); } else { String[] info = orderInfo.split(" "); int orderNum = Integer.parseInt(info[0]); String dishName = info[1]; int portion = Integer.parseInt(info[2]); Dish dish = menu.searchDish(dishName); if (dish != null) { table.addRecord(orderNum, dish, portion); } } orderInfo = scanner.nextLine(); } tables.add(table); tableNum = scanner.nextLine(); } // 按桌号从小到大输出每桌的总价 tables.sort((t1, t2) -> t1.tableNum.compareTo(t2.tableNum)); for (Table table : tables) { int totalPrice = table.getTotalPrice(); String output; if (table.time.compareTo("9:30") < 0 || table.time.compareTo("21:30") > 0) { output = "table " + table.tableNum + " out of opening hours"; } else if (table.time.compareTo("10:30") >= 0 && table.time.compareTo("14:30") <= 0) { output = String.valueOf(Math.round(totalPrice * 0.6)); } else if (table.time.compareTo("17:00") >= 0 && table.time.compareTo("20:30") <= 0) { output = String.valueOf(Math.round(totalPrice * 0.8)); } else { output = String.valueOf(totalPrice); } System.out.println(output); } } }
pta4代码如下:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; class Dish { String name; // 菜品名称 int unit_price; // 单价 public Dish(String name, int unit_price) { this.name = name; this.unit_price = unit_price; } public int getPrice(int portion) { double price; if (portion == 1) { price = unit_price; } else if (portion == 2) { price = unit_price * 1.5; } else if (portion == 3) { price = unit_price * 2; } else { throw new IllegalArgumentException("Invalid portion: " + portion); } return (int) Math.round(price); // 四舍五入取整 } } class Menu { private Map<String, Dish> dishes; // 菜品列表 public Menu() { dishes = new HashMap<>(); } public void addDish(String name, int unit_price) { Dish dish = new Dish(name, unit_price); dishes.put(name, dish); } public Dish searchDish(String dishName) { return dishes.get(dishName); } } class Record { int orderNum; // 序号 Dish dish; // 菜品 int portion; // 份额(1/2/3代表小/中/大份) public Record(int orderNum, Dish dish, int portion) { this.orderNum = orderNum; this.dish = dish; this.portion = portion; } public int getPrice() { return dish.getPrice(portion); } } class Order { List<Record> records; // 订单上的点菜记录 public Order() { records = new ArrayList<>(); } public void addRecord(int orderNum, Dish dish, int portion) { Record record = new Record(orderNum, dish, portion); records.add(record); } public void deleteRecord(int orderNum) { Record record = findRecordByNum(orderNum); if (record != null) { records.remove(record); } else { System.out.println("delete error"); } } public Record findRecordByNum(int orderNum) { for (Record record : records) { if (record.orderNum == orderNum) { return record; } } return null; } public int getTotalPrice() { int totalPrice = 0; for (Record record : records) { totalPrice += record.getPrice(); } return totalPrice; } } class Table { String tableNum; // 桌号 String time; // 时间 Order order; // 订单 public Table(String tableNum, String time) { this.tableNum = tableNum; this.time = time; order = new Order(); } public void addRecord(int orderNum, Dish dish, int portion) { order.addRecord(orderNum, dish, portion); } public void deleteRecord(int orderNum) { order.deleteRecord(orderNum); } public int getTotalPrice() { return order.getTotalPrice(); } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Menu menu = new Menu(); List<Table> tables = new ArrayList<>(); // 读取菜单 String dishInfo = scanner.nextLine(); while (!dishInfo.equals("end")) { String[] info = dishInfo.split(" "); String dishName = info[0]; int unitPrice = Integer.parseInt(info[1]); menu.addDish(dishName, unitPrice); dishInfo = scanner.nextLine(); } // 读取订单 String tableNum = scanner.nextLine(); while (!tableNum.equals("end")) { String time = scanner.nextLine(); Table table = new Table(tableNum, time); String orderInfo = scanner.nextLine(); while (!orderInfo.equals("end")) { if (orderInfo.equals("delete")) { int orderNum = Integer.parseInt(scanner.nextLine()); table.deleteRecord(orderNum); } else { String[] info = orderInfo.split(" "); int orderNum = Integer.parseInt(info[0]); String dishName = info[1]; int portion = Integer.parseInt(info[2]); Dish dish = menu.searchDish(dishName); if (dish != null) { table.addRecord(orderNum, dish, portion); } } orderInfo = scanner.nextLine(); } tables.add(table); tableNum = scanner.nextLine(); } // 按桌号从小到大输出每桌的总价 tables.sort((t1, t2) -> t1.tableNum.compareTo(t2.tableNum)); for (Table table : tables) { int totalPrice = table.getTotalPrice(); String output; if (table.time.compareTo("9:30") < 0 || table.time.compareTo("21:30") > 0) { output = "table " + table.tableNum + " out of opening hours"; } else if (table.time.compareTo("10:30") >= 0 && table.time.compareTo("14:30") <= 0) { output = String.valueOf(Math.round(totalPrice * 0.6)); } else if (table.time.compareTo("17:00") >= 0 && table.time.compareTo("20:30") <= 0) { output = String.valueOf(Math.round(totalPrice * 0.8)); } else { output = String.valueOf(totalPrice); } System.out.println(output); } } }
-
Dish
类:- 构造函数
Dish(String name, int unit_price)
:用于初始化菜品对象,并设置菜品的名称和单价。 getPrice(int portion)
方法:根据所选份额计算菜品的价格,并返回整数类型的价格。
- 构造函数
-
Menu
类:- 构造函数
Menu()
:用于初始化菜单对象,创建一个空的菜品列表。 addDish(String name, int unit_price)
方法:向菜单中添加一个菜品,传入菜品的名称和单价。searchDish(String dishName)
方法:根据菜品名称在菜单中查找并返回对应的菜品对象。
- 构造函数
-
Record
类:- 构造函数
Record(int orderNum, Dish dish, int portion)
:用于初始化点菜记录对象,设置序号、菜品和份额。 getPrice()
方法:根据点菜记录中的菜品和份额计算该记录的价格,并返回整数类型的价格。
- 构造函数
-
Order
类:- 构造函数
Order()
:用于初始化订单对象,创建一个空的点菜记录列表。 addRecord(int orderNum, Dish dish, int portion)
方法:向订单中添加一条点菜记录,传入订单号、菜品对象和份额。deleteRecord(int orderNum)
方法:从订单中删除指定序号的点菜记录。findRecordByNum(int orderNum)
方法:根据序号在订单中查找并返回对应的点菜记录对象。getTotalPrice()
方法:计算整个订单的总价格,并返回整数类型的总价。
- 构造函数
-
Table
类:- 构造函数
Table(String tableNum, String time)
:用于初始化桌子对象,设置桌号和时间。 addRecord(int orderNum, Dish dish, int portion)
方法:向桌子的订单中添加一条点菜记录,传入订单号、菜品对象和份额。deleteRecord(int orderNum)
方法:从桌子的订单中删除指定序号的点菜记录。getTotalPrice()
方法:计算桌子的订单总价格,并返回整数类型的总价。
- 构造函数
-
Main
类的main
方法:- 主要程序逻辑部分。
- 首先创建一个
Scanner
对象用于读取用户输入。 - 然后创建一个
Menu
对象用于存储菜单。 - 在读取菜单信息时,使用
Menu
对象的addDish
方法将菜品添加到菜单中。 - 接着依次读取桌号和订单信息,创建
Table
对象并添加到tables
列表中。 - 最后对
tables
列表进行排序,按照桌号从小到大输出每个桌子的总价,并根据时间判断是否在营业时间内,并计算不同时间段的折扣。
标签:总结,String,期中考试,double,orderNum,int,portion,pta4,public From: https://www.cnblogs.com/QiuBoHao/p/17517606.html