面向对象编程阶段性总结-3
前言
这是本学期最后一次阶段性总结了,在这一学期的面向对象编程学习中,我充分了解了面向对象编程的基本原理和概念,学会了如何使用Java语言进行面向对象编程。在学习中,我逐渐掌握了如何通过面向对象的思想,将程序中的数据和方法进行封装,从而创建出更加灵活、可重用、易维护的代码。我也练习了如何创建类、对象和方法,以及如何使用继承、多态和接口实现代码复用和扩展。同时,我也注意到了编程规范和良好的代码风格对于提高代码质量和可读性的重要性。在学习中,我不断思考如何写出可维护和可扩展的代码,并尝试着使用不同的技术和工具来提高代码质量和编程效率。总的来说,这一学期的面向对象编程学习让我深入理解了面向对象编程的核心思想和编程技术,同时也提高了我的代码设计和编程能力。在未来的编程学习中,我也将会继续探索新的编程技术和方法,不断提高自己的编程水平。
题目集整体分析
这一阶段的题目集是从学习面向对象编程课程以来题量最大也是最难的一系列题目,其中菜单计价系统和学生成绩统计系统都是对个人来说非常难的迭代题目,在这两次代码的迭代中,如果出现一次的代码没办法拿到满分,那么后续的迭代代码就会变得很难完成。而除了迭代题目的其他题目也都有着很明确的对某个知识点的考察性,题目对栈、堆、队列等基础数据结构的性质进行了基础考察,同时也通过考察的形式使我对知识点的认知程度和记忆程度都进一步加深。在后续编程时碰到类似问题也能很及时的想到通过这些数据类型来找到方便快捷的问题解决方式。
重点题目分析
- 题目集7 菜单计价程序-5
题目基础结构设计:
菜品类:对应菜谱上的一道菜的信息,提供的所有菜的信息。例如单价,菜品名称。
菜单类:保存所有菜品信息
订单类:保存用户点的所有菜的信息。并对其总价进行计算。
特殊要求:
代点菜信息包含:桌号 序号 菜品名称 份额 分数。
代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。
程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。
每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。
折扣的计算方法(注:以下时间段均按闭区间计算):
周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
周末全价,营业时间:9:30-21:30
如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"
题目分析:这道题难度很大,在满足普通客户点菜的前提下,还需要考虑特色菜、菜品菜系、数据是否合法以及下单时间是否在营业时间区间内等等。单个数据的判断并不难,但如此多的限制条件相结合的情况下就让这道题的难度直线上升,经过努力之后最后按照提示艰难写出了示例代码,但是提交上去的结果却是非零返回,明明在编译器中能顺利运行,但是在pta内提交却出现我意料之外的结果,在后续我也检查过是否是方法体错误导致的,但在检查后我个人认为并没有什么问题,所以这次代码的结果就是提交结果为非零返回。又因为这次代码的错误编程,迭代代码也根本没有参照蓝本,所以后续的迭代代码的编写也以失败告终。
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Menu mu = new Menu(); Table[] tablemes = new Table[10]; int j = 0;//菜单数 int l = 0;//订单数 int k = 0;//代点菜数 Dish tt; //int sum = 0; int cntTable = 0;//桌号 int count; String[] temp; int a1,a2,a3,a4,a5; while (true) { String st = sc.nextLine(); temp = st.split(" "); if(st.equals("end")) break; count = temp.length; if (count == 2) {//一个空格 //String[] temp1 = st.split(" "); if (temp[1].equals("delete")) {//第二个为delete a1 = Integer.parseInt(temp[0]); int c = tablemes[cntTable].odt.delARecordByOrderNum(a1); tablemes[cntTable].sum-=c; } else {//菜单添加 a2 = Integer.parseInt(temp[1]); mu.dishs[j] = mu.addDish(temp[0], a2); j++; } //continue; } else if (count == 4) {//三个空格 //String[] temp2 = st.split(" "); if (temp[0].equals("table")) {//桌号 cntTable++;//跳过0; l = 0; tablemes[cntTable] = new Table(); //tablemes[cntTable].tableDtime = st; tablemes[cntTable].AheadProcess(st); System.out.println("table " + cntTable + ": "); } else {//增加订单的情况; a3 =Integer.parseInt(temp[0]); a4 = Integer.parseInt(temp[2]); a5=Integer.parseInt(temp[3]); tablemes[cntTable].odt.addARecord(a3, temp[1],a4 , a5); tt = mu.searthDish(temp[1]); if (tt != null) { tablemes[cntTable].odt.records[l].d = tt; int a = tablemes[cntTable].odt.records[l].getPrice(); System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a ); tablemes[cntTable].sum +=a; } l++; } //continue; } else if (count == 5) {//代点菜 //String[] temp3 = st.split(" "); a1 = Integer.parseInt(temp[1]); a2 = Integer.parseInt(temp[3]); a3 = Integer.parseInt(temp[4]); tablemes[cntTable].odt.addARecord( a1, temp[2], a2, a3); tt = mu.searthDish(temp[2]); if (tt != null) { tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price; int b = tablemes[cntTable].odt.records[l].getPrice(); System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b); tablemes[cntTable].sum += b; } l++; } //st = sc.nextLine(); } for (int i = 1; i < cntTable + 1; i++) { tablemes[i].Gettottalprice(); } } } class Table { int tableNum; String tableDtime; int year,month,day,week,hh,mm,ss; int sum = 0;//一桌价格 ; // boolean f = true; Order odt = new Order(); //Order odre = new Order(); float discnt = -1; void Gettottalprice(){ if(discnt>0){ sum = Math.round(sum*discnt); System.out.println("table " + tableNum + ": " + sum); }else { System.out.println("table " + tableNum + " out of opening hours"); } } void AheadProcess(String tableDtime){ this.tableDtime = tableDtime; processTime(); discount(); //CheckAtime(); } void processTime(){//处理时间 String[] temp = tableDtime.split(" "); tableNum = Integer.parseInt(temp[1]); String[] temp1 = temp[2].split("/"); String[] temp2 = temp[3].split("/"); year = Integer.parseInt(temp1[0]); month = Integer.parseInt(temp1[1]); day = Integer.parseInt(temp1[2]); Calendar c = Calendar.getInstance(); c.set(year, (month-1), day); week = c.get(Calendar.DAY_OF_WEEK); if(week==1) week = 7; else week--; hh = Integer.parseInt(temp2[0]); mm = Integer.parseInt(temp2[1]); ss = Integer.parseInt(temp2[2]); } //void CheckAtime(){ // f= !(discnt < 0); // } void discount(){ if(week>=1&&week<=5) { if(hh>=17&&hh<20) discnt=0.8F; else if(hh==20&&mm<30) discnt=0.8F; else if(hh==20&&mm==30&&ss==0) discnt=0.8F; else if(hh>=11&&hh<=13||hh==10&&mm>=30) discnt=0.6F; else if(hh==14&&mm<30) discnt=0.6F; else if(hh==14&&mm==30&&ss==0) discnt=0.6F; } else { if(hh>=10&&hh<=20) discnt= 1.0F; else if(hh==9&&mm>=30) discnt= 1.0F; else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0) discnt= 1.0F; } } } class Order { Record[] records = new Record[10];//保存订单上每一道的记录 int count = 0;//订单数量 //int forcount = 0;//代点菜的数量 /*int getTotalPrice(){ int sum=0; for(int i=0;i<count;i++){ if(records[i].exist==0) continue; sum=sum+records[i].getPrice(); } return sum; }//计算订单的总价*/ void addARecord(int orderNum,String dishName,int portion,int num){ records[count] = new Record(); records[count].d.name = dishName; records[count].orderNum = orderNum; records[count].portion = portion; records[count].num = num; count++; }//添加一条菜品信息到订单中。 /*Record TakeOdfor(int AnotherNUm,int orderNum,String dishName,int portion,int num){ Record rd2 = new Record(); rd2.d.name = dishName; rd2.orderNum = orderNum; rd2.portion = portion; rd2.d.num = num; rd2.AntherOrderNum = AnotherNUm; //forcount++; return rd2; }*/ int delARecordByOrderNum(int orderNum){ if(orderNum>count||orderNum<=0){ System.out.println("delete error;"); return 0; }else { return records[orderNum - 1].getPrice(); } }//根据序号删除一条记录 } class Record { int orderNum;//序号\ //int AntherOrderNum; Dish d = new Dish();//菜品\ int num = 0; int portion;//份额(1/2/3代表小/中/大份)\ //int exist = 1; int getPrice(){ return d.getPrice(portion)*num; }//计价,计算本条记录的价格\ } class Menu { Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息 int count = 0; Dish searthDish(String dishName){ Dish temd = null; for(int i=count-1;i>=0;i--){ if(dishName.equals(dishs[i].name)){ temd = dishs[i]; break; } } if(temd==null){ System.out.println(dishName+" does not exist"); } return temd; }//根据菜名在菜谱中查找菜品信息,返回Dish对象。 Dish addDish(String dishName,int unit_price){ Dish dh = new Dish(); dh.name = dishName; dh.unit_price = unit_price; count++; return dh; }//添加一道菜品信息 } class Dish { String name;//菜品名称 int unit_price; //单价 //int num; int getPrice(int portion) { int peic = 0; if (portion == 1) { peic = unit_price ; } else if (portion == 2) { peic = Math.round((float) (unit_price * 1.5)) ; } else if (portion == 3) { peic = (unit_price * 2) ; } return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) } }

2.课程成绩统计程序-1
题目基础结构设计:
课程类:存储课程的基本信息,如课程名称,考查形式,是否有平时成绩等
学生类:存储学生的基本信息以及学生的全部成绩。
班级类:将同一个班级的学生进行集中储存
判断类:判断输入的成绩信息是否合法
成绩类:存储科目名称,平时成绩以及期末成绩
输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
题目分析:本题的难度我认为属于偏难的一类,虽然是典型的数据统计题。但是在编写过程中还是经常出现意料之外的错误,最后最重要的正则表达式问题可能也没有解决,否则程序不应该将一切数据都归为不合法数据,在后续的几次程序中该问题也始终没有彻底解决,所以后续的程序代码也几乎没有分数。
import java.util.*; public class Main { public static void main(String[] args) { LinkedHashSet<Course> Allcourses = new LinkedHashSet<>(); ArrayList<Class> Allclasses = new ArrayList<>(); Judge judge = new Judge(); Scanner scanner = new Scanner(System.in); ArrayList<String> Date = new ArrayList<>(); while (true) { String New = scanner.nextLine(); if (New.equals("end")) { break; } if(New.matches("[0-9]{8} [\\S]{1,11} [\\S]{1,11}\\S(0|100|[1-9][0-9]?) (0|100|[1-9][0-9]?)")){ Date.add(New); }else if(New.matches("[0-9]{8} [\\S]{1,10} [\\S]{1,10} (0|100|[1-9][0-9]?)")){ Date.add(New); }else{ System.out.println("wrong format"); } } for (String message : Date) { String[] A = message.split(" "); if (A.length == 3 || A.length == 2) { if (A.length == 2) {//长度为2必然必修或者错误选修 if (A[1].equals("必修")) { Course newcourse = new Course(A[0], A[1], true); Allcourses.add(newcourse); } else { System.out.println("wrong format"); } } else { if (A[1].equals("必修")&&A[2].equals("考察")){ System.out.println(A[0]+" : course type & access mode mismatch"); }else{ if (A[1].equals("必修")) {//有考试形式的必修课 Course newcourse = new Course(A[0], A[1], true); Allcourses.add(newcourse); } else if (A[1].equals("选修") && A[2].equals("考试")) {//有考试形式的选修 Course newcourse = new Course(A[0], A[1], true); Allcourses.add(newcourse); } else if (A[1].equals("选修") && A[2].equals("考察")) { Course newcourse = new Course(A[0], A[1], false); Allcourses.add(newcourse); } else {//输入错误统一输出 System.out.println("wrong format"); } } } } else if (A.length == 4 || A.length == 5) {//长度为4,5时录入学生信息 if (judge.LegalCourse(A[2], A.length, Allcourses) == 1) { if (A.length == 4) { Student student = new Student(A[0], A[1]); Score score = new Score(A[2], Integer.parseInt(A[3])); for(Course course:Allcourses){ if(course.getName().equals(A[2])){ course.addScore(score); } } student.getScores().add(score); if (judge.isClassExist(student.getId().substring(0, 6), Allclasses)) { judge.SearchClass(student.getId().substring(0, 6), Allclasses).addStudent(student); } else { Class newclass = new Class(student.getId().substring(0, 6)); newclass.addStudent(student); Allclasses.add(newclass); } }else{ Student student = new Student(A[0], A[1]); Score score = new Score(A[2], Integer.parseInt(A[3]),Integer.parseInt(A[4])); for(Course course:Allcourses){ if(course.getName().equals(A[2])){ course.addScore(score); } } student.getScores().add(score); if (judge.isClassExist(student.getId().substring(0, 6), Allclasses)) { judge.SearchClass(student.getId().substring(0, 6), Allclasses).addStudent(student); } else { Class newclass = new Class(student.getId().substring(0, 6)); newclass.addStudent(student); Allclasses.add(newclass); } } }else if(judge.LegalCourse(A[2], A.length, Allcourses) == -1){ System.out.println(A[0]+" "+A[1]+" "+": access mode mismatch"); }else{ System.out.println(A[0]+" "+A[1]+" "+":"+A[2]+"does not exist"); } } } for (Class classA:Allclasses){//个人总成绩平均分 Collections.sort(classA.getStudents()); for(Student student:classA.getStudents()){ student.getTotal(); } } for(Course course:Allcourses){ if(course.getIsNomal()){ course.NormalAverage(); }else { course.ExamAverage(); } } Collections.sort(Allclasses); for(Class classA:Allclasses){ classA.getaverege(); } } } class Course { private String Name; private String isRequired;//课程类型 private Boolean isNomal;//是否有平时成绩 private ArrayList<Score> scores = new ArrayList<>();//单科成绩数组 Course(){ } Course(String name,String form,Boolean Normal){ this.isRequired = form; this.Name = name; this.isNomal = Normal; } public String getName() { return Name; } public String getForm() { return isRequired; } public void setForm(String form) { this.isRequired = form; } public void setName(String name) { Name = name; } public Boolean getIsNomal() { return isNomal; } public void setIsNomal(Boolean exam) { isNomal = exam; } public void addScore(Score score){ scores.add(score); } public void NormalAverage(){ if(scores.size() !=0){ int sum =0; int exam = 0; int total = 0; for (Score score:scores){ sum += score.getQuiz(); exam += score.getExam(); total += score.getScore(); } System.out.println(Name +" "+sum/scores.size()+" "+exam/scores.size()+" "+total/scores.size()); } else { System.out.println(Name+" "+"has no grades yet"); } } public void ExamAverage(){ if(scores.size() !=0){ int exam = 0; int total = 0; for (Score score:scores){ exam += score.getExam(); total += score.getScore(); } System.out.println(Name +" "+exam/scores.size()+" "+total/scores.size()); } else { System.out.print(Name+" "+"has no grades yet"); } } } class Judge {//判断类,检测是否合法 public boolean isClassExist(String classNumber, ArrayList<Class> Allclasses){ for(Class classA:Allclasses){ if(classA.getClassid().equals(classNumber)){ return true; } } return false; } public Class SearchClass(String Id,ArrayList<Class> Allclasses){ Class Temporaray = null; for(Class classA:Allclasses){ if(classA.getClassid().equals(Id)){ Temporaray = classA; } } return Temporaray; } public int LegalCourse(String name, int A, HashSet<Course> Allcourses){ for(Course course:Allcourses){ if(course.getName().equals(name)){ if ((!(course.getIsNomal()) && A == 5) || (course.getIsNomal() && A ==4)){ return -1; }else { return 1; } } } return 0; } } class Class implements Comparable<Class>{ private String ClassId;//班级 private ArrayList <Student> students = new ArrayList<>(); Class(){ } Class(String Id){ this.ClassId = Id; } public ArrayList<Student> getStudents() { return students; } public void setStudents(ArrayList<Student> students) { this.students = students; } public void addStudent(Student student){ students.add(student); } public String getClassid() { return ClassId; } public void setClassid(String classid) { this.ClassId = classid; } public void getaverege(){ int sum = 0; for(Student A:students){ sum += A.getaverage(); } if(sum == 0){ System.out.println(ClassId+" "+"has no grades yet"); }else{ System.out.println(ClassId+" "+sum/students.size()); } } @Override public int compareTo(Class o) { int A = Integer.parseInt(this.ClassId); int B = Integer.parseInt(o.ClassId); return Integer.compare(B,A); } } class Score { private String courseName;//科目名称 private int quiz = 0;//平时成绩 private int exam;//期末成绩 public Score(String courseName, int quiz, int exam) { this.courseName = courseName; this.quiz = quiz; this.exam = exam; } public Score(String courseName, int exam) { this.courseName = courseName; this.exam = exam; } public String getCourseName() { return courseName; } public int getQuiz() { return quiz; } public int getExam() { return exam; } public int getScore(){ if(quiz ==0){ return exam; }else { return (int)(0.3*quiz+0.7*exam); } } } class Student implements Comparable<Student>{ private String id; private String name; private ArrayList<Score> scores = new ArrayList<>(); Student(String id,String name){ this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public ArrayList<Score> getScores() { return scores; } public void setScores(ArrayList<Score> scores) { this.scores = scores; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void getTotal(){//得出总成绩平均分 if(scores.size() ==0 ) { System.out.println(id + " " + name + " " + "did not take any exams"); }else{ System.out.println(id+" "+name+" "+getaverage()); } } public int getaverage(){//得出平均分用于计算 int sum = 0; for(Score score:scores){ sum += score.getScore(); } return sum/scores.size(); } @Override public int compareTo(Student o) { int A = Integer.parseInt(this.id); int B = Integer.parseInt(o.id); if(A > B){ return -1; }else if(A < B){ return 1; }else{ return 0; } } }

3.容器-HashMap-排序-检索
该题目是有关map类的首次使用,体现了map类一一对应和方便排序等特性。题目i相对来说比较容易,但很有代表性,所以在这里拿出来单说。
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Map<String, String> studentMap = new HashMap<>(); while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } String[] infos = str.split(" "); String id = infos[0]; String name = infos[1]; String score = infos[2]; studentMap.put(id, name + " " + score); } String id = input.nextLine(); if (studentMap.containsKey(id)) { String info = id + " " + studentMap.get(id); System.out.println(info); } else { System.out.println("The student " + id + " does not exist"); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Map<String, String> studentMap = new HashMap<>(); while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } String[] infos = str.split(" "); String id = infos[0]; String name = infos[1]; String score = infos[2]; studentMap.put(id, name + " " + score); } Map<String, String> sortedMap = new TreeMap<>(Collections.reverseOrder()); sortedMap.putAll(studentMap); for (Map.Entry<String, String> entry : sortedMap.entrySet()) { String id = entry.getKey(); String info = id + " " + entry.getValue(); System.out.println(info); } } }
踩坑心得
这阶段的学习最让我印象深刻的就是:要先画类图再编程,类图对于编程的思路来说是很重要的,清晰的思路能让编程变得简单而且逻辑清晰。其余的大小问题也都在这一学期的学习中补全完善了。而类图的缺失和编程逻辑思想的不足是这一阶段最突出的问题,所以我决定把这个问题拿出来单独说
对课程的意见和建议
个人认为老师的课程安排是很合理的,虽然说过难度已经降低了,但是对我来说还是比较艰难的。要提的建议是:希望课程能多讲解一些基础知识和技巧,在翻转课堂时,希望老师能将重点的内容大致说一遍再交给同学自由发挥,就可以很大程度避免听的云里雾里的情况。
总结
在这一学期的Java学习中,我从零基础开始逐渐学会了使用Java语言进行程序设计和开发。在学习中,我通过系统的学习了解了Java语言的基本语法、面向对象编程思想和基本数据结构,同时也了解了Java在Web开发、网络编程和并发编程等方面应用的特点和优势。
在实践中,我通过完成多个小项目和练习,不断磨练了自己的编程技巧和经验。同时,我也注重了代码编写的规范和代码风格的良好性,尝试用优雅的代码来实现较为复杂的任务,提高程序实现的效率和质量。
此外,我也注意到了Java社区的开放性和创新性,许多优秀的Java开源框架、库和工具可以让开发者更加轻松地开发应用。因此,在学习中我也积极尝试了使用不同的Java开源框架进行编程。
总的来说,这一学期的Java学习让我深入了解了Java的基本语法和面向对象编程思想,并提高了我对编程的兴趣和热情。在未来的Java学习中,我也会继续探索新的技术和应用场景,不断提高自己的编程能力和水平。
浙公网安备 33010602011771号