首页 > 其他分享 >课程阶段性总结

课程阶段性总结

时间:2024-06-09 23:34:57浏览次数:12  
标签:总结 阶段性 int double public 课程 device inputVoltage id

前言:

学习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方法打印所有设备的状态。
异常处理:
代码中包含了对输入字符串的处理,如解析命令和连接信息,以及可能的异常处理。
功能扩展:
代码提供了基础的设备管理和状态打印功能,可以在此基础上进一步扩展,例如添加更多类型的设备、实现更复杂的控制逻辑、添加用户界面等。
这个模拟器框架为智能家居系统的设计和实现提供了一个良好的起点,可以根据具体需求进行扩展和优化。

标签:总结,阶段性,int,double,public,课程,device,inputVoltage,id
From: https://www.cnblogs.com/yjr011/p/18239974

相关文章

  • MyBatisPlus总结二
    MybatisPlus总结一在这:MybatisPlus总结1/2-CSDN博客六、分页查询:6.1.介绍:        MybatisPlus内置了分页插件,所以我们只需要配置一个分页拦截器就可以了,由于不同的数据库的分页的方式不一样,例如mysql和oracle数据库的写法是完全不一样的,所以我们需要去指定一个数......
  • HTML和CSS每周总结6.7day
    最近学的东西比较简单就没每天发了,下面我总结一下这周学的东西,最近端午节了祝大家端午节快乐。一,5.311.标签字体加粗:<b></b>   字体倾斜:<i></i>   下划线:<u></u>   删除线:<s></s>title网页标题 段落标签:<p></p> 换行标签:<br/> 字体标签:<fontcolor="......
  • 2024-6-9 面试总结
    1.上来先拷打项目,关于项目是怎么实现的,以及判题模块代码的编译和进行等等.2.询问过使用什么软件了,完成了什么东西等等.3.演示了一下python模型实验,关于金融知识图谱.4.根据我项目前端用到的MarkDown编辑器,扩展给我们讲解了一下关于医院病历前端的编辑器5.讲解了X86ARM6......
  • 对4-6次pta的总结
    【1】.前言这三次的pta难度还是比较高的,尤其是第四次的(根本没有思绪),考察了较多的正则表达式的使用,以及对类的设计有一些要求,第五次的pta难度一般,主要考察的是类的书写,并不需要太多关于类的设计,第六次的pta是在第五次的基础上进行迭代,增加了并联电路,难度还是有的。【2】.设计与......
  • 4~6题目集总结
    1.前言:知识点方面,涵盖了抽象类的定义、特点、作用,以及迭代相关的各种概念,如不同迭代器的使用等。题量适中,能够充分检验对这些知识点的理解和掌握程度。既包括对抽象类基本概念的直接考查,也有通过实际代码情境来分析的题目,还有涉及到与迭代结合运用的综合题。难度呈阶梯式分布。......
  • PTA第四次到第六次题目集总结
    PTA第四次到第六次题目集总结前言 第四次到第六次题目集的题目难度总的来说其实比第一次到第三次题目集的难度要稍小一点,因为第四次题目集总的来说就是在第三次题目集上做了一点拓展,增加了选择题和填空题两种题型,而第五次题目集开始就是一种新的背景的题目,以电路为背景,由于是理......
  • 4~6总结blog
    第四次:一.介绍:相比于前三次,这一次也有一定的迭代,但是仍然有五个测试点未通过,接下来重点展示二.类图:三.耦合度:在这个耦合度方面,Answer类的OCavg:4.36,Main类的OCavg:40.00Answer类的WMC:48,Main类的WMC:40在软件工程中,OCavg和WMC是两个与代码复杂性相关的度量指标。OCavg表示每......
  • PTA4-6题目集总结
    一.前言这几次题目集重点考察的知识点是继承与多态以及抽象类与接口以及对前面所学的知识的一些应用等。与之前三个题目集相类似,这三次题目集也是将分值大部分给与了第一题,甚至是全部的分值都在第一题中,伴随的两道小题都是考察的基本功,难度普遍简单,而第一题与之前类似,都是迭代类......
  • 高考假集训总结(6.9)
    6.9今天依然是单调队列优化dp和斜率优化dp(只不过斜率优化的题还没开始做,具体原因下面讲)突然发现自己学得越多,忘得越多,都想不起来单调队列怎么用了,于是又花一上午跑回去看了单调队列的题并调了一上午的t1暴力做法,现在终于可以将两者融会贯通也就是成功实现了单调队列优化dp不......
  • 第二次pta总结
    设计实现答题程序,模拟一个小型的测试,要求输入题目信息、试卷信息、答题信息、学生信息、删除题目信息,根据输入题目信息中的标准答案判断答题的结果。本题在答题判题程序-3基础上新增的内容统一附加在输出格式说明之后,用粗体标明。输入格式: 程序输入信息分五种,信息可能会打乱顺......