一、前言
1.通过这几次PTA的大作业,加深了我对java编程的了解和熟练度。
这一段时间的学习:这一段时间主要训练了有关java接口和多态相关方面的知识,这部分内容是Java的重要组成部分,他们共同提高了代码的开发效率,使得代码设计灵活,维护简单,结构清晰
关于类的特性:在设计类的过程中,通过对各种类之间关系的设计,发现类具有很多特性,这些特性使得类成为面向对象编程中的核心概念,能够更好地组织和管理代码,提高代码的可维护性和可扩展性。
2.这几次大作业每一次迭代,都在原先的基础上增加了一些需要考虑的东西,这需要我们对题目进行进一轮的分析和考虑,在加大题目难度的同时,也使我们更好的锻炼类之间的设计能力
二、设计与分析
1.7-1 答题判题程序-4
(1)该题目是答题程序的最后一次迭代,相较前几题增加了多选题和填空题,增加了需要考虑的情况,和对半对结果的分析
(2)类的设计:
具体如类图所示
具体设计如下:
public int getAnswerPaperNum() {return AnswerPaperNum;}public void setAnswerPaperNum(int answerPaperNum) {AnswerPaperNum = answerPaperNum;}public String getId() {return Id;}public void setId(String id) {Id = id;}public ArrayList<Question> getQuestionArrayList() {return questionArrayList;}public void setQuestionArrayList(ArrayList<Question> questionArrayList) {this.questionArrayList = questionArrayList;}public ArrayList<Integer> getAnswerNum() {return AnswerNum;}public void setAnswerNum(ArrayList<Integer> answerNum) {AnswerNum = answerNum;}public ArrayList<String> getQuestionAnswer() {return QuestionAnswer;}public void setQuestionAnswer(ArrayList<String> questionAnswer) {QuestionAnswer = questionAnswer;}public TestPaper getTestPaper() {return testPaper;}public void setTestPaper(TestPaper testPaper) {this.testPaper = testPaper;}}public static class choiceQuestion extends Question{private ArrayList<String> result;public choiceQuestion(int num, String question, ArrayList<String> result) {super();this.setQuestionNum(getQuestionNum());this.setQuestion(getQuestion());this.result=result;this.setQuestionExist(true);this.setType("choice");}}public static class FillQuestion extends Question{public FillQuestion(int num, String question, String result) {super(num, question, result);this.setType("fillquestion");}}public static class PTA4 {}public static class Question {private int QuestionNum;private String question;private String result;private boolean isQuestionExist;private String type;public Question() {}public Question(int QuestionNum, String question, String result) {this.QuestionNum = QuestionNum;this.question = question;this.result = result;this.isQuestionExist = true;this.type = "normal";}public int getQuestionNum() {return QuestionNum;}public void setQuestionNum(int questionNum) {this.QuestionNum = questionNum;}public String getQuestion() {return question;}public void setQuestion(String question) {this.question = question;}public String getResult() {return result;}public void setResult(String result) {this.result = result;}public boolean isQuestionExist() {return isQuestionExist;}public void setQuestionExist(boolean questionExist) {isQuestionExist = questionExist;}public String getType() {return type;}public void setType(String type) {this.type = type;}}public static class Student {private String name;private String id;public Student(String id,String name) {this.name = name;this.id = id;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}}public static class TestPaper {private int TestNum;private ArrayList<Integer> TitleNum;private ArrayList<Integer> TitleScore;private ArrayList<String> TitleType;public TestPaper() {}public TestPaper(int testNum, ArrayList<Integer> titleNum, ArrayList<Integer> titleScore) {TestNum = testNum;TitleNum = titleNum;TitleScore = titleScore;}public int getTestNum() {return TestNum;}public void setTestNum(int testNum) {TestNum = testNum;}public ArrayList<Integer> getTitleNum() {return TitleNum;}public void setTitleNum(ArrayList<Integer> titleNum) {TitleNum = titleNum;}public ArrayList<Integer> getTitleScore() {return TitleScore;}public void setTitleScore(ArrayList<Integer> titleScore) {TitleScore = titleScore;}public ArrayList<String> getTitleType() {return TitleType;}public void setTitleType(ArrayList<String> titleType) {TitleType = titleType;}}public static class Type {private ArrayList<Integer>QuestionNum;private ArrayList<String>QuestionType;public Type(ArrayList<Integer> questionNum, ArrayList<String> questionType) {QuestionNum = questionNum;QuestionType = questionType;}public Type() {}public ArrayList<Integer> getQuestionNum() {return QuestionNum;}public void setQuestionNum(ArrayList<Integer> questionNum) {QuestionNum = questionNum;}public ArrayList<String> getQuestionType() {return QuestionType;}public void setQuestionType(ArrayList<String> questionType) {QuestionType = questionType;}}
}
此次的答题判题程序中, 选择题的选项有多个, 正确答案也可以有多个.在这道题中,我使用了一个Arrlist去储存题目的正确答案 在判断答题正误时, 应首先首先判断学生的答案中是否存在正确答案之外的答案, 如果存在, 则答题错误.
然后计算正确答案中, 有几个被学生回答到, 如果一个都没有, 则答题错误; 如果存在, 但数目小于正确答案个数, 则半对; 如果数目等于正确答案个数, 则回答全对.
2.nchu-software-oop-2024-上-4:智能家用电器系统(一)
(1)解题思路:本题需要读入串联电路的控制设备与受控设备的信息,并通过读入操作指令对受控设备的档位或状态进行调节,从而实现对受控设备的状态的改变
(2)类的设计
具体类图如图所示
类的设计具体代码如下:
public static class Switch extends ControlDevice {public Switch(double inputPins1, double outputPins2) {super(inputPins1, outputPins2);super.setOpen(false);}@Overridepublic void ChangeOpen() {super.ChangeOpen();}@Overridepublic double getVoltage() {if(isOpen()){return getInputPins1()-getOutputPins2();}else {return 0;}}}public static class LXcontroler extends ControlDevice{public LXcontroler(double inputPins1, double outputPins2) {super(inputPins1, outputPins2);}public LXcontroler() {}}public static class LightRG extends Light{public LightRG(ControlDevice controlDevice) {super(controlDevice);}@Overridepublic double getBrightness() {if(getControlDevice().getVoltage()==0){setBrightness(0);}else {setBrightness(180);}return super.getBrightness();}}public static class LightBZ extends Light{public LightBZ(ControlDevice controlDevice) {super(controlDevice);}@Overridepublic double getBrightness() {if(getControlDevice().getVoltage()>0&&getControlDevice().getVoltage()<=9){setBrightness(0);} else if (getControlDevice().getVoltage()>9) {setBrightness((getControlDevice().getVoltage()-10)*(150/210.0)+50);}return super.getBrightness();}}public static class Light extends Equipment{private double brightness;public Light(ControlDevice controlDevice) {super(controlDevice);}public double getBrightness() {return brightness;}public void setBrightness(double brightness) {this.brightness = brightness;}}public static class FDcontroler extends ControlDevice{public FDcontroler(double inputPins1, double outputPins2) {super(inputPins1, outputPins2);}public FDcontroler() {}private int i=0;public int getI() {return i;}public void setI(int i) {this.i = i;}public double[] getGearsArray() {return gearsArray;}public void setGearsArray(double[] gearsArray) {this.gearsArray = gearsArray;}private double[] gearsArray={0,0.3,0.6,0.9};public void addGears() {if(i<=2){i++;super.setGears(gearsArray[i]);}}public void delGears(){if(i>=1){i--;super.setGears(gearsArray[i]);}}}public static class Fans extends Equipment{private double speed;public Fans(ControlDevice controlDevice) {super(controlDevice);}public double getSpeed() {if(getControlDevice().getVoltage()<80){setSpeed(0);} else if (getControlDevice().getVoltage()<=150) {setSpeed(80+(getControlDevice().getVoltage()-80)*(360-80)/70);} else if (getControlDevice().getVoltage()>150) {setSpeed(360);}return speed;}public void setSpeed(double speed) {this.speed = speed;}}public static class Equipment {private ControlDevice controlDevice;private double gears;private double voltage;private String name;private String num;private boolean ispin1=false;private boolean ispin2=false;public boolean isIspin1() {return ispin1;}public void setIspin1(boolean ispin1) {this.ispin1 = ispin1;}public boolean isIspin2() {return ispin2;}public void setIspin2(boolean ispin2) {this.ispin2 = ispin2;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}public double getVoltage() {return voltage;}public void setVoltage(double voltage) {this.voltage = voltage;}public Equipment(ControlDevice controlDevice) {this.controlDevice = controlDevice;this.gears=controlDevice.getGears();this.voltage=controlDevice.getVoltage();}public Equipment() {}public ControlDevice getControlDevice() {return controlDevice;}public void setControlDevice(ControlDevice controlDevice) {this.controlDevice = controlDevice;}public double getGears() {return gears;}public void setGears(double gears) {this.gears = gears;}}public static class ControlDevice {private double gears=0;//档位private boolean isOpen;private double InputPins1=0;//输入引脚private double OutputPins2=0;//输出引脚private boolean ispin1=false;private boolean ispin2=false;private String name;private String num;public String getNum() {return num;}public void setNum(String num) {this.num = num;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isIspin1() {return ispin1;}public void setIspin1(boolean ispin1) {this.ispin1 = ispin1;}public boolean isIspin2() {return ispin2;}public void setIspin2(boolean ispin2) {this.ispin2 = ispin2;}private double voltage;public ControlDevice(double inputPins1, double outputPins2) {this.isOpen = true;this.InputPins1 = inputPins1;this.OutputPins2 = outputPins2;}public boolean isOpen() {return isOpen;}public void setOpen(boolean open) {isOpen = open;}public double getVoltage() {if(isOpen){return (InputPins1-OutputPins2)*gears;}else {return 0;}}public void setVoltage(double voltage) {this.voltage = voltage;}public ControlDevice() {this.isOpen=true;}public double getGears() {return gears;}public void setGears(double gears) {if(gears>=0.00&&gears<=1.00){this.gears = gears;}}public void ChangeOpen() {if(this.isOpen){this.isOpen=false;}else {this.isOpen=true;}}public boolean getOpen() {return isOpen;}public double getInputPins1() {return InputPins1;}public void setInputPins1(double inputPins1) {InputPins1 = inputPins1;}public double getOutputPins2() {return OutputPins2;}public void setOutputPins2(double outputPins2) {OutputPins2 = outputPins2;}}public static class circuitCL {private ControlDevice controlDevice;private Equipment equipment;private Switch aSwitch=null;public circuitCL() {}public ControlDevice getControlDevice() {return controlDevice;}public void setControlDevice(ControlDevice controlDevice) {this.controlDevice = controlDevice;}public Equipment getEquipment() {return equipment;}public void setEquipment(Equipment equipment) {this.equipment = equipment;}public Switch getaSwitch() {return aSwitch;}public void setaSwitch(Switch aSwitch) {this.aSwitch = aSwitch;}}
}
对于任何不同的电器,都应该存在输入电压和输出电压的方法。因此我们应该把输入电压和输出的作为父类的方法。 并且可以通过将输入输出电压方法作为这个接口的方法,并由父类设备类实现这个方法。
3.nchu-software-oop-2024-上-6:智能家用电器系统(二)
(1)解题思路:本题将并联电路拆分为多个串联电路并将其储存在一个ArryList中作为一个并联电路,然后将总电路看作一个串联电路,将并联电路看作该串联电路的一个用电器,进行操作
(2)类的设计:
类图如下
具体类的实现代码如下:
class RGlight extends equipment {public RGlight(String name) {super(name);}private double light;public double getLight() {if(getVoltage()==0){setLight(0);} else {setLight(180);}return light;}public void setLight(double light) {this.light = light;}}class sort {public sort() {}public void bubbleSort(ArrayList<DFans> list) {for(int i = 0; i < list.size() - 1; ++i) {for(int j = 0; j < list.size() - i - 1; ++j) {if (((equip)list.get(j)).getName().compareTo(((equip)list.get(j + 1)).getName()) > 0) {Collections.swap(list, j, j + 1);}}}}}class Switch extends ControlDevice {public Switch() {}public Switch(String name) {setName(name);setGears(0);}public void changeopen(){if(getGears()==0){setGears(1.0);}else if(getGears()==1.0){setGears(0);}}}class LXGovernor extends ControlDevice {public LXGovernor() {}public LXGovernor(String name) {super(name);setGears(0);}}class LFans extends equipment {public LFans(String name) {super(name);setResistence(20);}private double speed;public double getSpeed() {if(getVoltage()>=80&&getVoltage()<100){setSpeed(80);} else if (getVoltage()>=100&&getVoltage()<120) {setSpeed(160);}else if (getVoltage()>=120&&getVoltage()<140) {setSpeed(260);}else if (getVoltage()>=140) {setSpeed(360);}return speed;}public void setSpeed(double speed) {this.speed = speed;}}class FDGovernor extends ControlDevice {private double[] gears=new double[]{0.0,0.3,0.6,0.9};private int i=0;public void setGears(double[] gears) {this.gears = gears;}public int getI() {return i;}public void setI(int i) {this.i = i;}public FDGovernor() {}public FDGovernor(String name) {super(name);setGears(0);}public void addgears(){if(i<3){i++;setGears(gears[i]);}}public void delgears(){if(i>0){i--;setGears(gears[i]);}}}class equipment extends equip {private String name;private String num;private String pin1name;private String pin2name;private double voltage;private double resistence;public equipment(String name) {this.name = name;}public double getResistence() {return resistence;}public void setResistence(double resistence) {this.resistence = resistence;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}public String getPin1name() {return pin1name;}public void setPin1name(String pin1name) {this.pin1name = pin1name;}public String getPin2name() {return pin2name;}public void setPin2name(String pin2name) {this.pin2name = pin2name;}public double getVoltage() {return voltage;}public void setVoltage(double voltage) {this.voltage = voltage;}}class equip {private String name;private String num;public equip() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}}class ControlDevice extends equip {private String name;private String num;private double gears;public ControlDevice() {}public ControlDevice(String name) {this.name = name;setGears(0);}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}public double getGears() {return gears;}public void setGears(double gears) {this.gears = gears;}}class DFans extends equipment {private double speed;public DFans(String name) {super(name);setResistence(20.0);}public double getSpeed() {if(getVoltage()>=80&&getVoltage()<=150){setSpeed((getVoltage()-80)*((360-80)/70.0)+80);} else if (getVoltage()>150){setSpeed(360);} else if (getVoltage()<80) {setSpeed(0);}return speed;}public void setSpeed(double speed) {this.speed = speed;}}class circuitBL extends circuit {private ArrayList<circultCL> circultCLArrayList=new ArrayList<>();private double voltage;private double resistence;private boolean istrue=true;private String name;public circuitBL() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public ArrayList<circultCL> getCircultCLArrayList() {return circultCLArrayList;}public void setCircultCLArrayList(ArrayList<circultCL> circultCLArrayList) {this.circultCLArrayList = circultCLArrayList;}public double getVoltage() {return voltage;}public void setVoltage(double voltage) {this.voltage = voltage;}public double getResistence() {double resistence=0;for (int i = 0; i < circultCLArrayList.size(); i++) {//System.out.println(circultCLArrayList.get(i).isIstrue());if(circultCLArrayList.get(i).isIstrue()){resistence+=1.0/ circultCLArrayList.get(i).getResistence();}}resistence=1.0/resistence;return resistence;}public void setResistence(double resistence) {this.resistence = resistence;}public boolean isIstrue() {return istrue;}public void setIstrue(boolean istrue) {this.istrue = istrue;}}class BZlight extends equipment {public BZlight(String name) {super(name);}private double light;public double getLight() {if(getVoltage()>=0&&getVoltage()<=9){setLight(0);} else if(getVoltage()>9){setLight((getVoltage()-10)*(150/210)+50);}return light;}public void setLight(double light) {this.light = light;}}class circuit extends equip {}class circultCL extends circuit {private String name;private ArrayList<equip>equipArrayList = new ArrayList<>();private double voltage;private double resistence;private boolean istrue=true;public circultCL() {this.istrue = istrue;}public circultCL(String name) {this.name = name;}public ArrayList<equip> getEquipArrayList() {return equipArrayList;}public void setEquipArrayList(ArrayList<equip> equipArrayList) {this.equipArrayList = equipArrayList;}public double getVoltage() {return voltage;}public void setVoltage(double voltage) {this.voltage = voltage;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getResistence() {double resistence=0;for (int i = 0; i < equipArrayList.size(); i++) {if(equipArrayList.get(i) instanceof Switch){if(((Switch) equipArrayList.get(i)).getGears()==0){setIstrue(false);return 0;}}if(istrue&&equipArrayList.get(i) instanceof equipment){resistence+=((equipment) getEquipArrayList().get(i)).getResistence();}}if(isIstrue()){this.setResistence(resistence);return resistence;}else {this.setResistence(0);return 0;}}public void setResistence(double resistence) {this.resistence = resistence;}public boolean isIstrue() {return istrue;}public void setIstrue(boolean istrue) {this.istrue = istrue;}}
三、采坑心得
(1)对字符串的处理方面,要从字符串中提取出有效信息并进行存储,而在提取有效信息方面正则表达式的格式是一个重要问题,在这三次题集中,由于正则表达式的格式错误,导致了输入无效信息等一系列错误
(2)在并联电路中,我们需要考虑到电源短路的情况
例如:
Exception in thread "main" java.lang.NumberFormatException: Cannot parse null string
at java.base/java.lang.Integer.parseInt(Integer.java:624)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at Paper.sumscoreJudge(Main.java:23) at Main.main(Main.java:66)
(2)在使用ArryList.get( )时,由于未注意List的大小,导致数组越界
例如,在PTA-4中报错
Exception in thread "main" java.lang.NullPointerException: Cannot assign field "ques" because "question[no]" is null
at Main.main(Main.java:39)
(3)在设计子类时,经常忘记重写父类的方法
2.(1)nchu-software-oop-2024-上-4:在最初设计本题的过程中,我对于如何输入并储存数据并不清楚,这是一个难点。后续我引入了hashmap来进行数据储存
(2)nchu-software-oop-2024-上-5:对于这道题目我们只需要输入后对相关字符串进行分割后进行储存即可,只需要注意理解题目意思即可,比如在对调档器进行调低一挡操作
时,需要注意调档器的最高的档位和最低的档位为3和0,如果未注意则会出现大面积错误
(3)nchu-software-oop-2024-上-6:对与这道题目而言,需要对电路进行分割,需要注意的地方在于要判断支路中的开关是否全部闭合,因为题目中并联电路的开关不一定全部闭合所以并联电路中的用电器不一定都有电压,其他注意点同nchu-software-oop-2024-上-5
3.这三次大作业的心得:
(1)理解编程逻辑的重要性:在编写答题判题程序时,逻辑是关键。确保你完全理解题目的要求和评分标准,然后将其转化为代码逻辑。这需要对编程和数据结构有深入的理解
(2)模块化编程的便利:将程序划分为多个模块或函数可以使代码更易于理解和维护。每个模块负责特定的功能,如读取题目、解析答案、评分等。
(3)异常处理的必要性:在编程过程中,会遇到各种意外情况,如输入错误、对应信息不存在等。使用异常处理机制可以确保程序在遇到问题时能够优雅地处理,而不是崩溃。
四、改进建议
1.nchu-software-oop-2024-上-4:(1)可考虑使用hashmap类进行数据存储
(2)可对正则表达式进行改进,使其能够更好地能和其进行匹配
2.nchu-software-oop-2024-上-5:(1)在设计调档器时应对调档器地最高档和最低档进行限定
3.nchu-software-oop-2024-上-6:(1)在设计电路时应注意电路中用电器串联的顺序,同时设计一个判断器用于判断开关是否全部闭合
五、总结
1.通过这次作业我学到了:
(1)继承的使用方法、注意事项及性质
(2)多态的实现途径及多态的主要作用
(3)接口的实现方法、使用时的注意事项及多态的性质
(4)掌握Java语言中抽象类的基本概念及使用方法
(5)在这次作页中还有一个很大的问题在于对题目意思理解不到位,导致许多预定功能没有实现
(6)通过这几次学习,我更加认识到设计类的重要性,否则写到后面容易出错