<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      oop個人總結第二集(

      前言:

      在第一集總結過后,我對于后續迭代的題目進行了重新設計,在之前設計失敗的第二三次題目中,總結經驗,向同學老師請教,也很好,有效的在答題判題程序設計題目的最后一次迭代設計成功,通過了所有測試。
      后續發布了新的題目類型:家居強電電路模擬程序。
      *我總結之前的經驗,吸取先前的教訓,通過家居強電電路模擬程序-1,部分通過家居強電電路模擬程序-2
      這次題目集較以往不同的是,設計思路較之前會更加清晰,但會涉及更多有關物理電路相關的算法,但歸根結底,前兩次迭代比較簡單,而且有了之前題目集的經驗,也會更加得心應手,但是由于一些設計失誤等原因,小部分測試點并未通過,這還需要我優化后續的設計思路。

      設計與分析:

      • 對于這次總結的第一題:答題判題程序-4
      點擊查看代碼
      import java.util.*;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.TreeMap;
      
      abstract class Question {
          private int num;
          private String content;
          private String standardAnswer;
          private boolean isValid = true;//是否刪除
          private boolean isExist = true;
      
          public Question(int num, String content, String standardAnswer) {
              this.num = num;
              this.content = content;
              this.standardAnswer = standardAnswer;
          }
      
          public String getContent() {
              return content;
          }
      
          public String getStandardAnswer() {
              return standardAnswer;
          }
      
          public boolean isValid() {
              return isValid;
          }
      
      
          public abstract int matchingStandardAnswers(String answer);
      
          public void disabled() {
              this.isValid = false;
          }
      
          public void nonExist() {
              this.isExist = false;
          }
      
          public boolean isExist() {
              return isExist;
          }
      }
      
      //Z
      class Choices_Question extends Question {
          private int num;
          private String content;
          private String standardAnswer;
          private boolean isValid = true;
      
          public Choices_Question(int num, String content, String standardAnswer) {
              super(num, content, standardAnswer);
              this.num = num;
              this.content = content;
              this.standardAnswer = standardAnswer;
          }
      
          @Override
          public int matchingStandardAnswers(String answer) {
              if(answer.equals(standardAnswer)){
                  return 1;
              }
      
              String[] standardAnswers = this.standardAnswer.split(" ");
              String[] userAnswers = answer.split(" ");
      
              List<String> matchedAnswers = new ArrayList<>();
              for (String standard_answer : standardAnswers) {
                  for (String Answer : userAnswers) {
                      if (standard_answer.equals(Answer)) {
                          matchedAnswers.add(Answer);
                          break;
                      }
                  }
              }
      
              boolean find = false;
              for(String Answer : userAnswers){
                  find = false;
                  for(String standard_answer : standardAnswers){
                      if(Answer.equals(standard_answer)){
                          find = true;
                          break;
                      }
                  }
                  if(!find){
                      return 0;
                  }
              }
      
              if (matchedAnswers.size() == standardAnswers.length) {
                  return 1;
              } else if (matchedAnswers.size() > 0) {
                  return 2;
              } else {
                  return 0;
              }
          }
      
      }
      
      //N
      class Filling_N extends Question {
          private int num;
          private String content;
          private String standardAnswer;
          private boolean isValid = true;
      
          public Filling_N(int num, String content, String standardAnswer) {
              super(num, content, standardAnswer);
              this.num = num;
              this.content = content;
              this.standardAnswer = standardAnswer;
          }
      
          @Override
          public int matchingStandardAnswers(String answer) {//判斷是否符合標準答案
              if (standardAnswer.equals(answer)) {
                  return 1;
              } else {
                  return 0;
              }
          }
      }
      
      //K
      class Filling_K extends Question {
          private int num;
          private String content;
          private String standardAnswer;
          private boolean isValid = true;
      
          public Filling_K(int num, String content, String standardAnswer) {
              super(num, content, standardAnswer);
              this.num = num;
              this.content = content;
              this.standardAnswer = standardAnswer;
          }
      
          @Override
          public int matchingStandardAnswers(String answer) {//判斷是否符合標準答案
              answer = answer.trim();
              if (this.standardAnswer.equals(answer)) {
                  return 1;
              } else if (this.standardAnswer.contains(answer)) {
                  return 2;
              } else {
                  return 0;
              }
          }
      }
      
      class Question_in_Paper {
          private Integer order_num; //在試卷中的順序號
          Question question;
          private int question_score = 0;
          private boolean isValid = true;
      
          public Question_in_Paper(Integer order_num, Question question, int question_score) {
              this.order_num = order_num;
              this.question = question;
              this.question_score = question_score;
          }
      
          public String getContent() {
              return this.question.getContent();
          }
      
          public String getStandarAnswer() {
              return this.question.getStandardAnswer();
          }
      
          public int getQuestion_score() {
              return question_score;
          }
      
          public int judge_markAnswer(String answer) {//判斷題目得分
              if (this.question.matchingStandardAnswers(answer) != 0) {
                  return this.question_score;
              } else {
                  return 0;
              }
          }
      
      }
      
      class TestPaper {
          private int paper_num;//試卷號
          private int questionTotal_num = 0;//題目數量
          private int Total_score = 0;
          private boolean isValid = true;
          HashMap<Integer, Question_in_Paper> questions = new HashMap<>();
      
          public TestPaper(int paper_num, int questionTotal_num) {
              this.paper_num = paper_num;
              this.questionTotal_num = questionTotal_num;
          }
      
          public void addQuestionToPaper(Integer order_num, Question_in_Paper question) {//增加題目
              this.questions.put(order_num, question);
              this.questionTotal_num++;
          }
      
          public int getQuestionTotal_num() {
              return questionTotal_num;
          }
      }
      
      class Answer {
          Question_in_Paper question;
          private String answer;
          private int mark;
          private int score = 0;//實際得分
      
          public Answer() {
          }
      
          public Answer(String answer) {
              this.answer = answer;
          }
      
          public void setAnswer(String answer) {
              this.answer = answer;
          }
      
          public void setScore(int score) {
              this.score = score;
          }
      
          public Question_in_Paper getQuestion() {
              return question;
          }
      
          public String getAnswer() {
              return answer;
          }
      
          public void disable() {
      
          }
      
          public int getScore() {
              return score;
          }
      
          public boolean isTRUE() {
              return true;
          }
      }
      
      class Answer_Paper {
          private TestPaper paper;
          private int answer_paper_num;
          private boolean isValid = true;
          private int answer_questions_num = 0;
      
          HashMap<Integer, Answer> answers = new HashMap<>();
      
          public Answer_Paper(int answer_paper_num) {
              this.answer_paper_num = answer_paper_num;
          }
      
          public void disable() {
              this.isValid = false;
          }
      
          public int getAnswer_paper_num() {
              return answer_paper_num;
          }
      
          public void saveAnswer(int answer_order_num, Answer answer) {
              this.answers.put(answer_order_num, answer);
              this.answer_questions_num++;
          }
      
          public void setPaper(TestPaper testPaper) {
              this.paper = testPaper;
          }
      
          public void OutputQ_As() {
              for (int i = 1; i <= this.paper.getQuestionTotal_num(); i++) {
      //            if (paper.questions.get(i) != null) {
                  if(answers.get(i) != null){
                      if (paper.questions.get(i).question.isValid() && paper.questions.get(i).question.isExist()) {
                          System.out.print(paper.questions.get(i).question.getContent() + "~");
                          System.out.print(answers.get(i).getAnswer() + "~");
                          if (paper.questions.get(i).question.matchingStandardAnswers(answers.get(i).getAnswer()) == 1) {
                              System.out.println("true");
                          } else if (paper.questions.get(i).question.matchingStandardAnswers(answers.get(i).getAnswer()) == 2) {
                              System.out.println("partially correct");
                          } else {
                              System.out.println("false");
                          }
                          if (paper.questions.get(i).question.matchingStandardAnswers(answers.get(i).getAnswer()) != 0) {
                              if (paper.questions.get(i).question.matchingStandardAnswers(answers.get(i).getAnswer()) == 1) {
                                  answers.get(i).setScore(paper.questions.get(i).getQuestion_score());
                              } else {
                                  answers.get(i).setScore(paper.questions.get(i).getQuestion_score() / 2);
                              }
                          }
                      } else if (!paper.questions.get(i).question.isValid()) {
                          System.out.println("the question" + " " + i + " invalid~0");
                      } else if (!paper.questions.get(i).question.isExist()) {
                          System.out.println("non-existent question~0");
                      }
                  } else {
                      System.out.println("answer is null");
                  }
              }
          }
      
          public void OutputScore() {
              int totalScore = 0;
              for (int i = 1; i <= this.paper.getQuestionTotal_num(); i++) {
                  int score;
                  if(answers.get(i)!=null){
                      score = answers.get(i).getScore();
                  } else {
                      score = 0;
                  }
                  System.out.print(" ");
                  System.out.print(score);
                  totalScore += score;
              }
              System.out.println("~" + totalScore);
          }
      
          public void Save_TestPaper() {
      
          }
      }
      
      class Student {
          private String id;
          private String name;
          private int answerSheet_num = 0;
          HashMap<Integer, Answer_Paper> answer_papers = new HashMap<>();
          TreeMap<Integer, Answer_Paper> sortedMap = new TreeMap<>();
          HashMap<Integer, TestPaper> testPapers;
      
          public Student(String id, String name) {
              this.id = id;
              this.name = name;
          }
      
          public void setAnswerSheet_num() {
              this.answerSheet_num++;
          }
      
          public void ergodicAnswerSheet(HashMap<Integer, TestPaper> testPapers) {
              sortedMap.putAll(answer_papers);
              this.testPapers = testPapers;
              for (Map.Entry<Integer, Answer_Paper> entry : sortedMap.entrySet()) {
                  int paper_num = entry.getValue().getAnswer_paper_num();
                  if (testPapers.containsKey(paper_num)) {
                      entry.getValue().OutputQ_As();
                      System.out.print(id + " " + name + ":");
                      entry.getValue().OutputScore();
                      //score
                  } else {
                      System.out.println("The test paper number does not exist");
                      continue;
                  }
              }
          }
      }
      
      class Input {
          ArrayList<String> lines = new ArrayList<>();
      
          HashMap<Integer, Question> questions = new HashMap<>();
          HashMap<Integer, TestPaper> testPapers = new HashMap<>();
          HashMap<String, Student> students = new HashMap<>();
          Output output;
      
          public Input() {
          }
      
          public void addLines(String line) {
              lines.add(line);
              this.solve_question_InLine(line);//題目優先
          }
      
          public void solve_question_InLine(String line) {
              Matcher matcherN = Pattern.compile("#N:").matcher(line);
              Matcher matcherK = Pattern.compile("#K:").matcher(line);
              Matcher matcherZ = Pattern.compile("#Z:").matcher(line);
              if (matcherN.find()) saveQuestionN(line);
              else if (matcherK.find()) saveQuestionK(line);
              else if (matcherZ.find()) saveQuestionZ(line);
          }
      
          public void solveLine_Delete() {
              for (String line : lines) {
                  Matcher matcherD = Pattern.compile("#D:").matcher(line);
                  if (matcherD.find()) saveDelete(line);
              }
          }
      
          public void solveLine_Student() {
              for (String line : lines) {
                  Matcher matcherX = Pattern.compile("#X:").matcher(line);
      
                  if (matcherX.find()) saveStudent(line);
              }
          }
      
          public void solveLine_TestPaper() {
              for (String line : lines) {
                  Matcher matcherT = Pattern.compile("#T:").matcher(line);
      
                  if (matcherT.find()) saveTestPaper(line);
      
              }
          }
      
          public void solveLine_AnswerSheet() {
              for (String line : lines) {
                  Matcher matcherS = Pattern.compile("#S:").matcher(line);
                  if (matcherS.find()) saveAnswerSheet(line);
              }
          }
      
          public void saveQuestionN(String line) {
              Matcher matcher_questionN = Pattern.compile("#N:(.*)#Q:(.*)#A:(.*)").matcher(line);
              Matcher matcher_questionN_ = Pattern.compile("^#N: *(\\d+) *#Q:(.*)#A:(.*)").matcher(line);
      
              if (!matcher_questionN_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
      
              if (matcher_questionN.find()) {
                  int number = Integer.parseInt(matcher_questionN.group(1).trim());
                  String content = matcher_questionN.group(2).trim();
                  String standardAnswer = matcher_questionN.group(3).trim();
                  Filling_N fill = new Filling_N(number, content, standardAnswer);
                  questions.put(number, fill);
                  return;
              }
          }
      
          public void saveQuestionK(String line) {
              Matcher matcher_questionK = Pattern.compile("#K:(\\d+)(\\s)*#Q:(.+)#A:(.+)").matcher(line);
              Matcher matcher_questionK_ = Pattern.compile("^#K:(\\d+)(\\s)*#Q:(.+)#A:(.+)").matcher(line);
              if (!matcher_questionK_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
      
              if (matcher_questionK.find()) {
                  int number = Integer.parseInt(matcher_questionK.group(1).trim());
                  String content = matcher_questionK.group(3).trim();
                  String standardAnswer = matcher_questionK.group(4).trim();
                  Filling_K fills = new Filling_K(number, content, standardAnswer);
                  questions.put(number, fills);
                  return;
              }
          }
      
          public void saveQuestionZ(String line) {
              Matcher matcher_questionZ = Pattern.compile("#Z:(\\d+)(\\s)*#Q:(.+)#A:(.+)").matcher(line);
              Matcher matcher_questionZ_ = Pattern.compile("^#Z:(\\d+)(\\s)*#Q:(.+)#A:(.+)").matcher(line);
              if (!matcher_questionZ_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
      
              if (matcher_questionZ.find()) {
                  int number = Integer.parseInt(matcher_questionZ.group(1).trim());
                  String content = matcher_questionZ.group(3).trim();
                  String standardAnswer = matcher_questionZ.group(4).trim();
                  Choices_Question choices_question = new Choices_Question(number, content, standardAnswer);
                  questions.put(number, choices_question);
                  return;
              }
          }
      
          public void saveTestPaper(String line) {
              Matcher matcherTestPaper = Pattern.compile("#T:(\\s+)?(\\d+)(\\s+)?").matcher(line);
              Matcher matcherTestPaper_ = Pattern.compile("^#T:(\\s+)?(\\d+)(\\s+)?").matcher(line);
              Matcher matcher_score = Pattern.compile("(\\d+)(-)(\\d+)").matcher(line);
      
              if (!matcherTestPaper_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
      
              int paper_num = 0;
              if (matcherTestPaper.find()) {
                  paper_num = Integer.parseInt(matcherTestPaper.group(2).trim());
              }
              int sum_score = 0;
              int question_count = 0;
              TestPaper testPaper = new TestPaper(paper_num, 0);
              while (matcher_score.find()) {
                  int num = Integer.parseInt(matcher_score.group(1).trim());
                  int score = Integer.parseInt(matcher_score.group(3).trim());
                  question_count++;
                  if (questions.get(num) != null) {
                      Question new_question = questions.get(num);
                      Question_in_Paper question_in_paper = new Question_in_Paper(question_count, new_question, score);
                      testPaper.addQuestionToPaper(question_count, question_in_paper);
                  } else {
                      Question UnExisted_question = new Filling_N(0, "0", "0");
                      UnExisted_question.nonExist();
                      Question_in_Paper question_in_paper = new Question_in_Paper(question_count, UnExisted_question, score);
                      testPaper.addQuestionToPaper(question_count, question_in_paper);
                  }
                  sum_score += score;
              }
              testPapers.put(paper_num, testPaper);
              if (sum_score != 100) {
                  System.out.print("alert: full score of test paper");
                  System.out.print(paper_num);
                  System.out.println(" is not 100 points");
              }
          }
      
          public void saveAnswerSheet(String line) {
              Matcher matcherS = Pattern.compile("#S: *(\\d+) +(\\d+)").matcher(line);
              Matcher matcherS_ = Pattern.compile("^#S: *\\d+ +\\d+ *(.*?)$").matcher(line);
              Matcher matcher_your_answer = Pattern.compile("#A:(\\s+)*(\\d+)(\\s+)*-(.*?)(\\s+)*(?=#A:|\\n|$)").matcher(line);//your answer
      
              if (!matcherS_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
      
              if (matcherS.find()) {
                  int paper_num = Integer.parseInt(matcherS.group(1).trim());
                  String id = matcherS.group(2);
      
                  Answer_Paper answer_paper = new Answer_Paper(paper_num);
                  students.get(id).answer_papers.put(paper_num, answer_paper);
                  students.get(id).answer_papers.get(paper_num).setPaper(testPapers.get(paper_num));
      
                  while (matcher_your_answer.find()) {
                      int answer_num = Integer.parseInt(matcher_your_answer.group(2).trim());
                      String answer = matcher_your_answer.group(4);
      
                      Answer new_answer = new Answer(answer);
                      students.get(id).answer_papers.get(paper_num).saveAnswer(answer_num, new_answer);
                  }
              }
          }
      
          public void saveStudent(String line) {
              Matcher matcher_student = Pattern.compile("(\\d+)(\\s)([a-zA-Z]+)").matcher(line);
              Matcher matcher_student_ = Pattern.compile("^#X: *( *\\d+ *\\w+ *-*)*$").matcher(line);
      
              if (!matcher_student_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
      
              while (matcher_student.find()) {
                  String id = matcher_student.group(1).trim();
                  String name = matcher_student.group(3).trim();
                  Student student = new Student(id, name);
                  students.put(id, student);
              }
          }
      
          public void saveDelete(String line) {
              Matcher matcher_remove_question_ = Pattern.compile("^#D:(\\s*)N(\\s*)-(\\s*)(.+)$").matcher(line);
              Matcher matcher_remove_question = Pattern.compile("N-(\\d+)").matcher(line);
      
              if (!matcher_remove_question_.find()) {
                  System.out.println("wrong format:" + line);
                  return;
              }
              while (matcher_remove_question.find()) {
                  int num = Integer.parseInt(matcher_remove_question.group(1).trim());
                  questions.get(num).disabled();
              }
          }
      
          public void output() {
              this.output = new Output(this.questions, this.testPapers, this.students);
          }
      }
      
      class Output {
          HashMap<Integer, Question> questions;
          HashMap<Integer, TestPaper> testPapers;
          HashMap<String, Student> students;
          TreeMap<String, Student> sortedMap = new TreeMap<>();
      
          public Output(HashMap<Integer, Question> questions, HashMap<Integer, TestPaper> testPapers, HashMap<String, Student> students) {
              this.questions = new HashMap<>(questions);
              this.testPapers = new HashMap<>(testPapers);
              this.students = new HashMap<>(students);
              sortedMap.putAll(students);
          }
      
          public void output() {
              for (Map.Entry<String, Student> entry : sortedMap.entrySet()) {//斷點
                  Student student = entry.getValue();
                  student.ergodicAnswerSheet(this.testPapers);
              }
          }
      }
      
      public class Main {
          public static void main(String[] args) {
              Input input = new Input();
              Scanner sc = new Scanner(System.in);
              while (true) {
                  String line = sc.nextLine();
                  if (line.trim().equals("end")) {
                      break;
                  }
                  input.addLines(line);
              }
              input.solveLine_Delete();
              input.solveLine_TestPaper();
              input.solveLine_Student();
              input.solveLine_AnswerSheet();
              input.output();
              input.output.output();
          }
      }
      
      
      我優化之前的設計思路,首先重新對題目做需求分析,創建 題目類,學生類,試卷類,答題類,答卷類,具體題目類 **類圖如下:**

      針對之前的錯誤案列,新增一些類,和處理方法,可以有效解決題目會出現的各種情況。
      這次迭代相對之前新增了題目的題型,考察了繼承和抽象類,我對Question類抽象,其中有抽象方法:判題方法,新增他的子類填空題,單選題,多選題等并具體,實現其不同的判題方法
      代碼邏輯:先從Main開始循環輸入題目,試卷,答題,學生等信息,在Input中對其使用正則表達式進行分析輸入信息,用不同的類儲存信息
      最后從Output開始遍歷試卷,題目,答卷,輸出判題的結果以及對應的分數。
      代碼分析:
      代碼中不乏錯誤處理:應對各種,題目,試卷,答案出錯或缺少都會輸出對應提示信息,避免程序崩潰,無法運行
      代碼設計了很多類,都實現了其單一職責,有效提高了代碼的可讀性,和理解性。
      要說缺點:那就是在其中題目亂序輸出的情況,我使用了較為復雜不通用的方法去讀取題目信息,而并沒有對其排序后再讀取其中信息,如圖

      相對于其他同學,針對性的寫一個排序方法,我這個通用性差,復用性差,如果后續再進行迭代,也許就死翹翹了T-T

      • 對于家居強電電路模擬程序-1
      點擊查看代碼
      import java.util.*;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
      
      // 設備類
      abstract class Device {
          int id;
          double inVoltage = 0;
          double outVoltage = 0;
      
          public Device(int id) {
              this.id = id;
          }
      
          public Device() {
          }
      
          public int getId() {
              return id;
          }
      
          public double getInVoltage() {
              return inVoltage;
          }
      }
      
      abstract class ControlledDevice extends Device {
          int id;
          double inVoltage = 0;
          double outVoltage = 0;
      
          public ControlledDevice(int id) {
              super(id);
          }
      
          public ControlledDevice() {
          }
      }//被控設備
      
      abstract class ControllingDevice extends Device {
          String id;
          double inVoltage = 0;
          double outVoltage = 0;
      
          public ControllingDevice(int id) {
              super(id);
          }
      
          public ControllingDevice() {
          }
      
          public abstract double getOutVoltage();
      }//控制設備
      
      // 開關類
      class Switch extends ControllingDevice {
          private int state = 0;
      
          public Switch(int id) {
              super(id);
          }
      
          public int getState() {
              return state;
          }
      
          public void setState() {
              if (state == 1) {
                  state = 0;
              } else {
                  state = 1;
              }
          }
      
          @Override
          public double getOutVoltage() {
              if(state == 1) {
                  return outVoltage;
              } else {
                  return 0.0;
              }
          }
      }
      
      // 分檔調速器類
      class GearSpeedController extends ControllingDevice {
          private int gear = 0;
      
          public GearSpeedController(int id) {
              super(id);
          }
      
      
      
          // 增加檔位和減少檔位的方法
          public void upGear() {
              if (gear < 3) {
                  gear++;
              }
          }
      
          public void downGear() {
              if (gear > 0) {
                  gear--;
              }
          }
      
          public int getGear() {
              return gear;
          }
      
          public void calculateVoltage(){
              switch (gear){
                  case 0: outVoltage = 0;
                  break;
                  case 1: outVoltage = 0.3*220;
                  break;
                  case 2: outVoltage = 0.6*220;
                  break;
                  case 3: outVoltage = 0.9*220;
              }
          }
      
          @Override
          public double getOutVoltage() {
              calculateVoltage();
              return outVoltage;
          }
      
      }
      
      // 連續調速器類
      class ContinuousSpeedController extends ControllingDevice {
          private double gear = 0;
      
          public ContinuousSpeedController(int id) {
              super(id);
          }
      
          public void setGear(double gear) {
              this.gear = gear;
          }
      
          public double getGear() {
              return gear;
          }
      
          public void calculateVoltage(){
              this.outVoltage = gear*220.0;
          }
      
          public double getOutVoltage() {
              calculateVoltage();
              return outVoltage;
          }
      }
      
      // 燈類
      abstract class Light extends ControlledDevice {
          public Light(int id) {
              super(id);
          }
      
          // 具體實現亮度計算的方法
          abstract double calculateBrightness(double voltage);
      }
      
      // 白熾燈類
      class IncandescentLamp extends Light {
      
          public IncandescentLamp(int id) {
              super(id);
          }
      
          @Override
          public double calculateBrightness(double voltage) {
              if(voltage<=9){
                  return 0.0;
              } else if(voltage>=220){
                  return 200.0;
              } else{
                  return  (5.0/7.0)*voltage+(300.0/7.0);
              }
          }
      }
      
      // 日光燈類
      class DaylightLamp extends Light {
      
          public DaylightLamp(int id) {
              super(id);
          }
      
          @Override
          public double calculateBrightness(double voltage) {
              if(voltage==0){
                  return 0.0;
              } else{
                  return 180.0;
              }
          }
      }
      
      // 風扇類
      abstract class Fan extends ControlledDevice {
          public Fan(int id) {
              super(id);
          }
      
          // 具體實現轉速計算的方法
          abstract double calculateSpeed(double voltage);
      }
      
      // 吊扇類
      class CeilingFan extends Fan {
          public CeilingFan(int id) {
              super(id);
          }
      
          @Override
          public double calculateSpeed(double voltage) {
              if(voltage<=80){
                  return 0.0;
              } else if(voltage>=150){
                  return 360.0;
              } else{
                  return ((voltage-80.0) / 70.0 *280.0)+80.0;
              }
          }
      }
      
      //串聯電路類
      class Series extends Device {
          ArrayList <Device> devices = new ArrayList <>();
      
          HashMap<Integer,Switch> switches = new HashMap<>();
          HashMap<Integer,GearSpeedController> gearSpeedControllerHashMap = new HashMap<>();
          HashMap<Integer,ContinuousSpeedController> continuousSpeedControllerHashMap = new HashMap<>();
          HashMap<Integer,IncandescentLamp> incandescentLampHashMap = new HashMap<>();
          HashMap<Integer,DaylightLamp> daylightLamps = new HashMap<>();
          HashMap<Integer,CeilingFan> ceilingFans = new HashMap<>();
      
      
          private double current;
          private double voltage;
          private double resistance;
      
          public Series() {}
      
          public Switch saveK(int id) {
              Switch newSwitch = new Switch(id);
              switches.put(id, newSwitch);
              return newSwitch;
          }
      
          public GearSpeedController saveF(int id) {
              GearSpeedController newGearSpeedController = new GearSpeedController(id);
              gearSpeedControllerHashMap.put(id, newGearSpeedController);
              return newGearSpeedController;
          }
      
          public ContinuousSpeedController saveL(int id) {
              ContinuousSpeedController newContinuousSpeedController = new ContinuousSpeedController(id);
              continuousSpeedControllerHashMap.put(id, newContinuousSpeedController);
              return newContinuousSpeedController;
          }
      
          public IncandescentLamp saveB(int id) {
              IncandescentLamp newIncandescentLamp = new IncandescentLamp(id);
              incandescentLampHashMap.put(id, newIncandescentLamp);
              return newIncandescentLamp;
          }
      
          public DaylightLamp saveR(int id) {
              DaylightLamp newDaylightLamp = new DaylightLamp(id);
              daylightLamps.put(id, newDaylightLamp);
              return newDaylightLamp;
          }
      
          public CeilingFan saveD(int id) {
              CeilingFan newCeilingFan = new CeilingFan(id);
              ceilingFans.put(id, newCeilingFan);
              return newCeilingFan;
          }
      
      }
      
      class Input {
          ArrayList<String> lines = new ArrayList<>();
          Series series = new Series();//串聯電路
          Output output;
      
          public Input() {
          }
      
          public void saveLines(String line) {
              this.lines.add(line);
          }
      
          public void saveDevices() {
              for (String line : this.lines) {
                  Matcher matcherDevices = Pattern.compile("\\[([A-Za-z]+)(\\d+)-(\\d+) ([A-Za-z]+)(\\d+)-(\\d+)]").matcher(line);
                  Matcher matcherE = Pattern.compile("\\[VCC ([A-Za-z]+)(\\d+)-(\\d+)]").matcher(line);
                  Matcher matcherG = Pattern.compile("\\[([A-Za-z]+)(\\d+)-(\\d+) GND]").matcher(line);
                  Matcher matcherSwitch = Pattern.compile("#K(\\d+)").matcher(line);
                  Matcher matcherGear = Pattern.compile("#F(\\d+)(.+)").matcher(line);
                  Matcher matcherContinuousSpeed = Pattern.compile("#L(\\d+):(.+)").matcher(line);
      
                  if (matcherE.find()) {
                      String device = matcherE.group(1);
                      int id = Integer.parseInt(matcherE.group(2));
                      int pin = Integer.parseInt(matcherE.group(3));
                      series.devices.add(judgeDevices(device, id));
                  }
      
                  if (matcherDevices.find()) {
                      String device1 = matcherDevices.group(1);
                      int id1 = Integer.parseInt(matcherDevices.group(2));
                      int pin1 = Integer.parseInt(matcherDevices.group(3));
                      String device2 = matcherDevices.group(4);
                      int id2 = Integer.parseInt(matcherDevices.group(5));
                      int pin2 = Integer.parseInt(matcherDevices.group(6));
                      series.devices.add(judgeDevices(device1, id1));
                      series.devices.add(judgeDevices(device2, id2));
                  }
      
                  if(matcherSwitch.find()){
                      series.switches.get(Integer.parseInt(matcherSwitch.group(1))).setState();
                  }
      
                  if(matcherGear.find()){
                      int id = Integer.parseInt(matcherGear.group(1));
                      if(matcherGear.group(2).equals("+")){
                          series.gearSpeedControllerHashMap.get(id).upGear();
                      }
                      if(matcherGear.group(2).equals("-")){
                          series.gearSpeedControllerHashMap.get(id).downGear();
                      }
                  }
      
                  if(matcherContinuousSpeed.find()){
                      int id = Integer.parseInt(matcherContinuousSpeed.group(1));
                      double gear = Double.parseDouble(matcherContinuousSpeed.group(2));
                      series.continuousSpeedControllerHashMap.get(id).setGear(gear);
                  }
              }
          }
      
          public Device judgeDevices(String device, int id) {
              switch (device) {
                  case "K":
                      return series.saveK(id);
                  case "F":
                      return series.saveF(id);
                  case "L":
                      return series.saveL(id);
                  case "B":
                      return series.saveB(id);
                  case "R":
                      return series.saveR(id);
                  default:
                      return series.saveD(id);
              }
          }
      
          public void saveList(){
              output = new Output(series.switches,series.ceilingFans,series.daylightLamps,series.incandescentLampHashMap,series.continuousSpeedControllerHashMap,series.gearSpeedControllerHashMap);
          }
      
          public void Output() {
              saveList();
              output.output();
          }
      }
      
      class Output{
          double TotalVoltage = 220.0;
          HashMap<Integer,Switch> switches = new HashMap<>();
          HashMap<Integer,GearSpeedController> gearSpeedControllerHashMap;
          HashMap<Integer,ContinuousSpeedController> continuousSpeedControllerHashMap;
          HashMap<Integer,IncandescentLamp> incandescentLampHashMap;
          HashMap<Integer,DaylightLamp> daylightLamps ;
          HashMap<Integer,CeilingFan> ceilingFans ;
      
          TreeMap<Integer,Switch> sortedSwitches = new TreeMap<>();
          TreeMap<Integer,GearSpeedController> sortedGearSpeedControllers = new TreeMap<>();
          TreeMap<Integer,ContinuousSpeedController> sortedContinuousSpeedControllers = new TreeMap<>();
          TreeMap<Integer,IncandescentLamp> sortedIncandescentLamps = new TreeMap<>();
          TreeMap<Integer,DaylightLamp> sortedDaylightLamps = new TreeMap<>();
          TreeMap<Integer,CeilingFan> sortedCeilingFans = new TreeMap<>();
      
          public Output(HashMap<Integer, Switch> switches, HashMap<Integer, CeilingFan> ceilingFans, HashMap<Integer, DaylightLamp> daylightLamps, HashMap<Integer, IncandescentLamp> incandescentLampHashMap, HashMap<Integer, ContinuousSpeedController> continuousSpeedControllerHashMap, HashMap<Integer, GearSpeedController> gearSpeedControllerHashMap) {
              this.switches = new HashMap<>(switches);
              this.gearSpeedControllerHashMap = new HashMap<>(gearSpeedControllerHashMap);
              this.continuousSpeedControllerHashMap = new HashMap<>(continuousSpeedControllerHashMap);
              this.incandescentLampHashMap = new HashMap<>(incandescentLampHashMap);
              this.daylightLamps = new HashMap<>(daylightLamps);
              this.ceilingFans = new HashMap<>(ceilingFans);
      
      
              sortedSwitches.putAll(switches);
              sortedGearSpeedControllers.putAll(gearSpeedControllerHashMap);
              sortedContinuousSpeedControllers.putAll(continuousSpeedControllerHashMap);
              sortedIncandescentLamps.putAll(incandescentLampHashMap);
              sortedDaylightLamps.putAll(daylightLamps);
              sortedCeilingFans.putAll(ceilingFans);
          }
      
          public void output() {
              outputK();
              outputF();
              outputL();
              outputB();
              outputR();
              outputD();
          }
      
          private void outputK(){
              for (Map.Entry<Integer,Switch> entry : sortedSwitches.entrySet()) {//斷點
                  if(entry == null){
                      break;
                  }
                  Switch aSwitch = entry.getValue();
                  if(aSwitch.getState()==0){
                      System.out.println("@K" + aSwitch.getId() + ":turned on");
                      this.TotalVoltage = 0;
                  } else {
                      System.out.println("@K" + aSwitch.getId() + ":closed");
                  }
              }
          }
      
          private void outputF(){
              for (Map.Entry<Integer,GearSpeedController> entry : sortedGearSpeedControllers.entrySet()) {//斷點
                  if(entry == null){
                      break;
                  }
                  GearSpeedController gearSpeedController = entry.getValue();
                  System.out.print("@F" + gearSpeedController.getId() + ":");
                  System.out.printf("%d\n", gearSpeedController.getGear());
                  gearSpeedController.calculateVoltage();
                  this.TotalVoltage = gearSpeedController.getOutVoltage();
              }
          }
      
          private void outputL(){
              for (Map.Entry<Integer,ContinuousSpeedController> entry : sortedContinuousSpeedControllers.entrySet()) {
                  if(entry == null){
                      break;
                  }
                  ContinuousSpeedController continuousSpeedController = entry.getValue();
                  System.out.print("@L" + continuousSpeedController.getId() + ":");
                  System.out.printf("%.2f\n",continuousSpeedController.getGear());
                  continuousSpeedController.calculateVoltage();
                  this.TotalVoltage = continuousSpeedController.getOutVoltage();
              }
          }
      
          private void outputB(){
              for(Map.Entry<Integer,IncandescentLamp> entry : sortedIncandescentLamps.entrySet()){
                  if(entry == null){
                      break;
                  }
                  IncandescentLamp incandescentLamp = entry.getValue();
                  System.out.print("@B" + incandescentLamp.getId() + ":" );
                  int integerPart = (int) incandescentLamp.calculateBrightness(TotalVoltage);
                  System.out.println(integerPart);
              }
          }
      
          private void outputR(){
              for(Map.Entry<Integer,DaylightLamp> entry : sortedDaylightLamps.entrySet()){
                  if(entry == null){break;}
                  DaylightLamp daylightLamp = entry.getValue();
                  System.out.print("@R" + daylightLamp.getId() + ":" );
                  System.out.printf("%.0f\n",daylightLamp.calculateBrightness(TotalVoltage));
              }
          }
      
          private void outputD(){
              for(Map.Entry<Integer,CeilingFan> entry : sortedCeilingFans.entrySet()){
                  if(entry == null){break;}
                  CeilingFan ceilingFan = entry.getValue();
                  System.out.print("@D" + ceilingFan.getId() + ":");
                  System.out.printf("%.0f\n",ceilingFan.calculateSpeed(TotalVoltage));
              }
          }
      }
      
      //并聯電路類
      class ParallelCircuit extends Device {
          private String id;
          private double current;
          private double voltage;
          private double resistance;
      }
      
      
      // 主程序
      public class Main {
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              Input input = new Input();
      
              while (true) {
                  String line = sc.nextLine();
                  if (line.equals("end")) {
                      break;
                  }
                  input.saveLines(line);
              }
              input.saveDevices();
      
              input.Output();
          }
      }
      
      
      *由于是系列題目的第一題,只含有串聯電路,所以較為簡單, 類圖如下:


      對于這次的電路只需要考慮,控制設備,受控設備,電路類,三種設備類的子類即可。

      • 代碼分析:
        定義了如下幾個類
        Input:負責讀取和處理輸入數據,包括保存輸入行、解析設備信息、保存設備到串聯電路中,以及輸出電路狀態。
        Output:類將設備按照類型排序,并分別輸出它們的狀態,如開關狀態、調速器檔位、燈的亮度和風扇的轉速
        Device類是所有設備的基類,包含設備的基本屬性,如ID、輸入電壓和輸出電壓。
        ControledDevice類,受控設備,包括幾個用電器,如下:
        Fan類:繼承自ControledDevice類,有抽象方法,根據電壓計算轉速
        CeilingFan類:繼承自Fan類,實現類計算轉速的方法。
        Light類:繼承自ControledDevice類,有抽象方法,根據電壓計算燈泡亮度。
        IncandescentLamp類和DaylightLamp類:繼承自Light,分別實現計算亮度的方法。

      ControlingDevice類:控制設備類,有調速器用于變壓和開關控制電路
      GearSpeedController類:分檔調速器。ContinuousSpeedController類:連續調速器。
      他們都有不同的方法可以調節電路的電壓。
      Switch類:開關,用于電路的聯通。
      Series類:串聯電路類,包含各種設備。

      • 代碼邏輯:
        首先由Input處理從Main輸入的信息,并分析電路信息,儲存電路信息,因為題目一暫時只有串聯電路情況,所以儲存到Series中,再針對控制設備的控制情況,對電路的情況進行更改,再由output分析Series中每個設備的電壓信息,輸出對應受控設備以及控制設備的信息情況。

      • 不足之處:
        1.Series 類中的 saveK、saveF、saveL 等方法有大量重復代碼,可以考慮使用工廠模式來簡化這些方法。
        2.代碼的結構較為復雜,有多個抽象類和具體實現類。可以考慮簡化類結構,減少不必要的抽象。

      • 總的來說,初步完成題目要求,但仍有許多不足之處需要改進。

      • 對于 家居強電電路模擬程序-2:

      • 定義了如下幾個類
        Input:負責讀取和處理輸入數據,包括保存輸入行、解析設備信息、保存設備到串聯電路中,以及輸出電路狀態。
        Output:類將設備按照類型排序,并分別輸出它們的狀態,如開關狀態、調速器檔位、燈的亮度和風扇的轉速
        Device類是所有設備的基類,包含設備的基本屬性,如ID、輸入電壓和輸出電壓。
        ControledDevice類,受控設備,包括幾個用電器,如下:
        Fan類:繼承自ControledDevice類,有抽象方法,根據電壓計算轉速
        CeilingFan類和PedestalFan類:繼承自Fan類,實現類計算轉速的方法。
        Light類:繼承自ControledDevice類,有抽象方法,根據電壓計算燈泡亮度。
        IncandescentLamp類和DaylightLamp類:繼承自Light,分別實現計算亮度的方法。

      新增并聯電路的情況,并新增幾個用電器,
      對于類進行類新增
      需要新增并聯電路類用于應對并聯電路的情況
      類圖如下:

      • 代碼分析:
        首先我在原先題目的基礎上新增了題目需求的PedestalFan類繼承于Fan類,再新增了電路和并聯電路類用于處理和分析并聯電路的信息,并有方法可以計算并聯電路總電阻,再對被控設備ControledDevice增加電阻屬性。
        根據并聯電路的添加,更改Input中識別電路信息的部分,新增可以匹配并聯電路的正則表達式。

      • 代碼邏輯:
        基本邏輯相較于前一道題目大致不變,在針對于并聯電路做了具體分析,由分析串聯電路,并聯電路,再構造出一條基于串聯電路的總電路,然后計算每一條分電路的總電阻,求和,得出總電路的總電阻,依據控制設備的得到調節之后的電壓,遍歷賦予每一條分電路各自的分壓。最后輸出時,先輸出控制設備的信息再,再按照之前已經賦予的分壓計算得出不同用電器的不同轉速或者亮度。遍歷所有總電路中已有用電器按照同樣的方法輸出即可。

      • 不足之處:
        在串聯電路和并聯電路中,有很多冗雜重復的代碼,可以將他們整合成一個方法,然后調用這個方法就可以避免重復。
        代碼邏輯很復雜,在理解過程中有很大的麻煩,而且不利于代碼的復用,很多一次性垃圾代碼,這也導致了錯誤率大大提高,也讓在排查未通過的測試點時,帶來了很大的麻煩與困難。

      • 未來改進:應該對兩個電路類進行優化,減少重復代碼,增強其可讀性。

      踩坑心得:

      • 對于答題判題程序-4,其中多選題的判題方法需要十分嚴謹,例如需要考慮空白字符等,需要嚴格匹配。
        還有踩坑的就是,之前對于題目的理解不夠充分,不夠深,所以大刀闊斧的對這道題目進行了重寫,浪費了大量的時間和精力,這表明了,做好需求分析的重要性!!!

      • 對于 家居強電電路模擬程序-1
        題目的設計思路十分清晰明了,基本上沒有踩坑點,唯一要注意的就是,在針對四舍五入還是直接舍去小數位時,需要多多注意,要嚴格按照題目要求來輸出相應的結果。

      • 對于 家居強電電路模擬程序-2

      題目復雜度上升,在設計并聯電路類時,將其設計的過于復雜,導致代碼可讀性不高,同時又因為復雜方法過多,循環過多,if判斷過多,讓人頭大,應該尋找更簡便的代碼思路來簡化代碼,以便后續迭代的編寫順利進行。
      對于被控設備類可以統一實現獲取電壓的接口,也可以簡化大量重復代碼。
      對于并聯和串聯電路,明顯同屬于電路,可以設計電路類,用于并聯和串聯的繼承,這樣就可以將重復冗雜的代碼去除,得到更有利于理解的代碼

      總結,相較于之前的幾次題目集,也大部分避免之前會經常犯的錯誤,但仍然不可避免的會在設計思路上有誤差,因為總體設計思路上的問題,也就導致局部的編寫錯誤,從而導致過于復雜。

      改進建議:

      在 家居強電電路模擬程序系列題目中,可以設計電路類,讓串聯電路,和并聯電路繼承于電路,找到兩者的共通性,設計抽象方法和具體方法,發別實現即可,這樣就可以避免在兩者之間出現的相同且重復的代碼,有限減少了代碼量

      總結:

      有了第一次寫題的經驗,后續的題目也更加得心應手,在寫題之前優先設計類圖,思考類與類之間的關系,應該建立怎么樣的關系保證之間的連接,從而實現題目需求。
      在寫完題目之后,也可以獲得很多收獲:
      如廣泛使用的oop的概念,如封裝、繼承和多態。通過定義基類和接口,以及創建具體的子類實現,可以理解如何構建一個具有層次結構的類體系。
      繼承和多態:通過繼承基類 Device ,不同的電路組件類繼承了共同的屬性和方法,并通過重寫方法實現特定的行為,有助于代碼的簡便和可拓展性
      組合:在 Series 和 Parallel類中,通過將不同的電路組件組合在一起,可以構建更復雜的電路結構。
      多種設計模式:Input 類中使用了工廠模式和策略模式(在方法中根據不同組件類型選擇不同的行為),有助于理解設計模式在實際代碼中的應用。
      正則表達式的使用:Input 類中使用了正則表達式來解析輸入字符串,提高匹配字符串的準確性和高效性
      代碼可維護性:代碼中存在一些可維護性問題,如重復代碼、復雜的條件判斷等,這是要求我們在編寫代碼時要注重可維護性和可讀性。
      錯誤處理的重要性:代碼中遍歷時使用了不是null,所以在編程時應該考慮異常情況和錯誤處理,以提高程序的健壯性。
      最后我希望接下來的課程能夠再接再厲,學好oop不是夢!

      posted on 2024-06-08 19:49  oblivioner  閱讀(23)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 日本中文字幕有码在线视频 | 欧美一进一出抽搐大尺度视频| 国产午夜福利不卡在线观看| 精品无码国产一区二区三区av| 99精品国产一区二区三| 国产免费无遮挡吃奶视频| 欧美精品在线观看| 中文字幕无码av不卡一区| 国产一区二区在线观看粉嫩| 国产精品午夜福利清纯露脸| 国产高潮刺激叫喊视频| 久久99国产精品久久99| 一二三四中文字幕日韩乱码| 老司机午夜福利视频| 日韩三级一区二区在线看| 午夜爽爽爽男女免费观看影院| 阿尔山市| 四虎精品国产精品亚洲精| 欧洲亚洲精品免费二区| 久久91精品牛牛| 亚洲国产精品综合久久2007| 国产精品一区二区三区黄| 亚洲欧美牲交| 精品久久8x国产免费观看| 亚洲国产成人无码影片在线播放| 国产香蕉久久精品综合网| 无码熟妇人妻AV在线影片最多| 制服 丝袜 亚洲 中文 综合| 国内精品伊人久久久影视| 日韩午夜福利视频在线观看| 人妻少妇久久久久久97人妻| 久久精品国产亚洲av麻豆长发| 国产亚洲精品自在久久vr| 久久精品国产亚洲av麻豆长发| 无码人妻丰满熟妇啪啪| 午夜成人精品福利网站在线观看| 玖玖在线精品免费视频| 成人无码视频在线观看免费播放| 国产精品久久久午夜夜伦鲁鲁| 国产美女在线精品免费观看| 人妻中文字幕亚洲精品|