首页 > 其他分享 >pta4,5及期中考试总结

pta4,5及期中考试总结

时间:2023-06-30 21:23:27浏览次数:59  
标签:总结 String 期中考试 double orderNum int portion pta4 public

(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);
        }
    }
}

 

  1. Dish类:

    • 构造函数 Dish(String name, int unit_price):用于初始化菜品对象,并设置菜品的名称和单价。
    • getPrice(int portion)方法:根据所选份额计算菜品的价格,并返回整数类型的价格。
  2. Menu类:

    • 构造函数 Menu():用于初始化菜单对象,创建一个空的菜品列表。
    • addDish(String name, int unit_price)方法:向菜单中添加一个菜品,传入菜品的名称和单价。
    • searchDish(String dishName)方法:根据菜品名称在菜单中查找并返回对应的菜品对象。
  3. Record类:

    • 构造函数 Record(int orderNum, Dish dish, int portion):用于初始化点菜记录对象,设置序号、菜品和份额。
    • getPrice()方法:根据点菜记录中的菜品和份额计算该记录的价格,并返回整数类型的价格。
  4. Order类:

    • 构造函数 Order():用于初始化订单对象,创建一个空的点菜记录列表。
    • addRecord(int orderNum, Dish dish, int portion)方法:向订单中添加一条点菜记录,传入订单号、菜品对象和份额。
    • deleteRecord(int orderNum)方法:从订单中删除指定序号的点菜记录。
    • findRecordByNum(int orderNum)方法:根据序号在订单中查找并返回对应的点菜记录对象。
    • getTotalPrice()方法:计算整个订单的总价格,并返回整数类型的总价。
  5. Table类:

    • 构造函数 Table(String tableNum, String time):用于初始化桌子对象,设置桌号和时间。
    • addRecord(int orderNum, Dish dish, int portion)方法:向桌子的订单中添加一条点菜记录,传入订单号、菜品对象和份额。
    • deleteRecord(int orderNum)方法:从桌子的订单中删除指定序号的点菜记录。
    • getTotalPrice()方法:计算桌子的订单总价格,并返回整数类型的总价。
  6. 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

相关文章

  • PTA4,5及期中总结
    1,前言PTA4,5还是继续之前的菜单计价程序,只是在1,2,3的基础上加以完善,增加了更多的新的功能,期中考试则是在测试考核面向对象及接口,继承和多态以及抽象类等等知识点、2,设计与分析 菜单计价程序-4 类图   第一次的课程成绩统计程序较为直观,题目要求中直接给了相关的类图,......
  • PTA 4,5题目集及期中考试总结
    PTA4,5题目集及期中考试总结前言第4次题目集知识点:对象和类的创建和应用,字符串的创建和应用。总共有1题,难度偏高。第5次题目集知识点:对象和类的创建和应用,字符串的创建和应用。总共有1题,难度偏高。期中考试知识点:字符的处理,类的封装,接口的创建和使用。总共有4题,难度偏低......
  • export,export default,exports - 导入导出方法总结
    1.export.default的使用方法特点:export.default向外暴露的成员,可以使用任意变量来接收在一个模块中,exportdefault只允许向外暴露一次在一个模块中,可以同时使用exportdefault和export向外暴露成员//exportdefault-默认导出constm=100;exportdefaultm;//导入......
  • 4-5次PTA题目总结blog
    前言:题目集1~3的知识点、题量、难度等情况如下:知识点:JAVA基础,基础算法,面向对象程序设计题量:共计2道题目难度:题目从易到难,逐层递进,分别为考察Java各个类的理解、掌握与运用。设计与分析:1importjava.text.ParseException;2importjava.time.DateTimeExce......
  • Java PTA第4~5次题目集总结以及期中考试总结
    一.前言1.第四次题目集的知识点涉及Time类以及前面学的各种知识点;题量很少只有一题;难度比较大。2.第五次题目集的知识点主要是Time类、异常处理等等;题量很少只有一题;难度比较大。3.期中考试的知识点涉及类、继承与多态、接口等等;题量不多,一共4题;整体难度不高。二.设计与分析7......
  • 第二周总结
    本周完成内容1.pta完成1000分的题目2.读完了大道至简并且写了相应的读后感3.完成了Java相关软件的安装和环境配置4.学习了一部分java的语法5.下载完成数据库MySQL以及环境配置6.初步学习了一点数据库相关内容7.算法学习了几节,但是又感觉应该先学习数据结构,遇到的问题就是学......
  • 日期处理总结
    1.日期处理1.1引入必要依赖:版本5.3.8 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency> <de......
  • 第一周进度总结
    第一周我开始了自学Java,通过B站黑马程序员up主的教学视频,我学习了Java基础与Javaweb的课程。目前,我Java基础学到了P9-Notepad的安装与使用,Javaweb学到了P4-HTML简述-Hbuilder的使用。同时我在电脑上已经安装Java环境,用Javac成功编译HelloWorld.java。下周开始,我将开始英语4级的准......
  • 跨端之小程序面试题总结
    微信小程序的相关文件类型WXML(WeiXinMarkupLanguage)是框架设计的一套标签语言,结合基础组件、事件系统,可以构建出页面的结构。内部主要是微信自己定义的一套组件WXSS(WeiXinStyleSheets)是一套样式语言,用于描述WXML的组件样式js逻辑处理,网络请求json小程序设置,如页面......
  • MySQL内存使用率高且不释放问题排查与总结
    一、内存使用率高且不释放问题排查生产环境MySQL5.7数据库告警内存使用率95%。排查MySQL内存占用问题的思路方法可以参考叶老师这篇文章:https://mp.weixin.qq.com/s/VneUUnprxzRGAyQNaKi-7g。TOP命令查看MySQL进程的RES指标,发现内存使用了10.6G,而数据库的innodb_buffer_pool_si......