首页 > 其他分享 >4、5及期中考试总结

4、5及期中考试总结

时间:2023-06-30 10:00:34浏览次数:51  
标签:总结 return 期中考试 double int Dish new public

一.前言

Java编程语言是当今最流行的编程语言之一,由于其跨平台性、面向对象性和安全性等特点,受到广泛的应用。
作为一名计算机专业的学生,在学习Java编程语言时,我们需要完成多个作业来巩固所学知识。
在前三次Java作业中,我们已经学习了Java的基础知识和常用技术,通过完成这些作业,我们更深入地了解了Java编程语言的特点和使用方法。

下面对这三次作业做一个简单的总结:

1.知识点:

 进一步掌握java中类,抽象类及接口的使用,继续完化菜单系列题目。

2.题量与难度:

 题量适中,菜单系统外的题目难度适中。对于菜单系统系列题目难度较大,已经无法独自完成。

3.题目:

实验四:菜单计价程序-4

 实验五:菜单计价程序-5

 期中: 测验1-圆类设计, 测验2-类结构设计,测验3-继承与多态,测验4-抽象类与接口。

二.设计与分析

1.实验四

7-1 菜单计价程序-4

在菜单3上进行的改进:

本次课题比菜单计价系列-3增加的异常情况:

1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:"invalid dish"

2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"

3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。

4、重复删除,重复的删除记录输出"deduplication :"+序号。

5、代点菜时,桌号不存在,输出"Table number :"+被点菜桌号+" does not exist";本次作业不考虑两桌记录时间不匹配的情况。

6、菜谱信息中出现重复的菜品名,以最后一条记录为准。

7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。

8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。

9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。

10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。

12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:"not a valid time period"

14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。

15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:"record serial number sequence error"。当前记录忽略。(代点菜信息的序号除外)

16、所有记录其它非法格式输入,统一输出"wrong format"

17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。

本次作业比菜单计价系列-3增加的功能:

菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+"T"

例如:麻婆豆腐 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

代码如下

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);
        Dish[] dish = new Dish[10];
        String[] command = in.nextLine().split(" ");
        for(int i = 0; !command[0].equals("table"); i++) {
            if(isNumeric(command[1])) {
                if(command.length > 2) {
                    if(command.length < 4)
                        dish[i] = new Dish(command[0],Integer.parseInt(command[1]),true);
                    else {
                        System.out.println("wrong format");
                        System.exit(0);
                    }        
                }
                else 
                    dish[i] = new Dish(command[0],Integer.parseInt(command[1]),false);
                if(!(dish[i].getUtilPrice() > 0&&dish[i].getUtilPrice() < 300)) {
                    System.out.println(dish[i].getName()+" price out of range "+dish[i].getUtilPrice());
                    dish[i] = new Dish();
                }
                
            }
            else {
                dish[i] = new Dish();
                System.out.println("wrong format");
            }
            
            command = in.nextLine().split(" ");
        }
        Menu menu = new Menu(dish);
        if(isTable(command)) {
            Table[] table = new Table[10];
            int tableNum =0;
            for(tableNum = 0; command[0].equals("table"); tableNum++) {
                table[tableNum] = new Table(Integer.parseInt(command[1]),command[2],command[3]);
                System.out.println("table "+table[tableNum].getNum()+": ");
                Record[] records = new Record[10];
                command = in.nextLine().split(" ");
                for(int j = 0; !(command[1].equals("delete")||isTable(command)); j++) {
                    if(command[0].length() > 2) {
                        if(command.length > 3)
                            records[j] = new Record(1,new Dish(),0,0);
                        else
                            System.out.println("invalid dish");
                    }
                    else {
                        if(menu.searthDish(command[1]).getName().equals("notexist")) {
                            System.out.println(command[1]+" does not exist");
                            records[j] = new Record();
                        }
                        else {                        
                                records[j] = new Record(Integer.parseInt(command[0]),menu.searthDish(command[1]),Integer.parseInt(command[2]),Integer.parseInt(command[3]));
                        }        
                    }
                    command = in.nextLine().split(" ");
                    if(command[0].equals("end"))
                        break;
                }
                Order order = new Order(records,menu);
                table[tableNum].setOrder(order);
                order.showOrder();
                if(!command[0].equals("end")) {
                    while(command[1].equals("delete")) {
                        order.delARecordByOrderNum(Integer.parseInt(command[0]));
                        command = in.nextLine().split(" ");
                        if(command[0].equals("end"))
                            break;
                        }
                    }
            
                }
            for(int i = 0; i < tableNum; i++) {
                table[i].showTotal();
            }
        }
        else
            System.out.println("wrong format");
    }
    public static boolean isTable(String[] table ) {
        if(table.length < 5) {
            if(table[0].equals("table")) {
                if(isNumeric(table[1])) {
                    if(table[3].length() == 8)
                        return true;
                    else
                        return false;
                }
                else
                    return false;                    
            }
            else
                return false;
        }
        else
            return false;
    }
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
}
class Dish{
    private String name;
    private int util_price;
    private boolean t;
    
    public Dish() {
        name = "";
        util_price = 0;
        t = false;
    }
    public Dish(String name,int util_price,boolean t){
        this.name = name;
        this.util_price = util_price;
        this.t = t;
    }
    public String getName() {
        return name;
    }
    public int getUtilPrice() {
        return util_price;
    }
    public boolean getT() {
        return t;
    }
    public int getPrice(int portion){
        double price = 0;
        switch(portion){
            case 1:{
                price = util_price;
                break;
            }
            case 2:{
                price = util_price * 1.5;
                break;
            }
            case 3:{
                price = util_price * 2;
                break;
            }
        }
        if(price - (int)price >= 0.5) 
            return (int)price + 1;
        else
            return (int)price;
    }
}

class Menu{
    private Dish[] dishs;
    
    public Menu(Dish[] dishs) {
        this.dishs = new Dish[dishs.length];
        for(int i = 0; i < dishs.length; i++) {
            if(dishs[i] == null)
                this.dishs[i] = new Dish();
            else
                this.dishs[i] = dishs[i];
        }
    }
    public Dish searthDish(String dishName) {
        int num = 0;
        Dish flag = new Dish("notexist",0,false); 
        for(int i = 0; i < dishs.length; i++) {
            if(dishs[i].getName().equals(dishName)) {
                num = i;
                flag = new Dish("exist",0,false); 
            }        
        }
        if(flag.getName().equals("exist"))
            return dishs[num];
        else
            return flag;
    }
    public Dish addDish(String dishName,int unit_price,boolean t) {
        return new Dish(dishName,unit_price,t);
    }
}
 class Record{
     private int orderNum;
     private Dish d;
     private int portion;
     private int num;
     
     public Record() {
         orderNum = 0;
         d = new Dish();
         portion = 0;
         num = 0;
     }
     public Record(int orderNum,Dish d,int portion,int num) {
         this.orderNum = orderNum;
         this.d = d;
         this.portion = portion;
         this.num = num;
     }
     public int getOrderNum(){
         return orderNum;
     }
     public Dish getDish() {
         return d;
     }
     public int getPortion() {
         return portion;
     }
     public int getNum() {
         return num;
     }
     public int getPrice() {
         if(!d.getT()&&(portion < 0||portion > 3))
             return 0;
         else if(d.getT()&&(portion < 0||portion > 3))
             return 0;
         else {
             if(num > 15)
                 return 0;
             else
                 return d.getPrice(portion)*num;
         }
     }
     public void Priceshow() {
         if(portion < 10) {
             if(!d.getT()&&(portion < 0||portion > 3))
                 System.out.println(orderNum+" num out of range "+portion);
             else if(d.getT()&&(portion < 0||portion > 3))
                 System.out.println(orderNum+" portion out of range "+portion);
             else {
                 if(num > 15)
                     System.out.println(orderNum+" num out of range "+num);
                 else
                     System.out.println(orderNum+" "+d.getName()+" "+getPrice());
             }
         }
         else
             System.out.println("wrong format");
         
     }
 }

class Order{
    private Record[] records;
    private Menu menu;
    
    public Order(Record[] records,Menu menu) {
        this.records = new Record[records.length];
        for(int i = 0; i < records.length; i++)
            if(records[i] == null)
                this.records[i] = new Record();
            else
                this.records[i] = records[i];
        this.menu = menu;
    }
    public Record[] getRecords() {
        return records;
    }
    public int getTotalPrice() {
        int TotalPrice = 0;
        for(int i = 0; i < records.length; i++) {
            TotalPrice += records[i].getPrice();
        }
        return TotalPrice;
    }
    public Record addARecord(int orderNum,String dishName,int portion,int num) {
        Dish d = menu.searthDish(dishName);
        Record error = new Record();
        if(d.getName().equals(""))
            return error;
        else
            return new Record(orderNum,d,portion,num);
    }
    public void delARecordByOrderNum(int orderNum) {
        int flag = findRecordByNum(orderNum);
        if(flag != -1) {
            if(records[flag].getNum() == 0)
                System.out.println("deduplication "+ orderNum);
            records[flag] = new Record(orderNum,new Dish(),0,0);
        }
        else
            System.out.println("delete error");
    }
    public int findRecordByNum(int orderNum) {
        for(int i = 0; i < records.length; i++) {
            if(records[i].getOrderNum() == orderNum) {
                return i;
            }
        }
        return -1;
    }
    public void showOrder() {
        for(int i = 0; i < records.length; i++) {
            if(records[i].getNum() != 0) {
                int m = i;
                for(m = i; m > 0; m--) {
                    if(records[m-1].getOrderNum() >= records[i].getOrderNum()) {
                        System.out.println("record serial number sequence error");
                        records[i] = new Record();
                        break;
                    }
                }
                if(m == 0)
                    records[i].Priceshow();
            }
            else {
                if(records[i].getOrderNum() == 1) 
                    System.out.println("wrong format");
            }
        }
    }
}
class Table {
    private int num;
    private Order order;
    private LocalDate date;
    private LocalTime time;
    private double total;
    
    public Table(int num,String date,String time) {
        this.num = num;
        if(check(date)) {
            String[] DATE = date.split("/");
            this.date = LocalDate.of(Integer.parseInt(DATE[0]),Integer.parseInt(DATE[1]),Integer.parseInt(DATE[2]));
        }
        else
            this.date = LocalDate.of(1,1,1);
        String[] TIME = time.split("/");
        this.time = LocalTime.of(Integer.parseInt(TIME[0]),Integer.parseInt(TIME[1]),Integer.parseInt(TIME[2]));
        this.total = 0.0;
    }
    public void setOrder(Order order) {
        this.order = order;
    }
    public int getNum() {
        return num;
    }
    public Order getOrder() {
        return order;
    }
    public LocalDate getDate() {
        return date;
    }
    public LocalTime getTime() {
        return time;
    }
    public double getTotal() {
        return total;
    }
    public int getWeek() {
        return date.get(ChronoField.DAY_OF_WEEK);
    }
    public int OpeningHours(int week,LocalTime time,boolean T) {
        if(week <= 5) {
            if(T) {
                return 7;
            }
            else {
                if(time.isAfter(LocalTime.of(16,59,59))&&time.isBefore(LocalTime.of(20,30,1)))
                    return 8;
                else if(time.isAfter(LocalTime.of(10,29,59))&&time.isBefore(LocalTime.of(14,30,1)))
                    return 6;
                else
                    return -1;
            }
            
        }
        else {
            if(time.isAfter(LocalTime.of(9,30,0))&&time.isBefore(LocalTime.of(21,30,0)))
                return 10;
            else
                return -1;
        }
    }
    public int getDiscountedPrice() {
        int discountedPrice = 0;
        Record[] records = order.getRecords();
        double[] price = new double[records.length];
        for(int i = 0; i < records.length; i++) {
            price[i] = records[i].getPrice()*OpeningHours(getWeek(),getTime(),records[i].getDish().getT())*0.1;
            if(price[i] - (int)price[i] >= 0.5) 
                price[i] = (int)price[i] + 1;
            else
                price[i] = (int)price[i];
        }
        for(int i = 0; i < price.length; i++)
            discountedPrice += (int)price[i];
        return discountedPrice;
    }
    public void showTotal() {
        int judge = OpeningHours(getWeek(),getTime(),false);
        if(judge == -1)
            System.out.println("table"+" "+num+"out of opening hours");
        else {
            if(date.isAfter(LocalDate.of(2020,1,1))&&date.isBefore(LocalDate.of(2023,12,31)))
                System.out.println("table"+" "+num+": "+order.getTotalPrice()+" "+getDiscountedPrice());
            else
                System.out.println("not a valid time period");
        }
    }
    public static boolean check (String str) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
        try {
            sd.setLenient(false);
            sd.parse(str);
        }
        catch (Exception e) {
            return false;
        }
        return true;
    }
}

类图如下

 基础类的分析

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish[] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)

int getPrice()//计价,计算本条记录的价格

}

订单类:保存用户点的所有菜的信息。

Order {

Record[] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

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

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

}

2.实验五

7-1 菜单计价程序-5

 在实验三的基础上对功能的增加:

1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

输出用户支付金额格式:

用户姓名+英文空格+手机号+英文空格+支付金额

代码如下

import java.util.*;

// 菜品类
class Dish {
    String name; // 菜名
    double unitPrice; // 单价

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

    double getPrice(int portion) {
        double price = unitPrice;
        if (portion == 2) {
            price *= 1.5;
        } else if (portion == 3) {
            price *= 2;
        }
        return Math.round(price);
    }
}

// 菜谱类
class Menu {
    List<Dish> dishes; // 菜品列表

    public Menu() {
        dishes = new ArrayList<>();
    }

    Dish searchDish(String dishName) { // 根据菜名在菜谱中查找菜品
        for (Dish dish : dishes) {
            if (dish.name.equals(dishName)) {
                return dish;
            }
        }
        return null;
    }

    Dish addDish(String dishName, double unitPrice) { // 添加菜品到菜谱
        Dish existingDish = searchDish(dishName);
        if (existingDish != null) {
            existingDish.unitPrice = unitPrice;
            return existingDish;
        } else {
            Dish newDish = new Dish(dishName, unitPrice);
            dishes.add(newDish);
            return newDish;
        }
    }
}

// 订单记录类
class OrderRecord {
    int orderNum; // 序号
    Dish dish; // 菜品
    int portion; // 份额

    public OrderRecord(int orderNum, Dish dish, int portion) {
        this.orderNum = orderNum;
        this.dish = dish;
        this.portion = portion;
    }

    double getPrice() { // 计算菜品价格
        return dish.getPrice(portion);
    }
}

// 订单类
class Order {
    List<OrderRecord> records; // 订单记录列表

    public Order() {
        records = new ArrayList<>();
    }

    double getTotalPrice() { // 计算订单的总价
        double totalPrice = 0;
        for (OrderRecord record : records) {
            totalPrice += record.getPrice();
        }
        return Math.round(totalPrice);
    }

    OrderRecord addRecord(int orderNum, String dishName, int portion) { // 添加订单记录
        Dish dish = Menu.searchDish(dishName);
        if (dish == null) {
            System.out.println("** " + dishName + " does not exist");
            return null;
        }
        OrderRecord record = new OrderRecord(orderNum, dish, portion);
        records.add(record);
        return record;
    }

    void removeRecord(int orderNum) { // 根据序号删除订单记录
        OrderRecord record = findRecordByNum(orderNum);
        if (record == null) {
            System.out.println("delete error");
            return;
        }
        records.remove(record);
    }

    OrderRecord findRecordByNum(int orderNum) { // 根据序号查找订单记录
        for (OrderRecord record : records) {
            if (record.orderNum == orderNum) {
                return record;
            }
        }
        return null;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        Menu menu = new Menu(); // 创建菜谱
        Order order = new Order(); // 创建订单

        while (true) {
            String input = scanner.nextLine();
            if (input.equals("end")) {
                break;
            }

            String[] parts = input.split(" ");
            if (parts[0].startsWith("table")) { // 处理桌号标识
                // 处理桌号标识
            } else if (parts.length == 2) { // 处理菜单记录
                String dishName = parts[0];
                double unitPrice = Double.parseDouble(parts[1]);
                menu.addDish(dishName, unitPrice);
            } else if (parts.length == 4) { // 处理点菜记录
                int orderNum = Integer.parseInt(parts[0]);
                String dishName = parts[1];
                int portion = Integer.parseInt(parts[2]);
                order.addRecord(orderNum, dishName, portion);
            } else if (parts.length == 2 && parts[1].equals("delete")) { // 处理删除记录
                int orderNum = Integer.parseInt(parts[0]);
                order.removeRecord(orderNum);
            }
        }

        for (OrderRecord record : order.records) {
            System.out.println(record.orderNum + " " + record.dish.name + " " + record.getPrice());
        }

        System.out.println("Total price: " + order.getTotalPrice());

        scanner.close();
    }
}

类图如下

新知识点:Math.round(),split(),

    1.Math.round()可以简单的理解为四舍五入函数,在负数的情况下0.5不进位。

    2. split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回;

    3.Integer.parseInt(String)就是将String字符类型数据转换为Integer整型数据,如果遇到不能转换的字符则会抛出异常!简而言之,这个代码就是用来把任何进制的数据转化成10进制的数据。

    (1)parseInt(String s)

      将字符串s转换为十进制的数字,默认为十进制

    (2)parseInt(String s,int radix)

      radix代表转换的进制,不写默认为十进制

 

3.期中

7-1 测验1-圆类设计

代码如下

import 期中.Point;

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double x1 = in.nextDouble();
double y1 = in.nextDouble();
double x2 = in.nextDouble();
double y2= in.nextDouble();
Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
double getLength = x2 - x1;
double getHeight = y1 - y2;
double getArea = getLength * getHeight;
System.out.println(String.format("%.2f",getArea));


}
}

新知识点:Math类

在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数、对数、平方根和三角函数等。Math 类位于 java.lang 包,它的构造方法是 private 的,因此无法创建 Math 类的对象,并且 Math 类中的所有方法都是类方法,可以直接通过类名来调用它们。

1.静态常量:

Math 类中包含 E 和 PI 两个静态常量,正如它们名字所暗示的,它们的值分别等于 e(自然对数)和 π(圆周率)。

调用 Math 类的 E 和 PI 两个常量,并将结果输出。代码如下:


  1. System.out.println("E 常量的值:" + Math.E);
  2. System.out.println("PI 常量的值:" + Math.PI);

2.求最大值、最小值和绝对值

在程序中常见的就是求最大值、最小值和绝对值问题,如果使用 Math 类提供的方法可以很容易实现。这些方法的说明如表 1 所示。

 
表 1 求最大值、最小值和绝对值的方法
方法说明
static int abs(int a) 返回 a 的绝对值
static long abs(long a) 返回 a 的绝对值
static float abs(float a) 返回 a 的绝对值
static double abs(double a) 返回 a 的绝对值
static int max(int x,int y) 返回 x 和 y 中的最大值
static double max(double x,double y) 返回 x 和 y 中的最大值
static long max(long x,long y) 返回 x 和 y 中的最大值
static float max(float x,float y) 返回 x 和 y 中的最大值
static int min(int x,int y) 返回 x 和 y 中的最小值
static long min(long x,long y) 返回 x 和 y 中的最小值
static double min(double x,double y) 返回 x 和 y 中的最小值
static float min(float x,float y) 返回 x 和 y 中的最小值

3.求整运算

 

表 2 取整方法及其说明
方法说明
static double ceil(double a) 返回大于或等于 a 的最小整数
static double floor(double a) 返回小于或等于 a 的最大整数
static double rint(double a) 返回最接近 a 的整数值,如果有两个同样接近的整数,则结果取偶数
static int round(float a) 将参数加上 1/2 后返回与参数最近的整数
static long round(double a) 将参数加上 1/2 后返回与参数最近的整数,然后强制转换为长整型

 

7-2 测验2-类结构设计

代码如下

import java.util.Scanner;

public class Circle {
    private double radius;
    
    public Circle() {
        // 默认构造函数
    }
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    public double getRadius() {
        return radius;
    }
    
    public void setRadius(double radius) {
        this.radius = radius;
    }
    
    public double calculateArea() {
        return Math.PI * radius * radius;
    }
    
    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.calculateArea();
            System.out.println(String.format("%.2f", area));
        }
        
        scanner.close();
    }
}

7-3 测验3-继承与多态

代码如下

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        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;
        }
        
    }
public class Circle {
    private double radius;
    
    public Circle() {
        // 默认构造函数
    }
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    public double getRadius() {
        return radius;
    }
    
    public void setRadius(double radius) {
        this.radius = radius;
    }
    
    public double calculateArea() {
        return Math.PI * radius * radius;
    }
    
    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.calculateArea();
            System.out.println(String.format("%.2f", area));
        }
        
        scanner.close();
    }
}


7-4 测验4-抽象类与接口

代码如下

abstract class Shape implements Comparable {
    public Shape() {
    }

    public abstract double getArea();
}
interface Comparable {
}

class Point {
    double x,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;
    }
}
class Rectangle extends Shape {
    Point topLeftPoint = new Point();
    Point lowerRightPoint = new Point();

    public Rectangle() {
    }

    @Override
    public double getArea() {
        double Area = Length * Height;
        return Area;
    }



    public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowerRightPoint = lowerRightPoint;
    }

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public Point getLowerRightPoint() {
        return lowerRightPoint;
    }

    public void setLowerRightPoint(Point lowerRightPoint) {
        this.lowerRightPoint = lowerRightPoint;
    }
   double Length;
    double Height;



}
abstract class Shape implements Comparable {
    public Shape() {
    }

    public abstract double getArea();
}
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                double Area  = 3.14159 * radiums * radiums;
                System.out.println(String.format("%.2f",Area));

                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);
                double getLength = x2 - x1;
                double getHeight = y1 - y2;
                double getArea = getLength * getHeight;
                System.out.println(String.format("%.2f",getArea));


                break;

        }

    }

    private static void printArea(Shape shape) {
        shape.getArea();
    }
}

三.错误与分析

 7-1 测验1-圆类设计

在计算圆的面积时,PI刚开始一直都在用数字来尝试,一直都没有得到正确答案,考试结束之后才知道Math.PI。

四.总结

    1.难度逐渐上升,需要熟练的掌握Java中类的使用及Object和String类中一些方法的使用。其中,最主要的是对类的使用,如类的继承、封装和多态性等。更加深入地理解面向对象的编程思想,熟悉Java编程语言的类和对象的使用方法。重点是对Javabean的基本使用,类的广泛应用。通过这些练习,我们可以更深入地掌握Java编程语言的常用技术,如数据结构、输入输出、异常处理等。对于菜单系统已经无法独自完成题目要求。

 

五.改进建议

  逻辑有些混乱,不能理清思路导致不能流畅地写出代码,并且使用的算法较为复杂,有些题目是可以用到更简单的方法,有许多简单易懂的算法和思路。

标签:总结,return,期中考试,double,int,Dish,new,public
From: https://www.cnblogs.com/lsy--/p/17514490.html

相关文章

  • Three.js教程:threejs语法总结
    推荐:将NSDT场景编辑器加入你的3D工具链其他系列工具:NSDT简石数字孪生threejs语法总结本节课从JavaScript面向对象语法的角度,给大家总结下threejsAPI的使用习惯,这样方便大家更好的使用threejsAPI。Three.js语法总结:类(构造函数)Three.js提供了各种各样的类(构造函数),通过ne......
  • 每日总结2023年6月29日
    今日学习:数据库夏季学期课程了解以及课题选择(快递管理系统)磁盘:SSD不是磁盘,机械硬盘是磁盘,磁盘盘面保存数据,磁盘时用磁头在盘上磁化出一个一个的小磁铁来记录信息(生成磁道,一个磁道上有许多扇区),磁盘的存取时间=寻道时间+等待时间(平均定位时间+转动延迟)下面是例题计算机总线分为:外......
  • 20230629习题总结
    1.QOJ6513EXPRESSION3考虑每个数正负状态的贡献,维护优先级后缀最小值。多项式部分不清晰。2.CF335FBuyOne,GetOneFree因为有重复的价格,贪心不成立,考虑反悔,反悔时不直接删去而是加入一个可撤销的等价于返回的操作,便于反悔之前的反悔。3.QOJ6178区间子集问题两个区间要......
  • 助教工作总结(计算机网络22级专升本)
    一、助教工作的具体职责和任务(1)协助并分担老师的工作,在线上或者线下对学生不理解的问题进行一定的指导以及答疑解答。(2)跟学生多聊天,了解一下老师的讲课进度是否过快,学生是否充分理解,收集并且分析后,跟老师进行一个反馈。二、助教工作的每周时长和具体安排(1)平常时段的7:00~23:00......
  • 每周总结1
    此篇是暑假每周总结的第一篇,下面简单总结一下这周所干的事情吧!刚回到家,比较浮躁,学习也没有心情,《大道至简》打算下周开始看,现在pta已经刷到500多分了,计划是三周刷完。回到家的生就不只有学习了,还得下地干活,因为天气炎热的原因,我妈妈每天将我4点半叫起来前去地里除草,那时候比较凉......
  • 菜单4、5以及期中考试总结-BLOG-PTA-4、5
    22201612-刘健涛目录(1)前言(2)设计与分析(3)踩坑心得(4)改进建议(5)总结正文(1)前言  (2)设计与分析(3)踩坑心得(4)改进建议(5)总结......
  • 4-5次及期中考试PTA题目总结
    前言 第四次和第五次pta每次都只有一道题目,分别是菜单计价4和菜单计价5,没有了其他小题pta拿起分来变得困难了许多。不过好在题目要求上菜单计价4和菜单计价5只是菜单计价3的两个分支,没有太多需要叠加的功能,避免的题目过于的复杂。 第四次的菜单计价在菜单计价三的基础上没有加......
  • 第二阶段知识点总结【day32-day35】
    第二阶段知识点总结day321.面向过程和面向对象优缺点,使用场景2.如何定义类,写出一个例子,定义类的过程发生了那些事,如何产生对象,产生的对象有何特点3.如何定制对象自己的属性4.属性的查找顺序是怎样的day331.分别写出一个绑定方法,非绑定方法的例子2.如何隐藏属性,写一个例子,......
  • 第二阶段知识点总结解释版【day32-day35】
    知识点总结day321.面向过程和面向对象优缺点,使用场景面向过程和面向对象都是编程的两种不同的范式。面向过程的优点:1.执行速度比面向对象更快。2.简单易懂,且不需要大量的规则或语法。3.它适合在小型程序中使用。面向过程的缺点:1.没有高度的拓展性。2.系统难以......
  • PTA4-5及期中总结
    (1)前言期中考试的题目集总体来说还是很简单的,题目量比较少而且难度偏易,考察的知识点可以说是很基础的面向对象编程知识点,基本上就是在考验我们的基本功扎不扎实,对于知识点很熟悉的同学可以很快的完成大部分题目,但是还有个接口题目,需要使用java自带类,这题先前没有遇到过就会做......