一、前言:
-
第一次pta作业:
1.知识点:简单的类的设计,类和对象的使用,类的数组类型的基本应用,类间的关联等基础的类间关系设计,实现对类的封装等操作。
2.题量:4道简单题,1道迭代的难题。
3.难度:前4道题比较简单;最后一道第一次写难度较大。 -
第二次pta作业:
1.知识点:考察类实现Comparable接口,重写compareTo方法。类的属性基本计算。
2.题量:3道简单题,1道迭代难题。
3.难度:这次最后一道题在上次的基础上添加了多张试卷和答卷。在第一次的基础上难度增加的不算太大。 -
第三次pta作业:
1.知识点:类的封装性,日期类的基本使用,类间关系的设计。
2.题量:2道简单题,1道迭代的难题。
3.难度:感觉最后一题这次难度在第二次作业上大幅加大。
二、设计分析
- 第一次pta作业:
1.Question类:这个类表示一个问题,包括问题的编号、问题内容和问题答案。通过构造函数初始化问题的信息,并且提供打印问题内容的方法以及获取问题答案的方法。
点击查看代码
class Question{
private int num;
private String question;
private String answer;
public Question(int num,String question,String answer){
this.num=num;
this.question=question;
this.answer=answer;
}
public void printQuestion(){
System.out.println("num="+num+"!question="+question+"!ans="+answer);
}
public String getAnswer(){return answer;}
public String getQuestion(){return question;}
}
点击查看代码
class Test{
private int quantity;
private Question[] text_paper;
public Test(Question[] question,int quantity){
this.text_paper=question;
this.quantity=quantity;
}
}
点击查看代码
class AnswerSheet{
private int quantity;
private String[] answer;
private boolean[] result;
public AnswerSheet(int quantity,String[] answer){
this.quantity=quantity;
this.answer=answer;
this.result=new boolean[quantity+1];
for(int i=0;i<=quantity;i++) this.result[i]=false;
}
public void judge(Question[] questions){
for(int i=1;i<=quantity;i++){
String standardAnswer=questions[i].getAnswer();
if(answer[i].equals(standardAnswer)==true) result[i]=true;
else result[i]=false;
}
}
public void printSheet(){
for(int i=1;i<=quantity;i++){
System.out.print("answer=["+answer[i]+"] result=["+result[i]+"]\n");
}
}
public String getindexOfAnswer(int i){
return answer[i];
}
public boolean getindexOfResult(int i){
return result[i];
}
}
点击查看代码
public class Main{
public static int getnum(String str){
String a[]=str.split("#N: *");
String b[]=a[1].split(" *#Q");
int res=Integer.parseInt(b[0]);
return res;
}
public static String getque(String str){
String a[]=str.split("#Q: *");
String b[]=a[1].split(" *#A");
return b[0];
}
public static String getans(String str){
str+="#A:";
String a[]=str.split(" *#A: *");
return a[1];
}
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int quantity=in.nextInt();
in.nextLine();
Question[] questions=new Question[quantity+1];
for(int i=1;i<=quantity;i++){
String str=in.nextLine();
int num=getnum(str);
String question=getque(str);
String answer=getans(str);
questions[num]=new Question(num,question,answer);
}
//for(int i=1;i<=quantity;i++) questions[i].printQuestion();
Test text=new Test(questions,quantity);
if(quantity!=0){
String str=in.nextLine();
String ans[]=str.split(" ?#A:");
AnswerSheet sheet = new AnswerSheet(quantity,ans);
//for(int i=1;i<=quantity;i++) System.out.printf("a[%d]=%s\n",i,ans[i]);
sheet.judge(questions);
for(int i=1;i<=quantity;i++) System.out.println(questions[i].getQuestion()+"~"+sheet.getindexOfAnswer(i));
System.out.print(sheet.getindexOfResult(1));
for(int i=2;i<=quantity;i++) System.out.print(" "+sheet.getindexOfResult(i));
}
//String end=in.nextLine();
}
}
- 第二次pta作业:
1.Question类:表示一个问题,包含问题编号、问题内容和标准答案。提供了获取问题信息的方法。
点击查看代码
class Question{
private int num;
private String question;
private String standardAnswer;
public Question(int num,String question,String standardAnswer){
this.num=num;
this.question=question;
this.standardAnswer=standardAnswer;
}
public int getNum() {
return num;
}
public String getQuestion() {
return question;
}
public String getStandardAnswer() {
return standardAnswer;
}
}
点击查看代码
class TestPaper{
private int amount;
private int testID;
private Question[] testPaper;
private int[] score;
private int sumScore=0;
public TestPaper(int n,int testID){
this.amount=n;
this.testID=testID;
testPaper=new Question[n+1];
score=new int[n+1];
}
public void add(int n,Question question,int score){
this.testPaper[n]=question;
this.score[n]=score;
}
public void GenerateSumScore(){
for(int i=1;i<=amount;i++) sumScore+=score[i];
}
public int getTestID(){
return testID;
}
public int getSumScore(){
return sumScore;
}
public int getAmount() {
return amount;
}
public String getQuestion(int n){
return testPaper[n].getQuestion();
}
public String getStandardAnswer(int n){
return testPaper[n].getStandardAnswer();
}
public int getScore(int n) {
return score[n];
}
}
点击查看代码
class AnswerSheet{
int amount;
int testID;
private String[] questions;
private String[] answers;
private boolean[] results;
private int[] scores;
public AnswerSheet(int testID,TestPaper testPaper,String[] answer){
int n=testPaper.getAmount();
this.amount=n;
this.testID=testID;
this.questions=new String[n+1];
this.answers=new String[n+1];
this.results=new boolean[n+1];
this.scores=new int[n+1];
this.answers=answer;
for (int i=1;i<=n;i++) questions[i]=testPaper.getQuestion(i);
}
public int getTestID(){
return testID;
}
public void judge(TestPaper testPaper){
int i=0;
for(i=1;i<answers.length;i++){
String standardAnswer=testPaper.getStandardAnswer(i);
if(answers[i].equals(standardAnswer)){
results[i]=true;
scores[i]=testPaper.getScore(i);
}
else{
results[i]=false;
scores[i]=0;
}
}
for(;i<=amount;i++){
results[i]=false;
scores[i]=0;
}
}
public void print(){
int i;
for(i=1;i<answers.length;i++) {
System.out.println(questions[i]+"~"+answers[i]+"~"+results[i]);
}
for(;i<=amount;i++) System.out.println("answer is null");
int sumScore=scores[1];
System.out.print(scores[1]);
for(i=2;i<=amount;i++){
System.out.print(" "+scores[i]);
sumScore+=scores[i];
}
System.out.println("~"+sumScore);
}
}
点击查看代码
public class Main {
public static HashMap<Integer,Question> questions=new HashMap<>();
public static HashMap<Integer,TestPaper> tests=new HashMap<>();
public static ArrayList<AnswerSheet> answerSheets=new ArrayList<>();
public static int max=0;
public static void addQuestion(String str){
Pattern pattern=Pattern.compile("#N:(.+)#Q:(.+)#A:(.+)");
Matcher matcher=pattern.matcher(str);
if(matcher.find()){
int n=Integer.parseInt(matcher.group(1).trim());
String question=matcher.group(2).trim();
String standardAnswer=matcher.group(3).trim();
Question ques=new Question(n,question,standardAnswer);
questions.put(n,ques);
}
}
public static void addTestPaper(String str){
String[] t=str.split("-");
int amount=t.length-1;
int i=0;
Matcher matcherAmount=Pattern.compile("#T: *(\\d+)").matcher(str);
if(matcherAmount.find()) i=Integer.parseInt(matcherAmount.group(1));
if(max<i) max=i;
TestPaper test=new TestPaper(amount,i);
Pattern pattern=Pattern.compile("(\\d+)-(\\d+)");
Matcher matcher=pattern.matcher(str);
int cnt=0;
while(matcher.find()){
int n=Integer.parseInt(matcher.group(1));
int score=Integer.parseInt(matcher.group(2));
test.add(++cnt,questions.get(n),score);
}
test.GenerateSumScore();
tests.put(i,test);
}
public static void addAnswerSheet(String str){
int i=0;
Matcher matcher1=Pattern.compile("#S: *(\\d+)").matcher(str);
if(matcher1.find()){
i=Integer.parseInt(matcher1.group(1));
}
String[] answer=str.split(" *#A: *");
int amount=answer.length-1;//ans[0]="";
answer[amount].trim();
AnswerSheet answerSheet;
if(i>max){
answerSheet=new AnswerSheet(i,tests.get(1),answer);
}
else answerSheet=new AnswerSheet(i,tests.get(i),answer);
answerSheets.add(answerSheet);
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str;
while(!(str=in.nextLine()).equals("end")){
if(str.contains("#N:")) addQuestion(str);
else if(str.contains("#T:")) addTestPaper(str);
else if(str.contains("#S:")) addAnswerSheet(str);
}
for(TestPaper i:tests.values()){
if(i.getSumScore()!=100) {
System.out.println("alert: full score of test paper"+i.getTestID()+" is not 100 points");
}
}
for(AnswerSheet i:answerSheets){
int k=i.getTestID();
if(tests.get(k)==null) System.out.println("The test paper number does not exist");
else{
i.judge(tests.get(k));
i.print();
}
}
}
- 第三次pta作业:
1.Question类:表示一个问题,包含问题的编号(num)、表示问题的内容(question)、表示问题的标准答案(standardAnswer)、表示问题的状态(status)。提供了获取问题的编号、获取问题的内容、获取问题的标准答案获取问题的状态、设置问题的状态的方法。
点击查看代码
class Question{
private int num;
private String question;
private String standardAnswer;
private int status;
public Question(int num,String question,String standardAnswer){
this.num=num;
this.question=question;
this.standardAnswer=standardAnswer;
this.status=1;
}
public int getNum() {
return num;
}
public String getQuestion() {
return question;
}
public String getStandardAnswer() {
return standardAnswer;
}
public int getStatus(){
return status;
}
public void setStatus(int status){
this.status=status;
}
}
点击查看代码
class TestPaper{
private int amount;
private int testID;
private Question[] questions;
private int[] status;
private int[] score;
private int sumScore;
public TestPaper(int testID,int amount){
this.amount=amount;
this.testID=testID;
questions=new Question[amount+1];
status=new int[amount+1];
score=new int[amount+1];
}
public void add(int i,Question question,int score){
this.questions[i]=question;
this.score[i]=score;
this.status[i]=question.getStatus();
}
public void add(int n){
this.status[n]=2;
}
public void delete(int n){
for(int i=1;i<=amount;i++){
if(status[i]==1&&questions[i].getNum()==n){
status[i]=3;
}
}
}
public int getTestID(){
return testID;
}
public int getAmount() {
return amount;
}
public String getQuestion(int n){
return questions[n].getQuestion();
}
public int getNum(int n){
return questions[n].getNum();
}
public String getStandardAnswer(int n){
return questions[n].getStandardAnswer();
}
public int getStatus(int n){
return status[n];
}
public int getScore(int n) {
return score[n];
}
public int getSumscore() {
return sumScore;
}
public int getSumScore(){
int sumScore=0;
for(int i=1;i<=amount;i++) sumScore+=score[i];
return sumScore;
}
public void setStatus1(int n){
this.status[n]=1;
}
public void setSumscore(int Sumscore){
this.sumScore=Sumscore;
}
}
点击查看代码
class AnswerSheet{
int testID;
int amount;
String studentID;
private String[] questions;
private String[] answers;
private boolean[] results;
private int[] scores;
public AnswerSheet(int testID,int amount,String studentID){
this.testID=testID;
this.studentID=studentID;
this.amount=amount;
this.questions=new String[amount+1];
this.answers=new String[amount+1];
this.results=new boolean[amount+1];
this.scores=new int[amount+1];
}
public AnswerSheet(int testID){
this.testID=testID;
}
public void add(int n,String answer){
this.answers[n]=answer;
}
public int getTestID(){
return testID;
}
public String getAnswer(int i){
return answers[i];
}
public boolean getResult(int i){
return results[i];
}
public String getStudentID(){
return studentID;
}
public void setResult(int i,boolean result){
this.results[i]=result;
}
public void setScore(int i,int score){
this.scores[i]=score;
}
public void print(){
int sumScore=0;
for(int i=1;i<=amount;i++) {
sumScore+=scores[i];
System.out.print(" "+scores[i]);
}
System.out.println("~"+sumScore);
}
}
点击查看代码
class Student{
private String ID;
private String name;
public 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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
点击查看代码
class Judge{
private TestPaper testPaper;
private AnswerSheet answerSheet;
public Judge(TestPaper testPaper,AnswerSheet answerSheet){
this.testPaper=testPaper;
this.answerSheet=answerSheet;
}
public void correct(HashMap<Integer,Question> questionBank){
for(int i=1;i<=testPaper.getAmount();i++){
if(answerSheet.getAnswer(i)!=null){
if(testPaper.getStatus(i)==2){
System.out.println("non-existent question~0");
}
else if(testPaper.getStatus(i)==3)
System.out.printf("the question %d invalid~0\n",testPaper.getNum(i));
else{
String standardAnswer=testPaper.getStandardAnswer(i);
String answer=answerSheet.getAnswer(i);
if(answer.equals(standardAnswer)){
answerSheet.setResult(i,true);
answerSheet.setScore(i,testPaper.getScore(i));
}
else{
answerSheet.setResult(i,false);
answerSheet.setScore(i,0);
}
System.out.println(testPaper.getQuestion(i)+"~"+answer+"~"+answerSheet.getResult(i));
}
}
else System.out.println("answer is null");
}
}
}
点击查看代码
public class Main {
public static HashMap<Integer,Question> questionBank=new HashMap<>();
public static HashMap<Integer,TestPaper> tests=new HashMap<>();
public static HashMap<String,Student> students=new HashMap<>();
public static ArrayList<AnswerSheet> answerSheets=new ArrayList<>();
public static void addQuestion(String str){
String regex="^#N: *(\\d+) *#Q:(.*)#A:(.*)";
Matcher format=Pattern.compile(regex).matcher(str);
if(!format.find()){
System.out.println("wrong format:"+str);
return;
}
Pattern pattern=Pattern.compile("#N:(.*)#Q:(.*)#A:(.*)");
Matcher matcher=pattern.matcher(str);
if(matcher.find()){
int n=Integer.parseInt(matcher.group(1).trim());
String question=matcher.group(2).trim();
String standardAnswer=matcher.group(3).trim();
Question ques=new Question(n,question,standardAnswer);
questionBank.put(n,ques);
}
}
public static void addTestPaper(String str){
String regex="^#T: *\\d+ *((\\d+ *- *\\d+ *)+)$";
Matcher format=Pattern.compile(regex).matcher(str);
if(!format.find()){
System.out.println("wrong format:"+str);
return;
}
int testID=0;
int amount=str.split("-").length-1;
Matcher matcherAmount=Pattern.compile("#T: *(\\d+)").matcher(str);
if(matcherAmount.find()) testID=Integer.parseInt(matcherAmount.group(1));
TestPaper test=new TestPaper(testID,amount);
Pattern pattern=Pattern.compile("(\\d+) *- *(\\d+)");
Matcher matcher=pattern.matcher(str);
int cnt=0;
int sum=0;
while(matcher.find()){
int n=Integer.parseInt(matcher.group(1));
int score=Integer.parseInt(matcher.group(2));
sum+=score;
if(questionBank.containsKey(n)) test.add(++cnt,questionBank.get(n),score);
else test.add(++cnt);
}
test.setSumscore(sum);
tests.put(testID,test);
}
public static void addAnswerSheet(String str){
String regex="^#S: *\\d+ +\\d+ *(.*?)$";
Matcher format=Pattern.compile(regex).matcher(str);
if(!format.find()){
System.out.println("wrong format:"+str);
return;
}
int testID=0;
String studentID="";
Matcher matcher1=Pattern.compile("#S: *(\\d+) +(\\d+)").matcher(str);
if(matcher1.find()) {
testID=Integer.parseInt(matcher1.group(1));
studentID=matcher1.group(2);
}
//if(!students.containsKey(studentID)) studentID="-1";
if(tests.containsKey(testID)){
int amount=tests.get(testID).getAmount();
AnswerSheet answerSheet=new AnswerSheet(testID,amount,studentID);
Matcher matcher=Pattern.compile("#A: *(\\d+) *- *(.*?) *(?=#A:|\\n|$)").matcher(str);
while(matcher.find()){
int questionID=Integer.parseInt(matcher.group(1).trim());
String answer=matcher.group(2).trim();
amount=tests.get(testID).getAmount();
if(questionID>amount) continue;
//if(questions.get(questionID)!=null) continue;
answerSheet.add(questionID,answer);
}
answerSheets.add(answerSheet);
//answerSheet.print();
}
else{
AnswerSheet answerSheet=new AnswerSheet(testID);
answerSheets.add(answerSheet);
}
}
public static void addStudent(String str){
String regex="^#X: *( *\\d+ *\\w+ *-*)*$";
Matcher format=Pattern.compile(regex).matcher(str);
if(!format.find()){
System.out.println("wrong format:"+str);
return;
}
Matcher matcher=Pattern.compile("(\\d+) *(\\w+)").matcher(str);
while(matcher.find()){
String ID=matcher.group(1),name=matcher.group(2);
if(students.containsKey(ID)) continue;
Student student=new Student(ID,name);
students.put(ID,student);
}
}
public static void deleteOp(String str){
String regex="^#D: *N *- *(.+)$";
Matcher format=Pattern.compile(regex).matcher(str);
if(!format.find()){
System.out.println("wrong format:"+str);
return;
}
Matcher matcher=Pattern.compile("(\\w) *- *(\\d+)").matcher(str);
if(matcher.find()){
int n=Integer.parseInt(matcher.group(2));
if(questionBank.containsKey(n)){
questionBank.get(n).setStatus(3);
for(TestPaper i:tests.values())
i.delete(n);
}
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
ArrayList<String> pretreatment = new ArrayList<>();
String str;
while(true){
str=in.nextLine();
if(str.contains("end")) break;
if(str.contains("#N:")) addQuestion(str);
else if(str.contains("#D:")) deleteOp(str);
else if(str.contains("#T:")) addTestPaper(str);
else if(str.contains("#S:")) addAnswerSheet(str);
else if(str.contains("#X:")) addStudent(str);
else System.out.println("wrong format:"+str);
}
for(TestPaper i:tests.values()){
if(i.getSumscore()!=100)
System.out.printf("alert: full score of test paper%d is not 100 points\n",i.getTestID());
}
for(AnswerSheet i:answerSheets){
int testID=i.getTestID();
if(!tests.containsKey(testID)){
System.out.println("The test paper number does not exist");
continue;
}
Judge judge=new Judge(tests.get(testID),i);
judge.correct(questionBank);
String studentID=i.getStudentID();
if(!students.containsKey(studentID)){
System.out.println( studentID+" not found");
continue;
}
System.out.print(studentID+" "+students.get(studentID).getName()+":");
i.print();
}
}
}
三、踩坑心得
- 第一次pta作业:
1.输入的内容可能有多个空格,用split分开字符串之后应当删除首尾空格,不然会有两个测试点格式错误。 - 第二次pta作业:
1.这次题目都是一遍通过所有样例,没有踩太多的坑。 - 第三次pta作业:
1.一开始匹配信息的各个正则表达式都有一些错误,比如匹配题目没用用匹配字符串首的"^"导致一些格式错误的题目输入没有被匹配到。
2.没有对题目的空答案和答卷的空答案做处理导致有4个缺失答案的测试点一直没过。
3.最后一题的输出顺序不对,最后有4个点因为Wrong Format的输出顺序和试卷是否满分的顺序不对导致一直没过。
四、改进建议
- 第一次pta作业:
1.输入的字符串里的信息可以用正则表达式去匹配。这样就不会匹配无用的空格,就不会有格式错误。 - 第二次pta作业:
1.最后一题第一次提交是用数组来储存试卷与答卷,后来觉得用数组不太合理,于是改成了用HashMap来存 - 第三次pta作业:
1.第三次各个类中的功能要更复杂,导致一个类中方法过多,应当适当修改设计,把一些与类关系不大的方法分离出去单独设计成一个类,而不是全放在一个类中,比如前两次Judge这个方法是放在AnswerSheet类中的,但其实这个Judge可以单独拎出来设计成一个判题类。还有解析输入的字符串并提取内容这些方法,可以单独设计成一个类而不是放在Main类中,让Main过于臃肿。以及可以单独设计一个类用来保存题目、试卷、答卷等数据。
五、总结
- 这三次作业学会了怎么用正则表达式,学会了一些基本的设计原理,比如单一职责、开闭原则、迪米特法则等。看了老师发的设计思路,发现自己还有好多需要修改的东西,比如我只设计了最基本的那几个类,题目试卷答卷类,应当修改修改类的设计。