第四、五次题目集以及期中考试总结

第四、五次题目集以及期中考试总结

一 、题目集四

第一题

题目内容:菜单计价程序-4

**

import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        Restaurant res=new Restaurant();
        res.start();
    }
}
import java.util.ArrayList;

public class Menu
{
    private ArrayList<Dish> dishes=new ArrayList<>();

    public ArrayList<Dish> getDishes() {
        return dishes;
    }

    public void setDishes(ArrayList<Dish> dishes) {
        this.dishes = dishes;
    }

    Dish searchDish(String dishName)
    {
        for(Dish d:dishes)
        {
            if(dishName.equals(d.getName()))
            {
                return d;
            }
        }
        return null;
    }
    void addDish(Dish dish)
    {
        this.dishes.add(dish);
    }
}
public class Dish
{
    private String name;
    private int unitPrice;

    public Dish(String name, int unitPrice) {
        this.name = name;
        this.unitPrice = unitPrice;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(int unitPrice) {
        this.unitPrice = unitPrice;
    }
}
import java.util.Scanner;

public class Restaurant
{
    public void start()
    {
        Menu menu=new Menu();
        Scanner sc=new Scanner(System.in);

        String str=sc.nextLine();
        int num=sc.nextInt();
        do{
            Dish d=new Dish(str,num);
            menu.addDish(d);
            str=sc.nextLine();
            num=sc.nextInt();
        }while(!(str.equals("table")));//菜单内容输入
        TableList tableList=new TableList();
        {
            Order newOrder=new Order(num);
            String recordNum=sc.nextLine();
            String recordDishName=sc.nextLine();
            int recordPortion=sc.nextInt();
            int recordTimes=sc.nextInt();
            for(;recordNum!="end"&&recordNum!="table";)
            {
                if(recordDishName!="delete")
                {
                    int recordNumber=Integer.valueOf(recordNum);
                    Record newRecord=new Record(menu,recordNumber,recordDishName,recordPortion,recordTimes);
                    newOrder.addARecord(newRecord);
                }
            }
            tableList.add(newOrder);
        }
    }
}
import java.util.ArrayList;

public class TableList
{
    ArrayList<Order> tableList=new ArrayList<>();
    public void add(Order newOrder)
    {
        tableList.add(newOrder);
    }
}
import java.util.*;
import java.text.*;
public class todayDate {
    Calendar calendar;

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    public void setDate(String tableDate)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=dft.parse(tableDate);
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        this.calendar=cal;
    }
    public int returnWeek()
    {
        boolean isFirstSunday = (getCalendar().getFirstDayOfWeek() == Calendar.SUNDAY);//获取周几
        int weekDay = getCalendar().get(Calendar.DAY_OF_WEEK);//若一周第一天为星期天,则-1
        if(isFirstSunday){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }
        return weekDay;
    }

}
import java.util.*;
import java.text.*;
public class todayDate {
    Calendar calendar;

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    public void setDate(String tableDate)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=dft.parse(tableDate);
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        this.calendar=cal;
    }
    public int returnWeek()
    {
        boolean isFirstSunday = (getCalendar().getFirstDayOfWeek() == Calendar.SUNDAY);//获取周几
        int weekDay = getCalendar().get(Calendar.DAY_OF_WEEK);//若一周第一天为星期天,则-1
        if(isFirstSunday){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }
        return weekDay;
    }

}

public class Record
{
    private int orderNumber;
    private Dish dish;
    private int portion;
    private int times;



    public Record(Menu menu,int orderNumber, String dishName, int portion, int times)
    {
        Dish dish=null;
        for(Dish d:menu.getDishes())
        {
            if(d.getName().equals(dishName))
                dish=d;
        }
        this.orderNumber = orderNumber;
        this.dish = dish;
        this.portion = portion;
        this.times = times;
    }

    int getPrice()
    {
        if(this.portion==1)
            return dish.getUnitPrice()*this.times;
        else if(this.portion==2)
            return (int)(dish.getUnitPrice()*1.5*this.times);
        else
            return dish.getUnitPrice()*2*this.times;
    }
}
import java.util.ArrayList;

public class Order
{
    private ArrayList<Record> records=new ArrayList<>();
    int tableNum;

    public Order(int tableNum) {
        this.tableNum = tableNum;
    }

    public int getTableNum() {
        return tableNum;
    }

    public void setTableNum(int tableNum) {
        this.tableNum = tableNum;
    }

    int getTotalPrice()//计算订单的总价
    {
        int totalPrice=0;
        for(Record r:records)
        {
            totalPrice+=r.getPrice();
        }
        return totalPrice;
    }

    void addARecord(Record record)//添加一条菜品信息到订单中。
    {
        this.records.add(record);
    }

    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
    {

    }

    void findRecordByNum(int orderNum)//根据序号查找一条记录
    {

    }
}


总结:根据题目要求,我们需要设计一个用于计算并输出菜品总价格的订餐和定价程序。此程序包括以下类:菜肴类(Dish)、菜单类(Menu)、订购记录类(Record)和订单类(Order) 等四个类。下面是对这些类和整个程序的详细解释。 Dish类: Dish(菜肴)类表示一种菜肴,由名称(name)和单价(unitPrice)构成。我们定义了一个getPrice(计算价格)方法,该方法根据菜品的份量(port)返回菜品的价格。如果份量是1,则返回单价;如果份量是2,则返回1.5倍的单价;如果份量是3,则返回2倍的单价。 Menu类: Menu(菜单)类代表包含多种菜肴的菜单。此类由一个包含菜肴的ArrayList(ArrayList<Dish>)初始化。searchDish(查找菜肴)方法允许通过菜肴名称在菜单中查找与之匹配的菜肴。addDish(添加菜肴)方法将新菜肴添加到菜单中。 Record类: Record(订购记录)类表示一项餐厅订单中的一个菜品记录。该类由订单编号(orderNum)、菜肴(Dish)实例和份量(port)构成。我们定义了一个getPrice(获取价格)方法,该方法根据份量(port)返回菜品的价格。 Order类: Order(订单)类代表用户在特定时点创建的订购表。它由一个包含订购记录的ArrayList(ArrayList<Record>)初始化。getTotalPrice(获取总价格)方法计算并返回所有订购记录的总价格。addARecord(添加一条记录)方法向订购记录列表中添加一条新记录,同时搜索菜单以确保要添加的菜肴存在。delARecordByOrderNum(通过订单编号删除记录)允许根据订购记录编号移除记录。题中示例创建了一个Order类实例,向其中添加了几个订单,最终计算出整个订单的价格。

二 、题目集五

第一题

题目内容:菜单计价程序

import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        Restaurant res=new Restaurant();
        res.start();
    }
}
import java.util.ArrayList;

public class Menu
{
    private ArrayList<Dish> dishes=new ArrayList<>();

    public ArrayList<Dish> getDishes() {
        return dishes;
    }

    public void setDishes(ArrayList<Dish> dishes) {
        this.dishes = dishes;
    }

    Dish searchDish(String dishName)
    {
        for(Dish d:dishes)
        {
            if(dishName.equals(d.getName()))
            {
                return d;
            }
        }
        return null;
    }
    void addDish(Dish dish)
    {
        this.dishes.add(dish);
    }
}
public class Dish
{
    private String name;
    private int unitPrice;

    public Dish(String name, int unitPrice) {
        this.name = name;
        this.unitPrice = unitPrice;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(int unitPrice) {
        this.unitPrice = unitPrice;
    }
}
import java.util.Scanner;

public class Restaurant
{
    public void start()
    {
        Menu menu=new Menu();
        Scanner sc=new Scanner(System.in);

        String str=sc.nextLine();
        int num=sc.nextInt();
        do{
            Dish d=new Dish(str,num);
            menu.addDish(d);
            str=sc.nextLine();
            num=sc.nextInt();
        }while(!(str.equals("table")));//菜单内容输入
        TableList tableList=new TableList();
        {
            Order newOrder=new Order(num);
            String recordNum=sc.nextLine();
            String recordDishName=sc.nextLine();
            int recordPortion=sc.nextInt();
            int recordTimes=sc.nextInt();
            for(;recordNum!="end"&&recordNum!="table";)
            {
                if(recordDishName!="delete")
                {
                    int recordNumber=Integer.valueOf(recordNum);
                    Record newRecord=new Record(menu,recordNumber,recordDishName,recordPortion,recordTimes);
                    newOrder.addARecord(newRecord);
                }
            }
            tableList.add(newOrder);
        }
    }
}
import java.util.ArrayList;

public class TableList
{
    ArrayList<Order> tableList=new ArrayList<>();
    public void add(Order newOrder)
    {
        tableList.add(newOrder);
    }
}
import java.util.*;
import java.text.*;
public class todayDate {
    Calendar calendar;

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    public void setDate(String tableDate)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=dft.parse(tableDate);
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        this.calendar=cal;
    }
    public int returnWeek()
    {
        boolean isFirstSunday = (getCalendar().getFirstDayOfWeek() == Calendar.SUNDAY);//获取周几
        int weekDay = getCalendar().get(Calendar.DAY_OF_WEEK);//若一周第一天为星期天,则-1
        if(isFirstSunday){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }
        return weekDay;
    }

}
import java.util.*;
import java.text.*;
public class todayDate {
    Calendar calendar;

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    public void setDate(String tableDate)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=dft.parse(tableDate);
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        this.calendar=cal;
    }
    public int returnWeek()
    {
        boolean isFirstSunday = (getCalendar().getFirstDayOfWeek() == Calendar.SUNDAY);//获取周几
        int weekDay = getCalendar().get(Calendar.DAY_OF_WEEK);//若一周第一天为星期天,则-1
        if(isFirstSunday){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }
        return weekDay;
    }

}

public class Record
{
    private int orderNumber;
    private Dish dish;
    private int portion;
    private int times;



    public Record(Menu menu,int orderNumber, String dishName, int portion, int times)
    {
        Dish dish=null;
        for(Dish d:menu.getDishes())
        {
            if(d.getName().equals(dishName))
                dish=d;
        }
        this.orderNumber = orderNumber;
        this.dish = dish;
        this.portion = portion;
        this.times = times;
    }

    int getPrice()
    {
        if(this.portion==1)
            return dish.getUnitPrice()*this.times;
        else if(this.portion==2)
            return (int)(dish.getUnitPrice()*1.5*this.times);
        else
            return dish.getUnitPrice()*2*this.times;
    }
}
import java.util.ArrayList;

public class Order
{
    private ArrayList<Record> records=new ArrayList<>();
    int tableNum;

    public Order(int tableNum) {
        this.tableNum = tableNum;
    }

    public int getTableNum() {
        return tableNum;
    }

    public void setTableNum(int tableNum) {
        this.tableNum = tableNum;
    }

    int getTotalPrice()//计算订单的总价
    {
        int totalPrice=0;
        for(Record r:records)
        {
            totalPrice+=r.getPrice();
        }
        return totalPrice;
    }

    void addARecord(Record record)//添加一条菜品信息到订单中。
    {
        this.records.add(record);
    }

    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
    {

    }

    void findRecordByNum(int orderNum)//根据序号查找一条记录
    {

    }
}


总结:

功能 这个程序是一个订餐和定价系统,旨在帮助用户创建菜单、点餐并计算订单总价。用户可以方便地添加和删除菜品,系统会根据用户选择的菜品和份数计算每道菜的价格,最后将所有菜品的价格加起来,得出订单总价。

类与对象 本程序包含四个类:Dish(菜肴)、Menu(菜单)、Record(订购记录)和Order(订单)。这些类分别代表了餐厅订单系统中的不同实体,各自执行特定功能,完成订餐和定价任务。

程序结构 程序使用面向对象编程的设计思想,使用类和对象分别表示系统中的各个组件。通过嵌套不同类的对象,使程序更易于理解和维护。主要通过创建和操作Dish、Menu、Record和Order对象来驱动整个程序。

代码优化和扩展 目前的程序仅实现了基本功能,并未涉及到输入输出的处理,以及异常处理等。根据实际应用场景,可以对代码进行优化,例如添加输入输出处理、菜品种类和属性的扩展、订单信息的持久化存储等。同时,也可以考虑对订单系统进行升级,加入会员功能、优惠策略等。

总结:本程序是一个简化的订餐和定价系统,它采用面向对象编程设计,包含四个不同的类和相应的对象。程序功能清晰明了,易于理解和维护。虽然仅实现基本功能,但可根据实际需求进行扩展和优化。核心是理解订餐和定价系统的设计思路,根据具体应用场景进行调整。

三 、期中考试

第一题

题目内容:圆类设计

import java.util.Scanner;

class Circle {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return Math.PI * Math.pow(radius, 2);
    }
}

public class CircleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入圆的半径: ");
        if (scanner.hasNextDouble()) {
            double radius = scanner.nextDouble();
            if (radius > 0) {
                Circle circle = new Circle(radius);
                double area = circle.getArea();
                System.out.println(String.format("圆的面积为: %.2f", area));
            } else {
                System.out.println("Wrong Format");
            }
        } else {
            System.out.println("Wrong Format");
        }
        scanner.close();
    }
}



总结:在这个Java程序中,我们创建了一个名为Circle的类,它有一个私有属性radius(半径),用于表示圆的半径。接下来,我们定义了一个名为getArea()的方法,该方法会返回圆的面积。 在CircleArea类的main方法中,我们首先使用Scanner类从控制台获取用户输入。用户需要输入一个表示圆半径的数值。我们首先检查输入值是否为double类型,如果是,继续判断输入值是否大于0。如果输入值满足要求,我们创建一个Circle类的实例,并将半径值传递给它。然后,我们调用getArea()方法计算圆的面积,并使用String.format()函数将结果保留两位小数。最后,输出圆的面积。 如果输入的数值为负数或非数值,则程序会输出"Wrong Format",表示输入的格式错误。

第二题

题目内容:类结构设计

class Coordinate {
    private double x;
    private double y;

    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}

class Rectangle {
    private Coordinate topLeft;
    private Coordinate bottomRight;

    public Rectangle(Coordinate topLeft, Coordinate bottomRight) {
        this.topLeft = topLeft;
        this.bottomRight = bottomRight;
    }

    public double getArea() {
        double width = Math.abs(bottomRight.getX() - topLeft.getX());
        double height = Math.abs(topLeft.getY() - bottomRight.getY());
        return width * height;
    }
}

public class RectangleArea {
    public static void main(String[] args) {
        Coordinate topLeft = new Coordinate(6, 5.8);
        Coordinate bottomRight = new Coordinate(-7, 8.9);

        Rectangle rectangle = new Rectangle(topLeft, bottomRight);
        double area = rectangle.getArea();
        System.out.println(String.format("矩形的面积为: %.2f", area));
    }
}


总结:在这个Java程序中,首先创建了一个名为Coordinate的坐标类,它包含两个私有属性x和y,分别代表坐标轴上的X和Y轴的值。接着,我们为Coordinate类定义了获取坐标值的方法getX()和getY()。 然后我们设计了一个名为Rectangle的矩形类。这个类由两个私有属性组成,分别是矩形的左上角和右下角坐标点。我们创建了一个构造函数,用来接收左上角和右下角的坐标点。 我们为Rectangle类添加了一个名为getArea()的方法,用于计算矩形的面积。在该方法中,首先计算宽度(右下角点的X坐标 - 左上角点的X坐标)和高度(左上角点的Y坐标 - 右下角点的Y坐标),然后将宽度和高度相乘得到矩形的面积。 在程序的main方法中,我们创建了表示左上角点和右下角点的Coordinate类实例。然后,根据这两个坐标点创建了一个Rectangle类实例。再调用getArea()方法计算矩形的面积,并保留两位小数进行输出。 示例输入: 6 5.8 -7 8.9 示例输出: 40.30

第三题

题目内容:继承与多态

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 * Math.pow(radius, 2);
    }
}

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 topLeft;
    private Point bottomRight;

    public Rectangle(Point topLeft, Point bottomRight) {
        this.topLeft = topLeft;
        this.bottomRight = bottomRight;
    }

    @Override
    public double getArea() {
        double width = Math.abs(bottomRight.getX() - topLeft.getX());
        double height = Math.abs(topLeft.getY() - bottomRight.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 = input.nextDouble();
                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 void printArea(Shape shape) {
        System.out.printf("图形的面积为: %.2f%n", shape.getArea());
    }
}


总结:在这个Java程序中,我们首先创建了一个名为Shape的抽象类。这个类包含了计算形状面积的抽象方法getArea()

接着,我们创建了Circle类和Rectangle类作为Shape类的子类。我们已经在前面的设计中定义了它们的属性和方法。我们需要在这两个类中添加@Override注解,以覆盖抽象类中的getArea()方法。

主方法main()中,我们根据输入的选择(1或2),分别创建一个Circle类实例或Rectangle类实例。然后,将创建的形状实例作为参数传递给主类中定义的printArea()静态方法。printArea()方法输出形状的面积,保留两位小数。这里展示了程序设计的多态性。

第四题

题目内容:抽象类与接口

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();
                Shape circle = new Circle(radiums);
                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();
        }    

        list.sort(Comparator.naturalOrder());//正向排序

        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }    
    }    
}


总结:在这个Java程序中,我们首先创建了一个名为Shape的抽象类。这个类包含了计算形状面积的抽象方法getArea()

接着,我们创建了Circle类和Rectangle类作为Shape类的子类。我们已经在前面的设计中定义了它们的属性和方法。我们需要在这两个类中添加@Override注解,以覆盖抽象类中的getArea()方法。

主方法main()中,我们根据输入的选择(1或2),分别创建一个Circle类实例或Rectangle类实例。然后,将创建的形状实例作为参数传递给主类中定义的printArea()静态方法。printArea()方法输出形状的面积,保留两位小数。这里展示了程序设计的多态性。

posted @ 2023-06-30 22:25  arcticquality  阅读(84)  评论(0)    收藏  举报