课程阶段性总结

news/2024/10/8 2:26:21

前言:

学习Java到了第二个阶段了,通过这几个月的学习,我对Java的了解逐渐深入.但是随着深入学习,我发现编写代码所需要的知识越来越多,就需要我们不断学习更多的知识.通过这几次的大作业,让我成长的非常迅速,为我提供了宝贵的实践机会。我将对题目集的知识点、题量及难度进行简要总结。

一.知识点

1.抽象类和方法 2.封装 3.多态 4.接口

二.题量

题量相较于前几次是非常少的.量少,但是非常经典.

三.难度

难度是非常大的,一次大作业每每需要五六天时间来完成,虽然难但是所涉及到的知识点非常广泛,学习到的东西非常多.

设计与分析:

代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 电路设备基类
abstract class CircuitDevice {
protected int id;
protected int inputPin;
protected int outputPin;

public CircuitDevice(int id, int inputPin, int outputPin) {
this.id = id;
this.inputPin = inputPin;
this.outputPin = outputPin;
}

public abstract double getOutputVoltage(double inputVoltage);
}

// 控制设备类
class ControlDevice extends CircuitDevice {
public ControlDevice(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
}
}

// 开关类
class Switch extends ControlDevice {
private int state; // 0表示打开,1表示关闭

public Switch(int id) {
super(id, 1, 2);
this.state = 0;
}

public void toggleState() {
this.state = 1 - this.state;
}

@Override
public double getOutputVoltage(double inputVoltage) {
return this.state == 0 ? inputVoltage : 0;
}
}

// 分档调速器类
class StepDimmer extends ControlDevice {
private int level; // 0-3表示4个档位

public StepDimmer(int id) {
super(id, 1, 2);
this.level = 0;
}

public void increaseLevel() {
this.level = Math.min(this.level + 1, 3);
}

public void decreaseLevel() {
this.level = Math.max(this.level - 1, 0);
}

@Override
public double getOutputVoltage(double inputVoltage) {
return inputVoltage * (this.level * 0.3);
}
}

// 连续调速器类
class ContinuousDimmer extends ControlDevice {
private double level; // 0.00-1.00

public ContinuousDimmer(int id) {
super(id, 1, 2);
this.level = 0.0;
}

public void setLevel(double level) {
this.level = Math.max(0.0, Math.min(1.0, level));
}

@Override
public double getOutputVoltage(double inputVoltage) {
return inputVoltage * this.level;
}
}

// 受控设备类
class ControlledDevice extends CircuitDevice {
public ControlledDevice(int id, int inputPin, int outputPin) {
super(id, inputPin, outputPin);
}

public abstract double getOutputParameter(double inputVoltage);
}

// 白炽灯类
class IncandescentLight extends ControlledDevice {
private double brightness; // 0-200 lux

public IncandescentLight(int id) {
super(id, 1, 2);
this.brightness = 0;
}

@Override
public double getOutputParameter(double inputVoltage) {
if (inputVoltage >= 0 && inputVoltage < 9) {
this.brightness = 0;
} else if (inputVoltage >= 10 && inputVoltage <= 220) {
this.brightness = (inputVoltage - 10) / 210.0 * 200;
} else {
this.brightness = 200;
}
return Math.floor(this.brightness);
}
}

// 日光灯类
class FluorescentLight extends ControlledDevice {
private double brightness; // 0 or 180 lux

public FluorescentLight(int id) {
super(id, 1, 2);
this.brightness = 0;
}

@Override
public double getOutputParameter(double inputVoltage) {
this.brightness = inputVoltage == 0 ? 0 : 180;
return this.brightness;
}
}

// 吊扇类
class CeilingFan extends ControlledDevice {
private double speed; // 80-360 rpm

public CeilingFan(int id) {
super(id, 1, 2);
this.speed = 0;
}

@Override
public double getOutputParameter(double inputVoltage) {
if (inputVoltage >= 80 && inputVoltage <= 150) {
this.speed = (inputVoltage - 80) / 70.0 * 280 + 80;
} else if (inputVoltage > 150) {
this.speed = 360;
} else {
this.speed = 0;
}
return Math.floor(this.speed);
}
}

// 电路连接类
class CircuitConnection {
private List<CircuitDevice> devices;
private double voltage;

public CircuitConnection() {
this.devices = new ArrayList<>();
this.voltage = 220.0;
}

public void addDevice(CircuitDevice device) {
this.devices.add(device);
}

public void processInput(String input) {
if (input.startsWith("#")) {
processCommand(input);
} else if (input.startsWith("[")) {
processConnection(input);
} else if (input.equals("end")) {
return;
} else {
System.out.println("Invalid input: " + input);
}
}

private void processCommand(String command) {
if (command.startsWith("#K")) {
int id = Integer.parseInt(command.substring(2));
for (CircuitDevice device : this.devices) {
if (device instanceof Switch && device.id == id) {
((Switch) device).toggleState();
break;
}
}
} else if (command.startsWith("#F")) {
int id = Integer.parseInt(command.substring(2, 3));
if (command.endsWith("+")) {
for (CircuitDevice device : this.devices) {
if (device instanceof StepDimmer && device.id == id) {
((StepDimmer) device).increaseLevel();
break;
}
}
} else if (command.endsWith("-")) {
for (CircuitDevice device : this.devices) {
if (device instanceof StepDimmer && device.id == id) {
((StepDimmer) device).decreaseLevel();
break;
}
}
}
} else if (command.startsWith("#L")) {
int id = Integer.parseInt(command.substring(2, 3));
double level = Double.parseDouble(command.substring(4));
for (CircuitDevice device : this.devices) {
if (device instanceof ContinuousDimmer && device.id == id) {
((ContinuousDimmer) device).setLevel(level);
break;
}
}
}
}

private void processConnection(String connection) {
String[] pins = connection.substring(1, connection.length() - 1).split(" ");
CircuitDevice prevDevice = null;
double prevOutputVoltage = this.voltage;

for (String pin : pins) {
String[] parts = pin.split("-");
int deviceId = Integer.parseInt(parts[0].substring(1));
int pinId = Integer.parseInt(parts[1]);

CircuitDevice device = null;
for (CircuitDevice d : this.devices) {
if (d.id == deviceId) {
device = d;
break;
}
}

if (device == null) {
if (parts[0].startsWith("V")) {
prevOutputVoltage = this.voltage;
} else if (parts[0].startsWith("G")) {
prevOutputVoltage = 0;
} else {
System.out.println("Invalid device: " + parts[0]);
return;
}
} else {
if (pinId == device.inputPin) {
device.getOutputVoltage(prevOutputVoltage);
} else if (pinId == device.outputPin) {
if (prevDevice != null) {
prevOutputVoltage = prevDevice.getOutputVoltage(prevOutputVoltage);
}
} else {
System.out.println("Invalid pin: " + pin);
return;
}

if (prevDevice != null) {
prevDevice.getOutputVoltage(prevOutputVoltage);
}
prevDevice = device;
}
}
}

public void printStatus() {
for (CircuitDevice device : this.devices) {
if (device instanceof Switch) {
System.out.printf("@K%d:%s%n", device.id, ((Switch) device).state == 0 ? "turned on" : "closed");
} else if (device instanceof StepDimmer) {
System.out.printf("@F%d:%d%n", device.id, ((StepDimmer) device).level);
} else if (device instanceof ContinuousDimmer) {
System.out.printf("@L%d:%.2f%n", device.id, ((ContinuousDimmer) device).level);
} else if (device instanceof IncandescentLight) {
System.out.printf("@B%d:%.0f%n", device.id, ((IncandescentLight) device).getOutputParameter(device.getOutputVoltage(this.voltage)));
} else if (device instanceof FluorescentLight) {
System.out.printf("@R%d:%.0f%n", device.id, ((FluorescentLight) device).getOutputParameter(device.getOutputVoltage(this.voltage)));
} else if (device instanceof CeilingFan) {
System.out.printf("@D%d:%.0f%n", device.id, ((CeilingFan) device).getOutputParameter(device.getOutputVoltage(this.voltage)));
}
}
}
}

public class SmartHomeSimulator {
public static void main(String[] args) {
CircuitConnection circuit = new CircuitConnection();
Scanner scanner = new Scanner(System.in);

while (true) {
String input = scanner.nextLine();
circuit.processInput(input);
if (input.equals("end")) {
break;
}
}

circuit.printStatus();
}
}

 devices:存储所有电路设备的列表。voltage:电路的输入电压,默认为220.0V。addDevice(CircuitDevice device):向电路中添加设备。processInput(String input):处理输入的命令或连接信息。processCommand(String command):处理命令。processConnection(String connection):处理设备之间的连接。printStatus():打印所有设备的状态。

 main方法中创建了一个CircuitConnection实例和一个Scanner实例,用于读取用户输入。在一个循环中读取输入,直到输入为"end"时退出循环。调用processInput方法处理输入,然后调用printStatus方法打印所有设备的状态。这个模拟器的目的是模拟智能家居中的电路设备,包括开关、调速器和各种电器,以及它们之间的连接和交互。用户可以通过输入特定的命令来控制这些设备,模拟器会根据这些命令更新设备的状态并输出结果。

继承自ControlDevicestate:表示开关的状态,0为打开,1为关闭。toggleState():切换开关状态。getOutputVoltage(double inputVoltage):如果开关打开,输出电压等于输入电压;如果关闭,输出电压为0。

 

踩坑心得:

PTA发布都会是在之前的基础上迭代的,会越来越难,这就导致我如果继续按照这样的写法就会很麻烦因为几乎每一道题都需要重新思考,最开始的正则表达式无法匹配,一直报错,最终对正则表达式进行修改才将正确有效信息录入,在代码测试和调试方面存在一定的不足。没有考虑到边界条件,导致函数在某些情况下无法正确运行.

改进建议:

针对一些涉及复杂计算或大量数据处理的题目,可以尝试使用更高效的算法和数据结构来提高程序的性能。提前开始最后一题的思考,有什么问题一定要和同学一起讨论.在编码过程中,应注重代码的规范性和可读性

总结:

基类定义:
CircuitDevice:电路设备的基类,定义了所有电路设备共有的属性(id、inputPin、outputPin)和方法(getOutputVoltage)。
ControlDevice:控制设备的基类,继承自CircuitDevice。
ControlledDevice:受控设备的基类,也继承自CircuitDevice。
具体类实现:
Switch:表示一个开关,具有打开和关闭的状态。
StepDimmer:分档调速器,可以设置不同的档位。
ContinuousDimmer:连续调速器,可以调节电压的连续变化。
IncandescentLight:白炽灯,亮度与输入电压相关。
FluorescentLight:日光灯,亮度固定为180 lux。
CeilingFan:吊扇,速度与输入电压相关。
电路连接管理:
CircuitConnection:负责管理所有电路设备的连接,包括添加设备、处理输入和输出。
addDevice(CircuitDevice device):向电路中添加新的设备。
processInput(String input):处理用户的输入命令,如开关状态、调速器档位等。
processConnection(String connection):处理设备之间的连接信息。
printStatus():打印所有设备的状态信息。
用户交互:
main 方法中创建了一个CircuitConnection实例和一个Scanner实例,用于读取用户输入。
在一个循环中读取用户输入,直到输入为"end"时退出循环。
调用processInput方法处理输入,然后调用printStatus方法打印所有设备的状态。
异常处理:
代码中包含了对输入字符串的处理,如解析命令和连接信息,以及可能的异常处理。
功能扩展:
代码提供了基础的设备管理和状态打印功能,可以在此基础上进一步扩展,例如添加更多类型的设备、实现更复杂的控制逻辑、添加用户界面等。
这个模拟器框架为智能家居系统的设计和实现提供了一个良好的起点,可以根据具体需求进行扩展和优化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/42649.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

【攻防技术系列+权限维持】①

这种方式我觉得挺好用的,且不需要管理员权限,我们都知道lnk文件可以指向一个exe文件,相当于一个快捷方式,所以我们可以更改指向的文件,指向我们的exe文件,但是这样的话原本的lnk文件就没用了,所以我们可以CreateShortcut方法来创建lnk快捷方式,在不损坏其原始lnk文件的…

第四到第六次大作业总结

一,前言 第四次大作业:第四次大作业,对我而言,并没有新增多少内容,这也给了我更多的时间去审视之前的结构与设计,我将我原先的结构重新写了一遍,做了许多优化,比如说又多提取出了两个类,尽量去实现单一职责原则,但是,在主函数当中,仍然用了一些循环,或许我可以将这…

第二次博客作业-答题判题程序4/家居强电电路模拟程序1-2 blog-2

一、前言: 在前三次面向对象程序设计题目集的基础上,我又完成了三次PTA题目集的作业,在此过程中我感受颇多。在解决前三次答题判题程序后,我逐渐掌握了这套程序的设计精髓,并在我第三次题目集的基础上做了扩展性的重构,以便于解决答题判题程序-4的相关问题。然而,在…

xxt

引子 前不久,我完成了第五次和第六次大作业。这次的作业主题是“家居强电电路模拟程序”,每次的作业都是在前一次作业的基础上进行迭代。此次作业训练了前段时间新学的继承与多态,并且巩固了之前学到的一些旧的知识。 作业总结从难度来看,这两次的大作业题都是由一种题目发…

【NAS】Docker Gitea+SakuraFrp+绿联DPX4800标 搭建私有代码托管平台

本文主要分享 Gitea的一些设置,和Https的实现。本文主要分享 Gitea的一些设置,和Https的实现。 Gitea的一些设置 映射网络HTTPS的实现 先准备好一个域名,建议准备一个1Panel创建一个AC账户然后点击申请证书,手动解析。 申请完毕后,点击详情,查看证书crt和私钥key 自己创建…

【转载】基于 Docker 的 PHP 集成环境 dnmp

参考https://github.com/yeszao/dnmp?tab=readme-ov-file https://learnku.com/articles/19289 https://www.awaimai.com/2120.html 源码 【下载】(由于限制20m上传,删除 .git 文件夹 )正文 介绍 PHP 环境搭建是个麻烦事,nginx、PHP、MySQL 一个不能少,有时候一个错误可能…

mac python 包管理工具 pip 的配置

python3 --version Python 3.12.3 brew install python@3.12 pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip3 config set global.break-system-packages truepip3 install aiohttp python 包管理工具 pip 的配置近几年来,python的包管理系统…

init_array与got劫持——[zer0pts 2020]easy strcmp

只是在顺思路,wp参考了2位大佬 文章列表 | NSSCTF [Zer0pts2020]easy strcmp 分析与加法-CSDN博客 题目Die 虚拟机运行一下 没有输入,直接报错退出了 IDA 很奇怪啊,就是一个比较 从我们运行直接报错来看,我们运行时a1>1这个条件是不成立的 我的最初思路就是调试把a1改了…