第三阶段PTA题目集总结及课程的教学评价
前言:
第三阶段PTA题目集一共包括六次题目集,相比之前的题目集,难度和代码数量都得到了提升,题目集越往后,越注重实际问题的解决和对数据的分析以及对Map,Set等的实际应用。而在第三阶段PTA题目集中涉及到了许多知识点,如类的继承、多态、接口及集合框架,以及对ArrayList,Map的使用。第三阶段PTA题目集的后半段跟多的是涉及到对数据的分析与使用,对数据的合法性进行判断。
第七次OPP训练集: 第七次训练集集中于让我们了解类继承、多态、接口及集合框架。
第八次OPP训练集: 第八次训练集则是让我们掌握类的继承、多态性使用方法以及接口在对实际问题的应用。
第九次OOP训练集: 第九次训练集主要是对List、Set、Map的熟练与应用,并使用如上接口解决实际问题。
第十次OOP训练集: 第十次训练集主要是课程成绩统计程序的编写,考察我们对于类之间的关系的运用减少题目难度。
第十一次OOP训练集: 第十一次训练集主要是对容器-HashMap的熟练与应用以及对多态的进一步运用。
第十二次OOP训练集: -第十二次训练集主要是对容器-ArrayList的熟练与运用,利用其中的接口完成排序等问题以及自定义接口ArrayIntegerStack,理解Stack的逻辑。
PTA题目集:
7-1 统计Java程序中关键词的出现次数
设计与分析:
设计 先将获取代码储存,再将代码进行分析。
分析 提取关键字先将53个关键字数组存好,先将代码中的注释替换,字符串替换,再统计代码中的所有关键字。再重写Map或Set中的compare接口升序输出。
具体代码
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static String[] keywords = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"};
static Scanner input = new Scanner(System.in);
public static String getSourceCode(){//
StringBuilder sb=new StringBuilder();
String line=input.nextLine();
if(line.equals("exit")){
System.out.println("Wrong Format");
System.exit(0);
}
while (!line.equals("exit")){
sb.append(line);
sb.append("\n");
line=input.nextLine();
}
return sb.toString();
}
public static void main(String[] args){
Map keyWordMap= new TreeMap<>();
//存源码
String sourceCode=getSourceCode();
//去除字符串
String removeString="\"(.|[\\r\\n])*?\"";
sourceCode=sourceCode.replaceAll(removeString,"");
//去除单行注释
String removeOneComments="//.*\n";//去除单行注释
sourceCode=sourceCode.replaceAll(removeOneComments,"");
//去除多行注释
String removeMoreComments="/\\\\*(.|[\\r\\n])*?\\\\*/";//去除多行注释
sourceCode=sourceCode.replaceAll(removeMoreComments,"");
//去除运算符
sourceCode=sourceCode.replaceAll("\\=","a");
for (String keyword : keywords) {
keyWordMap.put(keyword, 0);
}
String[] words = sourceCode.split("\\W+");
for(String keyword : keywords) {
//处理关键字
int count = 0;
for (String word : words) {
if (word.matches(keyword)) {//(.|[\r\n])*
count++;
}
}
keyWordMap.put(keyword, count);
}
for(String keyword:keyWordMap.keySet()){
int count=keyWordMap.get(keyword);
if(count>0){
System.out.println(count+"\t"+keyword);
}
}
}
}
生成报表
踩坑心得
这道题在写的过程中要注意内存与时间,不同方法的实现所占的内存和所耗时间都是不一样的,我一开始是将整个源码进行处理,当最后还是超时了,最后换成了一行一行的处理,减少内存和时间。
改进建议
这段代码还可以优化一下,如遵循好单一职责,Main函数的职责还是过重,不规范。用更少的时间来处理源码,减少时间和内存的消耗。
课程成绩统计程序-1
设计与分析:
分为三部分
第一部分 获取信息
第二部分 处理信息
第三部分 输出信息
主要包括 Score类 ,Student类 ,Class类,Course类。
具体代码
import javax.print.DocFlavor;
import java.text.Collator;
import java.util.*;
public class Main {
public static void warning(){
System.out.println("wrong format");
System.exit(0);
}
public static boolean isValueFormat(String[] words){
if(words.length<2||words.length>5){
return false;
}
//课程
if(words.length==2||words.length==3){
if(words[0].length()>10){
return false;
}
if(!words[1].matches("必修|选修")){
return false;
}
if(words.length==3){
if(!words[2].matches("考试|考察")){
return false;
}
}
if(words.length==2){
if(!words[1].equals("必修"))
return false;
}
}
//学生
else {
if(words[0].length()!=8)
return false;
if(words[1].length()>10)
return false;
if(words.length==4)
if(!words[3].matches("^(100|[1-9]\\d|\\d)$"))
return false;
if(words.length==5)
if(!words[3].matches("^(100|[1-9]\\d|\\d)$")||!words[4].matches("^(100|[1-9]\\d|\\d)$"))
return false;
}
return true;
}
public static void addCourses(String[] words,Map courses) {
boolean flag = true;
for (String name : courses.keySet()) {
if (words[0].equals(name)) {
flag = false;
break;
}
}
if(flag){
if (words.length == 2) {
courses.put(words[0], new Course(words[0], words[1], "考试"));
} else if (words.length == 3) {
courses.put(words[0], new Course(words[0], words[1], words[2]));
}
}
}
public static void getData(Map courses,Map classes){
Scanner input=new Scanner(System.in);
ArrayListarrayList=new ArrayList<>();
String line=input.nextLine();
while(!line.equals("end")){
boolean hasMatched;
String words[]=line.split(" ");
if(!isValueFormat(words)){
System.out.println("wrong format");
line=input.nextLine();
continue;
}
boolean repeat=false;
for(String lines:arrayList){
if(line.equals(lines)){
repeat=true;
}
}
arrayList.add(line);
if(repeat){
line=input.nextLine();
continue;
}
switch (words.length){
case 2:
addCourses(words,courses);
break;
case 3:
if(words[1].equals("必修")&&words[2].equals("考察")){
System.out.println(words[0] + " : course type & access mode mismatch");
}
else {
addCourses(words,courses);
}
break;
case 4:
hasMatched=true;
if(courses.size()==0){
System.out.println(words[2]+" does not exist");
hasMatched=false;
}
for(Map.EntrycourseEntry:courses.entrySet()){
if(!courses.containsKey(words[2])){
System.out.println(words[2]+" does not exist");
hasMatched=false;
break;
}
if(words[2].equals(courseEntry.getKey())){
if(courseEntry.getValue().getAssessmentMethod().equals("考试")){
System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
hasMatched=false;
}
}
}
if(hasMatched){
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Score(Double.parseDouble(words[3])),new Student(words[0],words[1]));
}
else{
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Student(words[0],words[1]));
}
break;
case 5:
hasMatched=true;
if(courses.size()==0){
System.out.println(words[2]+" does not exist");
hasMatched=false;
}
for(Map.EntrycourseEntry:courses.entrySet()){
if(!courses.containsKey(words[2])){
System.out.println(words[2]+" does not exist");
hasMatched=false;
break;
}
if(words[2].equals(courseEntry.getKey())){
if(courseEntry.getValue().getAssessmentMethod().equals("考察")){
System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
hasMatched=false;
}
}
}
if(hasMatched) {
classes = addClass(words[0].substring(0, 6), classes);
classes.get(words[0].substring(0, 6)).addStudent(words[2], new Score(Double.parseDouble(words[3]), Double.parseDouble(words[4])), new Student(words[0], words[1]));
for (String name : courses.keySet()) {
if (words[2].equals(name)) {
if (courses.get(name).getAssessmentMethod().equals("考试")) {
classes.get(words[0].substring(0, 6)).getStudents().get(words[0]).getCourseScores().get(words[2]).setExamScores();
} else {
classes.get(words[0].substring(0, 6)).getStudents().get(words[0]).getCourseScores().get(words[2]).setAssessmentScores();
}
}
}
}
break;
default:
}
line=input.nextLine();
}
}
//添加班级
public static Map addClass(String id,Map classes){
boolean flag=true;
for(String classesId:classes.keySet()){
if(id.equals(classesId)){
flag=false;
}
}
if(flag){
Class newClass=new Class(id);
classes.put(id,newClass);
}
return classes;
}
public static void printfResult(Map courses,Map classes){
ArrayList deleteStudent=new ArrayList<>();
for(Map.Entry classEntry: classes.entrySet()){//班级列表遍历
for(Map.Entry studentEntry:classEntry.getValue().getStudents().entrySet()) {//学生列表遍历
if(studentEntry.getValue().getCourseScores().size()==0){
deleteStudent.add(studentEntry.getKey());
System.out.println(studentEntry.getKey()+" "+studentEntry.getValue().getStudentName() +" did not take any exams");
}
else
System.out.println(studentEntry.getValue().getStudentID()+" "+studentEntry.getValue().getStudentName()+" "+String.format("%d",studentEntry.getValue().getStudentAverageScore()));
}
for(String name:deleteStudent){
classEntry.getValue().getStudents().remove(name);
}
}
//课程
Collator collator = Collator.getInstance(Locale.CHINA);
Map resortCourseMap=new TreeMap<>(collator);
for(Map.Entry courseEntry:courses.entrySet()){
double dailyAverageScore=0,finalAverageScore=0,averageScore=0,num=0;
for(Map.Entry classEntry: classes.entrySet()){//班级列表遍历
for(Map.Entry studentEntry:classEntry.getValue().getStudents().entrySet()){//学生列表遍历
for(Map.Entry scoreEntry:studentEntry.getValue().getCourseScores().entrySet()){
if(courseEntry.getValue().getCourseName().equals(scoreEntry.getKey())){
dailyAverageScore+=scoreEntry.getValue().getRegularScore();
finalAverageScore+=scoreEntry.getValue().getFinalScore();
averageScore+=scoreEntry.getValue().getScore();
num++;
}
}
}
}
if(num==0){
num=1;
averageScore=-1;
}
dailyAverageScore=dailyAverageScore/num;
finalAverageScore=finalAverageScore/num;
averageScore=averageScore/num;
if(courseEntry.getValue().getAssessmentMethod().equals("考试")) {
StringBuilder endResult=new StringBuilder();
endResult.append(courseEntry.getKey()+" "+(int)dailyAverageScore+" "+(int)finalAverageScore+" "+(int)averageScore);
resortCourseMap.put(courseEntry.getKey(),endResult.toString());
}
else {
StringBuilder endResult=new StringBuilder();
endResult.append(courseEntry.getKey()+" "+(int)finalAverageScore+" "+(int)averageScore);
resortCourseMap.put(courseEntry.getKey(),endResult.toString());
}
}
//课程输出
for(String i:resortCourseMap.keySet()){
//课程没有任何成绩信息
if(Integer.parseInt(resortCourseMap.get(i).split(" ")[resortCourseMap.get(i).split(" ").length-1])==-1){
System.out.println(resortCourseMap.get(i).split(" ")[0]+" "+"has no grades yet");
}
else{
System.out.println(resortCourseMap.get(i));
}
}
//班级输出
for(Map.Entry classEntry:classes.entrySet()){
if(classEntry.getValue().getClassAverageScore()==-1){
System.out.println(classEntry.getKey()+" has no grades yet");
}
else {
System.out.println(classEntry.getKey()+" "+String.format("%d",classEntry.getValue().getClassAverageScore()));
}
}
}
public static void main(String[] args){
Collator collator = Collator.getInstance(Locale.CHINA);
Map courses=new TreeMap< >(collator);//课程信息,key:CourseName
Map classes=new TreeMap<>();//班级信息,key:classID
getData(courses,classes);
printfResult(courses,classes);
}
}
class Student{
private String studentID;
private String studentName;
private MapcourseScores=new TreeMap<>();//key:CourseName
Student(String studentID,String studentName){
this.studentID=studentID;
this.studentName=studentName;
}
public void addCourseScore(String courseName,Score score){
boolean flag=true;
for(String name:courseScores.keySet()){
if(courseName.equals(name)){
flag=false;
break;
}
}
if(flag){
courseScores.put(courseName,score);
}
else{
//当课程成绩重复输入,只记录第一次输入
}
}
//添加平时成绩,期末成绩,最终成绩
public boolean isRepeat(String name){
boolean flag=true;
for(String courseName:courseScores.keySet()){
if(courseName.equals(name)){
flag=false;
break;
}
}
return !flag;
}
//最终所有学生成绩平均分
public int getStudentAverageScore(){
double sum=0;
for(String name:courseScores.keySet()){
sum+=courseScores.get(name).getScore();
}
sum=sum/(double)(courseScores.size());
return (int)sum;
}
public String getStudentName() {
return studentName;
}
public String getStudentID() {
return studentID;
}
public Map getCourseScores() {
return courseScores;
}
}
class Class{
private String id; // 班级号
private Map students=new TreeMap<>(); //key:学号 学生列表
Class(String id){
this.id=id;//6位id
}
public void addStudent(String courseName,Score score,Student student){
boolean flag=true;
for(String id:students.keySet()){
if(student.getStudentID().equals(id)){
flag=false;
break;
}
}
if(flag){
this.students.put(student.getStudentID(),student);
this.students.get(student.getStudentID()).addCourseScore(courseName,score);
}
else{
this.students.get(student.getStudentID()).addCourseScore(courseName,score);
}
}
public void addStudent(String courseName,Student student){
boolean flag=true;
for(String id:students.keySet()){
if(student.getStudentID().equals(id)){
flag=false;
break;
}
}
if(flag){
this.students.put(student.getStudentID(),student);
}
}
//班级中每个学生的成绩的平均分
public int getClassAverageScore(){
double sum=0;
boolean flag=false;
for(String id:students.keySet()){
sum+=students.get(id).getStudentAverageScore();
if(students.get(id).getCourseScores().size()!=0){
flag=true;
}
}
sum=sum/(double) students.size();
if(!flag){
sum=-1;
}
return (int)sum;
}
public Map getStudents() {
return students;
}
public String getId() {
return id;
}
}
class Score{
private String assessmentMethod;
private double regularScore=-1;
private double finalScore;
private double score;
Score(){
}
Score(double regularScore,double finalScore){
this.regularScore=regularScore;
this.finalScore=finalScore;
}
Score(double finalScore){
this.finalScore=finalScore;
this.score=finalScore;
}
public double getRegularScore() {
return regularScore;
}
public double getFinalScore() {
return finalScore;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public void setExamScores(){
setScore(getRegularScore()*0.3+getFinalScore()*0.7);
}
public void setAssessmentScores(){
setScore(getFinalScore());
}
}
class Course{
private String courseName;
private String property; // 课程性质(必修/选修)
private String assessmentMethod; // 考核方式(考试/考察)
Course(String courseName,String property,String assessmentMethod){
this.assessmentMethod=assessmentMethod;
this.courseName=courseName;
this.property=property;
}
public String getAssessmentMethod() {
return assessmentMethod;
}
public String getCourseName() {
return courseName;
}
}
生成报表
踩坑心得
这一题踩坑比较深,一开始的信息处理没有做好,并且对错误数据的处理没有正确的处理,并以一种比较复杂的方式处理,加大了后续更改,优化程序的难度。后面对数据的处理也非常臃肿,很不规范。
改进建议
将信息的错误处理放在信息储存的地方,二是将信息处理完后再输出,增加代码的可读性。也为后续修改和优化提供便利。
课程成绩统计程序-2
设计与分析:
主要增加了实验课以及实验课的成绩计算和输出,以及实验课的错误信息判定。
具体代码
import javax.print.DocFlavor;
import java.text.Collator;
import java.util.*;
public class Main {
public static boolean isValueFormat(String[] words){
if(words.length<2||words.length>13){
return false;
}
//课程
if(words.length==2||words.length==3){
if(words[0].length()>10){
return false;
}
if(!words[1].matches("必修|选修|实验")){
return false;
}
if(words.length==3){
if(!words[2].matches("考试|考察|实验")){
return false;
}
}
if(words.length==2){
if(!words[1].equals("必修"))
return false;
}
}
//学生
else {
boolean hasExperiment=false;
if(words[0].length()!=8)
return false;
if(words[1].length()>10)
return false;
if(words.length==4){
if(!words[3].matches("^(100|[1-9]\\d|\\d)$"))
return false;
}
if(words.length>5){
if(Integer.parseInt(words[3])<4||Integer.parseInt(words[3])>9){
return false;
}
for(int i=4;i courses) {
boolean flag = true;
for (String name : courses.keySet()) {
if (words[0].equals(name)) {
flag = false;
break;
}
}
if(flag){
if (words.length == 2) {
courses.put(words[0], new Course(words[0], words[1], "考试"));
} else if (words.length == 3) {
courses.put(words[0], new Course(words[0], words[1], words[2]));
}
}
}
public static boolean isExist(String[] words,Map courses){
boolean flag=false;
for(String name:courses.keySet()){
if(words[2].equals(name)){
flag=true;
}
}
return flag;
}
public static boolean isMatch(String []words,Map courses){
boolean flag=false;
String method=new String();
method=courses.get(words[2]).getAssessmentMethod();
if(method.equals("考试")){
if(words.length==5){
flag=true;
}
}
else if(method.equals("考察")){
if(words.length==4||words.length==5){
flag=true;
}
}
else if(method.equals("实验")){
if(words.length==Integer.parseInt(words[3])+4){
flag=true;
}
}
return flag;
}
public static boolean isCourseTypeAndAccessModeMismatch(String []words){
boolean flag=false;
String CourseType=words[1];
String AccessMode;
if(words.length==2){
AccessMode="考试";
}
else {
AccessMode=words[2];
}
if(CourseType.equals("必修")&&AccessMode.equals("考试")){
flag=true;
}
else if(CourseType.equals("选修")&&(AccessMode.equals("考试")||AccessMode.equals("考察"))){
flag=true;
}
else if(CourseType.equals("实验")&&AccessMode.equals("实验")){
flag=true;
}
return flag;
}
public static void getData(Map courses,Map classes){
Scanner input=new Scanner(System.in);
ArrayListarrayList=new ArrayList<>();
String line=input.nextLine();
while(!line.equals("end")){
boolean hasMatched;
String words[]=line.split(" ");
if(!isValueFormat(words)){
System.out.println("wrong format");
line=input.nextLine();
continue;
}
if(words.length>3&&!isExist(words,courses)){
System.out.println(words[2]+" does not exist");
line=input.nextLine();
continue;
}
else if(words.length>3&&!isMatch(words,courses)){
System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
words[3]=String.format("%d",-1);
}
else if(words.length<=3&&!isCourseTypeAndAccessModeMismatch(words)){
System.out.println(words[0] + " : course type & access mode mismatch");
line=input.nextLine();
continue;
}
boolean repeat=false;
for(String lines:arrayList){
if(line.equals(lines)){
repeat=true;
}
}
arrayList.add(line);
if(repeat){
line=input.nextLine();
continue;
}
switch (words.length){
case 2:
addCourses(words,courses);
break;
case 3:
addCourses(words,courses);
break;
case 4:
hasMatched=true;
for(Map.EntrycourseEntry:courses.entrySet()){
if(words[2].equals(courseEntry.getKey())){
if(courseEntry.getValue().getAssessmentMethod().equals("考试")){
hasMatched=false;
}
}
}
if(hasMatched){
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Score(Integer.parseInt(words[3])),new Student(words[0],words[1]));
}
else{
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Student(words[0],words[1]));
}
break;
case 5:
hasMatched=true;
for(Map.EntrycourseEntry:courses.entrySet()){
if(words[2].equals(courseEntry.getKey())){
if(courseEntry.getValue().getAssessmentMethod().equals("考察")){
hasMatched=false;
}
}
}
if(hasMatched) {
classes = addClass(words[0].substring(0, 6), classes);
classes.get(words[0].substring(0, 6)).addStudent(words[2], new Score(Integer.parseInt(words[3]),Integer.parseInt(words[4])), new Student(words[0], words[1]));
for (String name : courses.keySet()) {
if (words[2].equals(name)) {
if (courses.get(name).getAssessmentMethod().equals("考试")) {
classes.get(words[0].substring(0, 6)).getStudents().get(words[0]).getCourseScores().get(words[2]).setExamScores();
} else {
classes.get(words[0].substring(0, 6)).getStudents().get(words[0]).getCourseScores().get(words[2]).setAssessmentScores();
}
}
}
}
break;
default:
//输入学生实验信息
if(words[3].equals("-1")){
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Student(words[0],words[1]));
}
else{
classes=addClass(words[0].substring(0,6),classes);
int sum=0;
for(int i=4;i addClass(String id,Map classes){
boolean flag=true;
for(String classesId:classes.keySet()){
if(id.equals(classesId)){
flag=false;
}
}
if(flag){
Class newClass=new Class(id);
classes.put(id,newClass);
}
return classes;
}
public static void printfResult(Map courses,Map classes){
ArrayList deleteStudent=new ArrayList<>();
for(Map.Entry classEntry: classes.entrySet()){//班级列表遍历
for(Map.Entry studentEntry:classEntry.getValue().getStudents().entrySet()) {//学生列表遍历
if(studentEntry.getValue().getCourseScores().size()==0){
deleteStudent.add(studentEntry.getKey());
System.out.println(studentEntry.getKey()+" "+studentEntry.getValue().getStudentName() +" did not take any exams");
}
else
System.out.println(studentEntry.getValue().getStudentID()+" "+studentEntry.getValue().getStudentName()+" "+String.format("%d",studentEntry.getValue().getStudentAverageScore()));
}
for(String name:deleteStudent){
classEntry.getValue().getStudents().remove(name);
}
}
//课程
Collator collator = Collator.getInstance(Locale.CHINA);
Map resortCourseMap=new TreeMap<>(collator);
for(Map.Entry courseEntry:courses.entrySet()){
double dailyAverageScore=0,finalAverageScore=0,averageScore=0,num=0;
for(Map.Entry classEntry: classes.entrySet()){//班级列表遍历
for(Map.Entry studentEntry:classEntry.getValue().getStudents().entrySet()){//学生列表遍历
for(Map.Entry scoreEntry:studentEntry.getValue().getCourseScores().entrySet()){
if(courseEntry.getValue().getCourseName().equals(scoreEntry.getKey())){
dailyAverageScore+=scoreEntry.getValue().getRegularScore();
finalAverageScore+=scoreEntry.getValue().getFinalScore();
averageScore+=scoreEntry.getValue().getScore();
num++;
}
}
}
}
if(num==0){
num=1;
averageScore=-1;
}
dailyAverageScore=dailyAverageScore/num;
finalAverageScore=finalAverageScore/num;
averageScore=averageScore/num;
if(courseEntry.getValue().getAssessmentMethod().equals("考试")) {
StringBuilder endResult=new StringBuilder();
endResult.append(courseEntry.getKey()+" "+(int)dailyAverageScore+" "+(int)finalAverageScore+" "+(int)averageScore+" "+courseEntry.getValue().getAssessmentMethod());
resortCourseMap.put(courseEntry.getKey(),endResult.toString());
}
else {
StringBuilder endResult=new StringBuilder();
endResult.append(courseEntry.getKey()+" "+(int)finalAverageScore+" "+(int)averageScore+" "+courseEntry.getValue().getAssessmentMethod());
resortCourseMap.put(courseEntry.getKey(),endResult.toString());
}
}
//课程输出
for(String i:resortCourseMap.keySet()){
//课程没有任何成绩信息
if(Integer.parseInt(resortCourseMap.get(i).split(" ")[resortCourseMap.get(i).split(" ").length-2])==-1){
System.out.println(resortCourseMap.get(i).split(" ")[0]+" "+"has no grades yet");
}
else{
if(resortCourseMap.get(i).split(" ")[resortCourseMap.get(i).split(" ").length-1].equals("实验")){
String[] word=resortCourseMap.get(i).split(" ");
System.out.println(word[0]+" "+word[1]);
}
else if(resortCourseMap.get(i).split(" ")[resortCourseMap.get(i).split(" ").length-1].equals("考试")){
String[] word=resortCourseMap.get(i).split(" ");
System.out.println(word[0]+" "+word[1]+" "+word[2]+" "+word[3]);
}
else if(resortCourseMap.get(i).split(" ")[resortCourseMap.get(i).split(" ").length-1].equals("考察")){
String[] word=resortCourseMap.get(i).split(" ");
System.out.println(word[0]+" "+word[1]+" "+word[2]);
}
}
}
//班级输出
for(Map.Entry classEntry:classes.entrySet()){
if(classEntry.getValue().getClassAverageScore()==-1){
System.out.println(classEntry.getKey()+" has no grades yet");
}
else {
System.out.println(classEntry.getKey()+" "+String.format("%d",classEntry.getValue().getClassAverageScore()));
}
}
}
public static void main(String[] args){
Collator collator = Collator.getInstance(Locale.CHINA);
Map courses=new TreeMap< >(collator);//课程信息,key:CourseName
Map classes=new TreeMap<>();//班级信息,key:classID
getData(courses,classes);
printfResult(courses,classes);
}
}
class Student{
private String studentID;
private String studentName;
private MapcourseScores=new TreeMap<>();//key:CourseName
Student(String studentID,String studentName){
this.studentID=studentID;
this.studentName=studentName;
}
public void addCourseScore(String courseName,Score score){
boolean flag=true;
for(String name:courseScores.keySet()){
if(courseName.equals(name)){
flag=false;
break;
}
}
if(flag){
courseScores.put(courseName,score);
}
else{
//当课程成绩重复输入,只记录第一次输入
}
}
//添加平时成绩,期末成绩,最终成绩
public boolean isRepeat(String name){
boolean flag=true;
for(String courseName:courseScores.keySet()){
if(courseName.equals(name)){
flag=false;
break;
}
}
return !flag;
}
//最终所有学生成绩平均分
public int getStudentAverageScore(){
double sum=0;
for(String name:courseScores.keySet()){
sum+=courseScores.get(name).getScore();
}
sum=sum/(double)(courseScores.size());
return (int)sum;
}
public String getStudentName() {
return studentName;
}
public String getStudentID() {
return studentID;
}
public Map getCourseScores() {
return courseScores;
}
}
class Class{
private String id; // 班级号
private Map students=new TreeMap<>(); //key:学号 学生列表
Class(String id){
this.id=id;//6位id
}
public void addStudent(String courseName,Score score,Student student){
boolean flag=true;
for(String id:students.keySet()){
if(student.getStudentID().equals(id)){
flag=false;
break;
}
}
if(flag){
this.students.put(student.getStudentID(),student);
this.students.get(student.getStudentID()).addCourseScore(courseName,score);
}
else{
this.students.get(student.getStudentID()).addCourseScore(courseName,score);
}
}
public void addStudent(String courseName,Student student){
boolean flag=true;
for(String id:students.keySet()){
if(student.getStudentID().equals(id)){
flag=false;
break;
}
}
if(flag){
this.students.put(student.getStudentID(),student);
}
}
//班级中每个学生的成绩的平均分
public int getClassAverageScore(){
double sum=0;
boolean flag=false;
for(String id:students.keySet()){
sum+=students.get(id).getStudentAverageScore();
if(students.get(id).getCourseScores().size()!=0){
flag=true;
}
}
sum=sum/(double) students.size();
if(!flag){
sum=-1;
}
return (int)sum;
}
public Map getStudents() {
return students;
}
public String getId() {
return id;
}
}
class Score{
private String assessmentMethod;
private int regularScore=-1;
private int finalScore;
private int score;
Score(){
}
Score(int regularScore,int finalScore){
this.regularScore=regularScore;
this.finalScore=finalScore;
}
Score(int finalScore){
this.finalScore=finalScore;
this.score=finalScore;
}
public int getRegularScore() {
return regularScore;
}
public int getFinalScore() {
return finalScore;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void setExamScores(){
setScore((int)(getRegularScore()*0.3+getFinalScore()*0.7));
}
public void setAssessmentScores(){
setScore(getFinalScore());
}
}
class Course{
private String courseName;
private String property; // 课程性质(必修/选修)
private String assessmentMethod; // 考核方式(考试/考察)
Course(String courseName,String property,String assessmentMethod){
this.assessmentMethod=assessmentMethod;
this.courseName=courseName;
this.property=property;
}
public String getAssessmentMethod() {
return assessmentMethod;
}
public String getCourseName() {
return courseName;
}
}
生成报表
踩坑心得
这里容易踩坑的地方就是对实验课的信息处理,因为实验课包含多个成绩,所以容易出错,还有便是实验课的错误信息处理,并且在原有的代码基础上新加入要求,就会对你代码规不规范起到检验。
改进建议
将成绩分为两个类并以组合关系运用。可以更方便的处理多个成绩的实验课。
课程成绩统计程序-3
设计与分析:
这一次我重新做了设计,其中的类包括Socre类 、SubTtemScore类 、CourseScore类 、Class类 、Student类 、Couse类。
以组合关系重建Score类,以SubTtemScore类 、CourseScore类组合。以达到更方便易懂的处理成绩信息。
将代码规范化,减少代码量,实现单一职责原则,将代码可读性提高,并对成绩信息先处理后,直接输出,大大减少了输出函数的负担。使代码更加简明易懂。
具体代码
import javax.print.DocFlavor;
import java.text.Collator;
import java.util.*;
public class Main {
public static boolean isValueFormat(String[] words){
//课程
if(isCourse(words)){
if(!words[2].matches("考试|考察|实验"))
return false;
if(words[2].equals("实验")){
if(words.length>3)
if(Integer.parseInt(words[3])<4||Integer.parseInt(words[3])>9)
return false;
}
if(words[2].equals("考试")){
if(words.length!=5)
return false;
}
if(words[2].equals("考察")){
if(words.length!=3)
return false;
}
}
else {//学生
if(words[0].length()!=8)
return false;
if(words[1].length()>10)
return false;
for(int i=3;i courses) {
boolean flag = true;
for (String name : courses.keySet()) {
if (words[0].equals(name)) {
flag = false;
break;
}
}
if(flag){
if (words[2].equals("考察")) {
float[] weight = {1};
courses.put(words[0], new Course(words[0], words[1], words[2], weight));
}
else if(words[2].equals("考试")){
float[]weight={Float.parseFloat(words[3]),Float.parseFloat(words[4])};//考试权重
courses.put(words[0],new Course(words[0],words[1],words[2],weight));
}
else {//实验权重
if (words.length <= 3) {
courses.put(words[0], new Course(words[0], words[1], words[2]));
} else {
float[] weight = new float[Integer.parseInt(words[3])];
for (int i = 0; i < weight.length; i++) {
weight[i] = Float.parseFloat(words[i + 4]);
}
courses.put(words[0], new Course(words[0], words[1], words[2], weight));
}
}
}
}
public static boolean isExist(String[] words,Map courses){
boolean flag=false;
for(String name:courses.keySet()){
if(words[2].equals(name)){
flag=true;
}
}
return flag;
}
public static boolean isCourse(String[] words){
if(words[1].matches("实验|必修|选修"))
return true;
return false;
}
public static boolean isMatch(String []words,Map courses){
boolean flag=true;
for(Map.Entry courseEntry:courses.entrySet()){
if(words[2].equals(courseEntry.getValue().getCourseName())){
if(words.length!=3+courseEntry.getValue().getWeight().length){
flag=false;
break;
}
}
}
return flag;
}
public static boolean isCourseTypeAndAccessModeMismatch(String []words){
boolean flag=false;
String CourseType=words[1];
String AccessMode=words[2];
if(CourseType.equals("必修")&&AccessMode.equals("考试")){
flag=true;
}
else if(CourseType.equals("选修")&&(AccessMode.equals("考试")||AccessMode.equals("考察"))){
flag=true;
}
else if(CourseType.equals("实验")&&AccessMode.equals("实验")){
flag=true;
}
return flag;
}
public static boolean isWeightValue(String []words){
if(words[2].equals("考察"))
return true;
if(words[2].equals("考试")){
if(Float.parseFloat(words[3])+Float.parseFloat(words[4])!=1)
return false;
}
if(words[2].equals("实验")){
float[]weight=new float[words.length-4];
float sum=0;
for(int i=0;i 1.01)
return false;
}
return true;
}
public static boolean isNumberMatch(String []words){
if(words.length>3&&Float.parseFloat(words[3])!=words.length-4)
return false;
return true;
}
public static void getData(Map courses,Map classes){
Scanner input=new Scanner(System.in);
ArrayListarrayList=new ArrayList<>();
String line=input.nextLine();
while(!line.equals("end")){
boolean hasMatched=true;
String words[]=line.split("\\s+");
if(!isValueFormat(words)){
System.out.println("wrong format");
line=input.nextLine();
continue;
}
if(!isCourse(words)&&!isExist(words,courses)){
System.out.println(words[2]+" does not exist");
line=input.nextLine();
continue;
}
else if(!isCourse(words)&&!isMatch(words,courses)){
System.out.println(words[0]+" "+words[1]+" : access mode mismatch");
hasMatched=false;
}
else if(isCourse(words)&&!isCourseTypeAndAccessModeMismatch(words)){
System.out.println(words[0] + " : course type & access mode mismatch");
line=input.nextLine();
continue;
}//" : course type & access mode mismatch"
if(words[2].equals("实验")&&!isNumberMatch(words)){
System.out.println(words[0] + " : number of scores does not match");
line=input.nextLine();
continue;
}
if(words.length>3&&isCourse(words)&&!isWeightValue(words)){
System.out.println(words[0] + " : weight value error");
line=input.nextLine();
continue;
}
boolean repeat=false;
for(String lines:arrayList){
if(line.equals(lines)){
repeat=true;
}
}
arrayList.add(line);
if(repeat){
line=input.nextLine();
continue;
}//重复
if(isCourse(words))
addCourses(words,courses);
else if(words.length==4){
if(hasMatched){
classes=addClass(words[0].substring(0,6),classes);
Score score=new Score();
int []scoreTroop={Integer.parseInt(words[3])};
score.setSubItemScore(new SubItemScore(scoreTroop));
classes.get(words[0].substring(0,6)).addStudent(words[2],score,new Student(words[0],words[1]));
}
else{
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Student(words[0],words[1]));
}
}
else if(words.length==5){
if(hasMatched) {
Score score=new Score();
int []scoreTroop={Integer.parseInt(words[3]),Integer.parseInt(words[4])};
score.setSubItemScore(new SubItemScore(scoreTroop));
classes = addClass(words[0].substring(0, 6), classes);
classes.get(words[0].substring(0, 6)).addStudent(words[2], score, new Student(words[0], words[1]));
}
else{
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Student(words[0],words[1]));
}
}
else{//输入学生实验信息
if(!hasMatched){
classes=addClass(words[0].substring(0,6),classes);
classes.get(words[0].substring(0,6)).addStudent(words[2],new Student(words[0],words[1]));
}
else{
classes=addClass(words[0].substring(0,6),classes);
Score score=new Score();
int []scoreTroop=new int[words.length-3];
for(int i=0;i addClass(String id,Map classes){
boolean flag=true;
for(String classesId:classes.keySet()){
if(id.equals(classesId)){
flag=false;
}
}
if(flag){
Class newClass=new Class(id);
classes.put(id,newClass);
}
return classes;
}
public static void operationData(Map courses,Map classes){
for(Map.Entry classEntry:classes.entrySet()){
for(Map.EntrystringStudentEntry:classEntry.getValue().getStudents().entrySet()){
for(Map.EntrystringScoreEntry:stringStudentEntry.getValue().getCourseScores().entrySet()){
for(Map.EntrycourseEntry:courses.entrySet()){
if(stringScoreEntry.getKey().equals(courseEntry.getKey())){
stringScoreEntry.getValue().getSubItemScore().setWeight(courseEntry.getValue().getWeight());
int sum=stringScoreEntry.getValue().getSubItemScore().getTotalScore();
if(courseEntry.getValue().getAssessmentMethod().equals("考试")){
int dailyScore=stringScoreEntry.getValue().getSubItemScore().getDailyScore();
int finalScore=stringScoreEntry.getValue().getSubItemScore().getFinalScore();
courseEntry.getValue().addDailyScore(dailyScore);
courseEntry.getValue().addFinalScore(finalScore);
}
else if(courseEntry.getValue().getAssessmentMethod().equals("考察")){
int finalScore=stringScoreEntry.getValue().getSubItemScore().getDailyScore();
courseEntry.getValue().addFinalScore(finalScore);
}
// stringScoreEntry.getValue().getCourseScore().setTotalScore(stringScoreEntry.getValue().getSubItemScore().getTotalScore());
stringScoreEntry.getValue().getCourseScore().setTotalScore(sum);
courseEntry.getValue().addScore(stringScoreEntry.getValue().getSubItemScore().getTotalScore());
classEntry.getValue().addScore(stringScoreEntry.getValue().getSubItemScore().getTotalScore());
}
}
}
}
}
}
public static void printfResult(Map courses,Map classes){
for(Map.Entry classEntry: classes.entrySet()){//班级列表遍历
for(Map.Entry studentEntry:classEntry.getValue().getStudents().entrySet()) {//学生列表遍历
if(studentEntry.getValue().getCourseScores().size()==0){
System.out.println(studentEntry.getKey()+" "+studentEntry.getValue().getStudentName() +" did not take any exams");
}
else
System.out.println(studentEntry.getValue().getStudentID()+" "+studentEntry.getValue().getStudentName()+" "+String.format("%d",studentEntry.getValue().getStudentAverageScore()));
}
}
for(Map.EntrycourseEntry:courses.entrySet()){
if(courseEntry.getValue().getScore()==-1){
System.out.println(courseEntry.getKey()+" has no grades yet");
}
else {
System.out.println(courseEntry.getKey() + " " + courseEntry.getValue().getAverageScore());
}
}
for(Map.EntryclassEntry:classes.entrySet()){
if(classEntry.getValue().getAverageScore()==-1){
System.out.println(classEntry.getKey()+" has no grades yet");
}
else{
System.out.println(classEntry.getKey()+" "+classEntry.getValue().getAverageScore());
}
}
}
public static void main(String[] args){
Collator collator = Collator.getInstance(Locale.CHINA);
Map courses=new TreeMap< >(collator);//课程信息,key:CourseName
Map classes=new TreeMap<>();//班级信息,key:classID
getData(courses,classes);
operationData(courses,classes);
printfResult(courses,classes);
}
}
class Student{
private String studentID;
private String studentName;
private MapcourseScores=new TreeMap<>();//key:CourseName
Student(String studentID,String studentName){
this.studentID=studentID;
this.studentName=studentName;
}
public void addCourseScore(String courseName,Score score){
boolean flag=true;
for(String name:courseScores.keySet()){
if(courseName.equals(name)){
flag=false;
break;
}
}
if(flag){
courseScores.put(courseName,score);
}
else{
//当课程成绩重复输入,只记录第一次输入
}
}
//添加平时成绩,期末成绩,最终成绩
public boolean isRepeat(String name){
boolean flag=true;
for(String courseName:courseScores.keySet()){
if(courseName.equals(name)){
flag=false;
break;
}
}
return !flag;
}
//最终学生成绩平均分
public int getStudentAverageScore(){
float sum=0;
for(String name:courseScores.keySet()){
sum+=courseScores.get(name).getCourseScore().getTotalScore();
}
sum=sum/(float)(courseScores.size());
return (int)sum;
}
public String getStudentName() {
return studentName;
}
public String getStudentID() {
return studentID;
}
public Map getCourseScores() {
return courseScores;
}
}
class Class{
private String id; // 班级号
private Map students=new TreeMap<>(); //key:学号 学生列表
private int score=-1;
private int number=0;
Class(String id){
this.id=id;//6位id
}
public void addStudent(String courseName,Score score,Student student){
boolean flag=true;
for(String id:students.keySet()){
if(student.getStudentID().equals(id)){
flag=false;
break;
}
}
if(flag){
this.students.put(student.getStudentID(),student);
this.students.get(student.getStudentID()).addCourseScore(courseName,score);
}
else{
this.students.get(student.getStudentID()).addCourseScore(courseName,score);
}
}
public void addScore(int s){
if(this.score==-1){
score=0;
}
this.score+=s;
this.number++;
}
public int getAverageScore() {
if(number==0){
return -1;
}
return (int)(score/(float)number);
}
public void addStudent(String courseName, Student student){
boolean flag=true;
for(String id:students.keySet()){
if(student.getStudentID().equals(id)){
flag=false;
break;
}
}
if(flag){
this.students.put(student.getStudentID(),student);
}
}
//班级中每个学生的成绩的平均分
public int getClassAverageScore(){
float sum=0;
boolean flag=false;
for(String id:students.keySet()){
sum+=students.get(id).getStudentAverageScore();
if(students.get(id).getCourseScores().size()!=0){
flag=true;
}
}
sum=sum/(float) students.size();
if(!flag){
sum=-1;
}
return (int)sum;
}
public Map getStudents() {
return students;
}
public String getId() {
return id;
}
}
class Score{
private CourseScore courseScore=new CourseScore();
private SubItemScore subItemScore;
public void setCourseScore(CourseScore courseScore) {
this.courseScore = courseScore;
}
public void setSubItemScore(SubItemScore subItemScore) {
this.subItemScore = subItemScore;
}
public CourseScore getCourseScore() {
return courseScore;
}
public SubItemScore getSubItemScore() {
return subItemScore;
}
}
class CourseScore{
private int totalScore;
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
public int getTotalScore() {
return totalScore;
}
}
class SubItemScore{
private float[] weight;//权重
private int[] itemScore;
SubItemScore(int[] itemScore){
this.itemScore=itemScore;
}
public int getDailyScore(){
return itemScore[0];
}
public int getFinalScore(){
return itemScore[1];
}
public void setWeight(float[] weight) {
this.weight = weight;
}
public int getTotalScore(){
float sum=0;
for(int i=0;i
生成报表
踩坑心得
这一题有一个大坑,便是要把成绩的;类型改为float,才能过,这一个卡了我好久。还有便是实验成绩的计算,看清题目后,将成绩与权重相乘得到最终成绩。
改进建议
将信息处理这一块的代码再优化一下,比如程序重复录入这一块,还有一些不规范,以及对信息的判定没有统一。
总结:
在本阶段(10-16周)学习了面向对象编程的基本概念和原则,学习了面向对象的思维方式和设计方法,包括封装、继承、多态等概念,掌握了面向对象编程的基本原则和技巧。以及各种设计模式,例如工厂方法模式、简单工厂模式、单例模式、建造者模式、桥接模式、观察者模式、外观模式。而对于自身来说这些模式的熟练程度还不够,不能正确的应用 到实际问题当中,在这一部分还需要加强学习和积累。其实对于后面fx的翻转课堂感觉还不错,如果每个人都写一个作品的话应该会对fx的熟练度会更高。
课程的教学评价
教学理念(OBE)
老师的教学理念其实一直都很清晰,就是让学生理解OPP,面向对象的理念,培养的是编程思维,抽象思维。但是可能在老师授课过程中会理解上与老师不同,但是在后面的课上实践中又会多出老师讲不到的那种体悟。我觉得还是需要一节课讲,一节课实践。但是在授课中我还是喜欢多看实例,或者实际解决问题。这样印象和体悟会更深一点。
教学方法(边讲边练):
我是很喜欢边讲边练的,以为听老师讲和自己实践是两种不同的体悟,当两者一起的时候会对前面老师讲不到或者没听懂的地方,有自己的理解。
教学过程(PTA题目集驱动):
PTA题目的难度有些起伏不定,有时候很多,但有时候又很少,而且希望能在PTA中多给一些测试用例,以更好的检测自己的代码,更好调整和修改。
教学模式(BOPPPS):
客观性评价:BOPPPS教学模式注重学生的参与和主动学习,通过引导、解释、实践等阶段来促进学生的学习效果。
总体来说老师一直在引导我们来去主动探索更多的解决办法和学习。可能在教学方法上可以更多的多样化一点。促进学生的主观能动性。