一,前言
一-1.第四次题集是接着前面的第三次题集的迭代,知识点主要是继承的运用,正则表达式的运用同样少不了,相对于前面一次添加了对选题以及填空题,难度也相对于前一次加大。
一-2.第五次题集是新的题集迭代是有关电路的,知识点有抽象类,抽象方法的使用以及继承的使用,同样正则表达式;由于没有考虑电阻问题只有一个用电器,也是只有一条串联电路,难度比较小。
一-3.第六次是第五次的迭代,知识点有抽象类,抽象方法,继承,接口,正则表达式。相对于上一次添加了串联电路,同时考虑电阻,所以难度有所提升。
二,设计分析
二-1,答判断-4
源代码如下:
点击查看代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, Question> questionMap = new HashMap<>();
Map<String, TestPaper> testPaperMap = new HashMap<>();
Map<String, Student> studentMap = new HashMap<>();
List<Answer> answerList = new ArrayList<>();
int totalScore = 0;
boolean isAlert = false;
while (true) {
String input = scanner.nextLine();
if (input.equals("end")) {
break;
}
String[] inputs = input.split(" ");
String type = inputs[0];
String output1 = type.substring(0, 2);
switch (output1) {
case "#N":
Question question = new Question(inputs[0], inputs[1], inputs[2]);
questionMap.put(inputs[1], question);
break;
case "#T":
TestPaper testPaper = new TestPaper(inputs[1]);
for (int i = 2; i < inputs.length; i += 2) {
testPaper.addQuestion(questionMap.get(inputs[i]), Integer.parseInt(inputs[i + 1]));
}
testPaperMap.put(inputs[1], testPaper);
if (testPaper.getTotalScore()!= 100) {
isAlert = true;
}
break;
case "#X":
Student student = new Student(inputs[1]);
for (int i = 2; i < inputs.length; i += 2) {
student.addName(inputs[i]);
}
studentMap.put(inputs[1], student);
break;
case "#S":
Answer answer = new Answer(inputs[1], inputs[3]);
for (int i = 4; i < inputs.length; i += 2) {
answer.addAnswer(questionMap.get(inputs[i]), inputs[i + 1]);
}
answerList.add(answer);
break;
case "#D":
questionMap.remove(inputs[2]);
break;
default:
//System.out.println("wrong format: " + input);
break;
}
}
for (Answer answer : answerList) {
TestPaper testPaper = testPaperMap.get(answer.getTestPaperNumber());
Student student = studentMap.get(answer.getStudentNumber());
if (testPaper == null) {
System.out.println("the test paper number does not exist");
continue;
}
if (student == null) {
//System.out.println("wrong format: student number does not exist");
continue;
}
int totalScoreForStudent = 0;
List<String> result = new ArrayList<>();
for (Map.Entry<Question, String> entry : answer.getAnswers().entrySet()) {
Question question = entry.getKey();
String studentAnswer = entry.getValue();
String correctAnswer = question.getCorrectAnswer();
int questionScore = testPaper.getQuestionScore(question);
if (question instanceof ChoiceQuestion) {
if (studentAnswer.equals(correctAnswer)) {
totalScoreForStudent += questionScore;
result.add("true");
} else {
result.add("false");
}
} else if (question instanceof FillQuestion) {
if (studentAnswer.equalsIgnoreCase(correctAnswer)) {
totalScoreForStudent += questionScore;
result.add("true");
} else {
result.add("false");
}
}
}
if (result.size() < testPaper.getQuestions().size()) {
for (int i = result.size(); i < testPaper.getQuestions().size(); i++) {
result.add("answer is null");
}
}
System.out.println(student.getStudentNumber() + " " + student.getStudentName() + ": " + String.join(" ", result) + "~" + totalScoreForStudent);
totalScore += totalScoreForStudent;
}
if (isAlert) {
System.out.println("alert: full score of test paper is not 100 points");
}
scanner.close();
}
}
class Question {
private String questionNumber;
private String questionContent;
private String correctAnswer;
public Question(String questionNumber, String questionContent, String correctAnswer) {
this.questionNumber = questionNumber;
this.questionContent = questionContent;
this.correctAnswer = correctAnswer;
}
public String getQuestionNumber() {
return questionNumber;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
class ChoiceQuestion extends Question {
public ChoiceQuestion(String questionNumber, String questionContent, String correctAnswer) {
super(questionNumber, questionContent, correctAnswer);
}
}
class FillQuestion extends Question {
public FillQuestion(String questionNumber, String questionContent, String correctAnswer) {
super(questionNumber, questionContent, correctAnswer);
}
}
class TestPaper {
private String testPaperNumber;
private Map<Question, Integer> questions;
public TestPaper(String testPaperNumber) {
this.testPaperNumber = testPaperNumber;
this.questions = new HashMap<>();
}
public void addQuestion(Question question, int score) {
questions.put(question, score);
}
public int getTotalScore() {
int totalScore = 0;
for (int score : questions.values()) {
totalScore += score;
}
return totalScore;
}
public int getQuestionScore(Question question) {
return questions.get(question);
}
public Set<Question> getQuestions() {
return questions.keySet();
}
}
class Student {
private String studentNumber;
private List<String> studentNames;
public Student(String studentNumber) {
this.studentNumber = studentNumber;
this.studentNames = new ArrayList<>();
}
public void addName(String studentName) {
studentNames.add(studentName);
}
public String getStudentNumber() {
return studentNumber;
}
public String getStudentName() {
return String.join(" ", studentNames);
}
}
class Answer {
private String testPaperNumber;
private String studentNumber;
private Map<Question, String> answers;
public Answer(String testPaperNumber, String studentNumber) {
this.testPaperNumber = testPaperNumber;
this.studentNumber = studentNumber;
this.answers = new HashMap<>();
}
public void addAnswer(Question question, String answer) {
answers.put(question, answer);
}
public String getTestPaperNumber() {
return testPaperNumber;
}
public String getStudentNumber() {
return studentNumber;
}
public Map<Question, String> getAnswers() {
return answers;
}
}