第七到八次PTA作业总结
前言
在最后的两次PTA作业中,难度和代码量无疑是最大最复杂的,因此我对这两次代码的完成度并不是太理想,只能堪堪通过为数不多的几个测试点。首先,对于第七次作业相较于前两次作业,又额外要求了互斥开关与受控窗帘的数据输入,并且在电路的串并联方面有了更为严苛的要求,只这两个点便使得这次的难度比起上一次作业难度大大加大。还有最后的第八次作业中还有添加了二极管这种电路原件,这也对我们的电学知识也是考验。
设计与分析:
第七次作业的类图:

import java.util.*;
import java.text.DecimalFormat;
class Device {
protected int id;
protected int inputPin;
protected int outputPin;
public Device(int id, int inputPin, int outputPin) {
this.id = id;
this.inputPin = inputPin;
this.outputPin = outputPin;
}
}
class Switch extends Device {
private boolean status;
public Switch(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.status = false; // default status is off
}
public void setStatus(boolean status) {
this.status = status;
}
public boolean getStatus() {
return this.status;
}
}
class SelectorSwitch extends Device {
private int selectedPin;
public SelectorSwitch(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.selectedPin = inputPin; // default selected pin is input pin
}
public void setSelectedPin(int selectedPin) {
this.selectedPin = selectedPin;
}
public int getSelectedPin() {
return this.selectedPin;
}
}
class SpeedAdjuster extends Device {
private int gear;
public SpeedAdjuster(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.gear = 0; // default gear is 0
}
public void increaseGear() {
this.gear++;
if (this.gear > 4) {
this.gear = 4;
}
}
public void decreaseGear() {
this.gear--;
if (this.gear < 0) {
this.gear = 0;
}
}
public int getGear() {
return this.gear;
}
}
class DimmableLight extends Device {
private double brightness;
public DimmableLight(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.brightness = 0; // default brightness is 0
}
public void setBrightness(double brightness) {
this.brightness = brightness;
}
public double getBrightness() {
return this.brightness;
}
}
class Fan extends Device {
private int speed;
public Fan(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.speed = 0; // default speed is 0
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSpeed() {
return this.speed;
}
}
class ExclusiveSwitch extends Device {
private boolean pin2Connected;
public ExclusiveSwitch(int id, int pin1, int pin2, boolean pin2Connected) {
super(id, pin1, pin2);
this.pin2Connected = pin2Connected;
}
public boolean isPin2Connected() {
return this.pin2Connected;
}
public void switchConnection() {
this.pin2Connected = !this.pin2Connected;
}
}
class ControlledCurtain extends Device {
private double openness;
private double[] brightnessRange = new double[]{0, 50, 100, 200, 300, 400};
public ControlledCurtain(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.openness = 100; // default openness is 100
}
public void calculateOpenness(double totalBrightness) {
if (totalBrightness < brightnessRange[0]) {
this.openness = 100; // fully open
} else if (totalBrightness >= brightnessRange[brightnessRange.length - 1]) {
this.openness = 0; // fully closed
} else {
for (int i = 1; i < brightnessRange.length; i++) {
if (totalBrightness < brightnessRange[i]) {
double ratio = (brightnessRange[i] - totalBrightness) / (brightnessRange[i] - brightnessRange[i - 1]);
this.openness = 100 - ratio * 20 * (i - 1);
break;
}
}
}
}
public double getOpenness() {
return this.openness;
}
}
public class Main {
private static DecimalFormat decimalFormat = new DecimalFormat("#.00");
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read devices information
Map<String, Device> deviceMap = new HashMap<>();
int deviceId = 1;
while (true) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
String[] deviceInfo = line.split(" ");
if (deviceInfo[0].equals("K")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new Switch(deviceId, 1, 2));
} else if (deviceInfo[0].equals("F")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new SpeedAdjuster(deviceId, 1, 2));
} else if (deviceInfo[0].equals("L")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new DimmableLight(deviceId, 1, 2));
} else if (deviceInfo[0].equals("B")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new DimmableLight(deviceId, 1, 2));
} else if (deviceInfo[0].equals("R")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new DimmableLight(deviceId, 1, 2));
} else if (deviceInfo[0].equals("D")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new Fan(deviceId, 1, 2));
} else if (deviceInfo[0].equals("A")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new Fan(deviceId, 1, 2));
} else if (deviceInfo[0].equals("H")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new ExclusiveSwitch(deviceId, 1, 2, true));
} else if (deviceInfo[0].equals("S")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new ControlledCurtain(deviceId, 1, 2));
}
deviceId++;
}
// Read connections information
List<List<Device>> circuits = new ArrayList<>();
while (true) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
String[] connections = line.substring(line.indexOf(":") + 1).split(" ");
List<Device> circuit = new ArrayList<>();
for (String connection : connections) {
String[] pins = connection.substring(1, connection.length() - 1).split(" ");
Device device = deviceMap.get(pins[0]);
if (pins.length == 2) {
device.outputPin = Integer.parseInt(pins[1]);
}
circuit.add(device);
}
circuits.add(circuit);
}
// Simulate circuits
for (List<Device> circuit : circuits) {
for (int i = 0; i < circuit.size() - 1; i++) {
Device device1 = circuit.get(i);
Device device2 = circuit.get(i + 1);
if (device1.outputPin != 0) {
device2.inputPin = device1.outputPin;
}
}
}
// Read and process control information
while (true) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
if (line.startsWith("#")) {
String controlType = line.substring(1, 2);
String deviceIdentifier = line.substring(2).split(":")[0];
if (controlType.equals("K")) {
Switch switchDevice = (Switch) deviceMap.get(deviceIdentifier);
switchDevice.setStatus(!switchDevice.getStatus());
} else if (controlType.equals("F")) {
SpeedAdjuster speedAdjuster = (SpeedAdjuster) deviceMap.get(deviceIdentifier);
if (line.endsWith("+")) {
speedAdjuster.increaseGear();
} else {
speedAdjuster.decreaseGear();
}
} else if (controlType.equals("L")) {
DimmableLight dimmableLight = (DimmableLight) deviceMap.get(deviceIdentifier);
double brightness = Double.parseDouble(line.substring(3));
dimmableLight.setBrightness(brightness);
} else if (controlType.equals("H")) {
ExclusiveSwitch exclusiveSwitch = (ExclusiveSwitch) deviceMap.get(deviceIdentifier);
exclusiveSwitch.switchConnection();
}
}
}
// Calculate device parameters and output
for (String key : deviceMap.keySet()) {
Device device = deviceMap.get(key);
int id = device.id;
if (device instanceof Switch) {
Switch switchDevice = (Switch) device;
String status = switchDevice.getStatus() ? "closed" : "turned on";
System.out.println("@K" + id + ":" + status);
} else if (device instanceof SpeedAdjuster) {
SpeedAdjuster speedAdjuster = (SpeedAdjuster) device;
System.out.println("@F" + id + ":" + speedAdjuster.getGear());
} else if (device instanceof DimmableLight) {
DimmableLight dimmableLight = (DimmableLight) device;
System.out.println("@B" + id + ":" + decimalFormat.format(dimmableLight.getBrightness()));
} else if (device instanceof Fan) {
Fan fan = (Fan) device;
System.out.println("@D" + id + ":" + fan.getSpeed());
} else if (device instanceof ExclusiveSwitch) {
ExclusiveSwitch exclusiveSwitch = (ExclusiveSwitch) device;
String status = exclusiveSwitch.isPin2Connected() ? "closed" : "turned on";
System.out.println("@H" + id + ":" + status);
} else if (device instanceof ControlledCurtain) {
ControlledCurtain controlledCurtain = (ControlledCurtain) device;
double openness = controlledCurtain.getOpenness();
System.out.println("@S" + id + ":" + decimalFormat.format(openness) + "%");
}
}
scanner.close();
}
}
问题与心得:
1、代码结构不清晰:整个程序代码被写在一个类中,没有进行合理拆分和组织,可读性较差。
2、类型转换问题:在控制命令解析阶段,通过字符串截取和转换来获取设备类型和参数,存在潜在的类型转换异常的风险。
3、代码可读性不高:代码中存在一些复杂的逻辑判断和循环结构,可读性较差。
改进建议:
以下是对代码的改进建议:
- 提高代码的可读性和可维护性:使用适当的命名规范,例如使用驼峰命名法,准确描述变量和函数的含义,使代码更易于理解和修改。
- 引入面向对象的思想:将设备和电路信息抽象为类,使用类的继承和组合关系进行模拟,提高代码的可扩展性和重用性。
- 封装设备和电路的状态和参数:将设备状态和参数封装在类中,并提供方法来操作和获取这些信息,避免直接访问类的成员变量。
设计与分析:
第八次作业的类图:

代码:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.*;
import java.text.DecimalFormat;
class Device {
protected int id;
protected int inputPin;
protected int outputPin;
protected double voltage;
public Device(int id, int inputPin, int outputPin) {
this.id = id;
this.inputPin = inputPin;
this.outputPin = outputPin;
}
public void setVoltage(double voltage) {
this.voltage = voltage;
}
public double getVoltage() {
return this.voltage;
}
public boolean checkCurrentLimit(double totalCurrent) {
return false;
}
}
class ControlledEquipment extends Device {
protected double currentLimit;
public ControlledEquipment(int id, int inputPin, int outputPin, double currentLimit) {
super(id, inputPin, outputPin);
this.currentLimit = currentLimit;
}
public boolean checkCurrentLimit(double current) {
return current > currentLimit;
}
}
class SecondaryTube extends ControlledEquipment {
private String status;
public SecondaryTube(int id, int inputPin, int outputPin, double currentLimit) {
super(id, inputPin, outputPin, currentLimit);
this.status = "cutoff"; // default status is cutoff
}
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return this.status;
}
}
class Switch extends Device {
private boolean status;
public Switch(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.status = false; // default status is off
}
public void setStatus(boolean status) {
this.status = status;
}
public boolean getStatus() {
return this.status;
}
}
class SelectorSwitch extends Device {
private int selectedPin;
public SelectorSwitch(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.selectedPin = inputPin; // default selected pin is input pin
}
public void setSelectedPin(int selectedPin) {
this.selectedPin = selectedPin;
}
public int getSelectedPin() {
return this.selectedPin;
}
}
class SpeedAdjuster extends Device {
private int gear;
public SpeedAdjuster(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.gear = 0; // default gear is 0
}
public void increaseGear() {
this.gear++;
if (this.gear > 4) {
this.gear = 4;
}
}
public void decreaseGear() {
this.gear--;
if (this.gear < 0) {
this.gear = 0;
}
}
public int getGear() {
return this.gear;
}
}
class DimmableLight extends Device {
private double brightness;
public DimmableLight(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.brightness = 0; // default brightness is 0
}
public void setBrightness(double brightness) {
this.brightness = brightness;
}
public double getBrightness() {
return this.brightness;
}
}
class Fan extends Device {
private int speed;
public Fan(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.speed = 0; // default speed is 0
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSpeed() {
return this.speed;
}
}
class ExclusiveSwitch extends Device {
private boolean pin2Connected;
public ExclusiveSwitch(int id, int pin1, int pin2, boolean pin2Connected) {
super(id, pin1, pin2);
this.pin2Connected = pin2Connected;
}
public boolean isPin2Connected() {
return this.pin2Connected;
}
public void switchConnection() {
this.pin2Connected = !this.pin2Connected;
}
}
class ControlledCurtain extends Device {
private double openness;
private double[] brightnessRange = new double[]{0, 50, 100, 200, 300, 400};
public ControlledCurtain(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
this.openness = 100; // default openness is 100
}
public void calculateOpenness(double totalBrightness) {
if (totalBrightness < brightnessRange[0]) {
this.openness = 100; // fully open
} else if (totalBrightness >= brightnessRange[brightnessRange.length - 1]) {
this.openness = 0; // fully closed
} else {
for (int i = 1; i < brightnessRange.length; i++) {
if (totalBrightness < brightnessRange[i]) {
double ratio = (brightnessRange[i] - totalBrightness) / (brightnessRange[i] - brightnessRange[i - 1]);
this.openness = 100 - ratio * 20 * (i - 1);
break;
}
}
}
}
public double getOpenness() {
return this.openness;
}
}
public class Main {
private static DecimalFormat decimalFormat = new DecimalFormat("#.00");
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read devices information
Map<String, Device> deviceMap = new HashMap<>();
int deviceId = 1;
while (true) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
String[] deviceInfo = line.split(" ");
double voltage = Double.parseDouble(deviceInfo[1]);
if (deviceInfo[0].equals("K")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new Switch(deviceId, 1, 2));
} else if (deviceInfo[0].equals("F")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new SpeedAdjuster(deviceId, 1, 2));
} else if (deviceInfo[0].equals("L")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new DimmableLight(deviceId, 1, 2));
} else if (deviceInfo[0].equals("B")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new DimmableLight(deviceId, 1, 2));
} else if (deviceInfo[0].equals("R")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new DimmableLight(deviceId, 1, 2));
} else if (deviceInfo[0].equals("D")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new Fan(deviceId, 1, 2));
} else if (deviceInfo[0].equals("A")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new Fan(deviceId, 1, 2));
} else if (deviceInfo[0].equals("H")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new ExclusiveSwitch(deviceId, 1, 2, true));
} else if (deviceInfo[0].equals("S")) {
deviceMap.put(deviceInfo[0] + deviceInfo[1], new ControlledCurtain(deviceId, 1, 2));
}
deviceMap.get(deviceInfo[0] + deviceInfo[1]).setVoltage(voltage);
deviceId++;
}
// Read connections information
List<List<Device>> circuits = new ArrayList<>();
while (true) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
String[] connections = line.substring(line.indexOf(":") + 1).split(" ");
List<Device> circuit = new ArrayList<>();
for (String connection : connections) {
String[] pins = connection.substring(1, connection.length() - 1).split(" ");
Device device = deviceMap.get(pins[0]);
if (pins.length == 2) {
device.outputPin = Integer.parseInt(pins[1]);
}
circuit.add(device);
}
circuits.add(circuit);
}
// Simulate circuits
boolean hasShortCircuit = false;
for (List<Device> circuit : circuits) {
for (int i = 0; i < circuit.size() - 1; i++) {
Device device1 = circuit.get(i);
Device device2 = circuit.get(i + 1);
if (device1.outputPin != 0) {
device2.inputPin = device1.outputPin;
}
if (device1.getVoltage() == 0 || device2.getVoltage() == 0) {
hasShortCircuit = true;
break;
}
}
if (hasShortCircuit) {
break;
}
}
// Read and process control information
while (true) {
String line = scanner.nextLine();
if (line.equals("end")) {
break;
}
if (line.startsWith("#")) {
String controlType = line.substring(1, 2);
String deviceIdentifier = line.substring(2).split(":")[0];
Device device = deviceMap.get(deviceIdentifier);
double totalCurrent = calculateTotalCurrent(deviceMap);
if (device instanceof Switch) {
Switch switchDevice = (Switch) device;
switchDevice.setStatus(!switchDevice.getStatus());
} else if (device instanceof SpeedAdjuster) {
SpeedAdjuster speedAdjuster = (SpeedAdjuster) device;
if (line.endsWith("+")) {
speedAdjuster.increaseGear();
} else {
speedAdjuster.decreaseGear();
}
} else if (device instanceof DimmableLight) {
DimmableLight dimmableLight = (DimmableLight) device;
double brightness = Double.parseDouble(line.substring(3));
dimmableLight.setBrightness(brightness);
} else if (device instanceof ExclusiveSwitch) {
ExclusiveSwitch exclusiveSwitch = (ExclusiveSwitch) device;
exclusiveSwitch.switchConnection();
} else if (device instanceof ControlledCurtain) {
ControlledCurtain controlledCurtain = (ControlledCurtain) device;
double totalBrightness = calculateTotalBrightness(deviceMap);
controlledCurtain.calculateOpenness(totalBrightness);
}
if (totalCurrent > 0 && device.checkCurrentLimit(totalCurrent)) {
System.out.print(decimalFormat.format(device.getVoltage()) + " " + decimalFormat.format(totalCurrent) + " ");
}
}
}
// Calculate device parameters and output
if (hasShortCircuit) {
System.out.println("short circuit error");
} else {
for (String key : deviceMap.keySet()) {
Device device = deviceMap.get(key);
int id = device.id;
if (device instanceof Switch) {
Switch switchDevice = (Switch) device;
String status = switchDevice.getStatus() ? "closed" : "turned on";
System.out.println("@K" + id + ":" + status);
} else if (device instanceof SpeedAdjuster) {
SpeedAdjuster speedAdjuster = (SpeedAdjuster) device;
System.out.println("@F" + id + ":" + speedAdjuster.getGear());
} else if (device instanceof DimmableLight) {
DimmableLight dimmableLight = (DimmableLight) device;
System.out.println("@B" + id + ":" + decimalFormat.format(dimmableLight.getBrightness()));
} else if (device instanceof Fan) {
Fan fan = (Fan) device;
System.out.println("@D" + id + ":" + fan.getSpeed());
} else if (device instanceof ExclusiveSwitch) {
ExclusiveSwitch exclusiveSwitch = (ExclusiveSwitch) device;
String status = exclusiveSwitch.isPin2Connected() ? "closed" : "turned on";
System.out.println("@H" + id + ":" + status);
} else if (device instanceof ControlledCurtain) {
ControlledCurtain controlledCurtain = (ControlledCurtain) device;
double openness = controlledCurtain.getOpenness();
System.out.println("@S" + id + ":" + decimalFormat.format(openness) + "%");
}
}
}
scanner.close();
}
public static double calculateTotalCurrent(Map<String, Device> deviceMap) {
double totalCurrent = 0;
for (String key : deviceMap.keySet()) {
Device device = deviceMap.get(key);
if (device instanceof ControlledEquipment) {
totalCurrent += device.getVoltage() / ((ControlledEquipment) device).currentLimit;
}
}
return totalCurrent;
}
public static double calculateTotalBrightness(Map<String, Device> deviceMap) {
double totalBrightness = 0;
for (String key : deviceMap.keySet()) {
Device device = deviceMap.get(key);
if (device instanceof DimmableLight) {
totalBrightness += ((DimmableLight) device).getBrightness();
}
}
return totalBrightness;
}
}
问题与心得:
1、设备类型判断过多:在主函数中使用大量的if-else语句进行设备类型判断,这会导致代码冗长且难以扩展和维护。
2、可读性:代码中存在较长的方法和类名,不符合命名规范,降低了代码的可读性。
3、单一职责原则:一些设备类中存在过多的功能,不符合单一职责原则。
4、设备连接关系处理:在处理设备连接关系时,使用了嵌套的List和多层循环,使得代码结构复杂,处理起来不够直观和高效。
5、数据类型:某些计算中使用了double数据类型,可能导致精度问题。
6、代码重复:在计算总电流和总亮度时,存在大量的遍历设备的重复代码。
改进建议:
1、使用工厂模式来创建不同类型的设备对象,避免大量的判断语句。
2、可以使用Map集合来表示并处理设备之间的连接关系,提高处理效率和代码可读性。
3、提取计算总电流和总亮度的方法,避免重复代码
对七、八次作业的总结:
在这两次的作业中,我能够依据处理后的数据来对各个设备进行调控,对各个元件随状态不同进行相应的调整与更新。但是,由于我个人能力的限制,我不得不寻求ai和同学的帮助,以此来分析其中的错误和漏洞,并尝试找到可行的方法来解决问题,但是在时间截止后,我仍然没有成功将问题解决。但我相信,随着我不断学习和提升自己的技能,我将能够解决当前面临的技术难题并取得进步。我会继续参考同学们的优秀代码,结合新学到的知识,重新审视问题并尝试找出解决方案。通过持续不断的学习和实践,我相信自己能够攻克这些难题,提升自己的编程水平和问题解决能力。虽然当前的作业可能困难,但我深知其中潜藏着宝贵的学习机会,能够帮助我更明确未来的学习方向和目标。
对八次作业的总结
这九次作业的知识是逐渐递进的,慢慢的将面对对象编程的逻辑思维由浅及深的让我们接受与消化。
在第一到三次的作业中,题目较为简单,主要目的在于使我们熟悉JAVA的基本语法:输入输出、条件语句、循环语句、数组等与上学期C语言相似的东西,以及不同的类(class),也对JAVA设计模式有了初步的掌握与了解。并且也在对各种复杂的数据的处理中学会了使用正则表达式,这也使得在之后的代码编写中减少了在对数据处理这关键的一步上造成的时间的浪费。
在第五到六次作业中我们已经学到了继承和多态的相关知识,需要合理的使用设计模式并且清晰的理解各个类之间存在的关系才能更好更合理的处理和解决需求和增强代码的复用性,在之后的代码迭代中更快速的解决问题。
这篇博客也其实也是对这学期共八次的PTA作业的一次总结,这八次中前三次的代码较为基础,并且夹杂着一些让我们练手的基础题,但在后面的作业中难度也渐渐增加,特别是是在第五次作业之后,原来还算简单的基础题也被删去,只剩下难度更为巨大程序设计题,因此这之后的作业都完成的不是特别理想。这也说明了我在面向对象设计这一课程上的水平还远远没有达标,对JAVA于语言的知识体系还掌握的也不全面和深入。在面对一些特别复杂的问题时,我也不能够做到游刃有余的分析和解决问题。这个学期的学习也让我明白了自主学习和持续学习的重要性。在未来的学习发展中,我也会更加细致入微,加强对自己的时间观念的培养,同时不断练习自己的编程能力以此来提高自己的水平。也希望我能够更加努力的学习,争取在不远的将来的学习或是工作中取得自己满意的结果。

浙公网安备 33010602011771号