首页 > 其他分享 >第三次Blog

第三次Blog

时间:2024-06-30 21:43:17浏览次数:16  
标签:return String double resistance name Blog id 第三次

abstract class Device {
String id;
double resistance;
Device(String id) {
this.id = id;
}
abstract String getStatus();
abstract double getResistance();
}
class Switch extends Device {
boolean state; // false代表turned on,true代表closed
Switch(String id) {
super(id);
this.state = false;
this.resistance = 0;
}
//切换开关
void toggle() {
this.state = !this.state;
}
@Override
String getStatus() {
return state ? "closed" : "turned on";
}
@Override
double getResistance(){
return this.resistance;
}
}
// 分档调速器类
class SteppedRegulator extends Device {
int currentLevel = 0;
int maxLevel = 3;
double[] outputVoltageRatios = {0.0, 0.3, 0.6, 0.9};
SteppedRegulator(String id) {
super(id);
this.resistance = 0;
}
void increase() {
if (currentLevel < maxLevel) {
currentLevel++;
}
}
void decrease() {
if (currentLevel > 0) {
currentLevel--;
}
}
double getOutputVoltage() {
return outputVoltageRatios[currentLevel];
}
@Override
String getStatus() {
return String.valueOf(currentLevel);
}
@Override
double getResistance(){
return this.resistance;
}
}
// 连续调速器类
class ContinuousRegulator extends Device {
double level = 0.00;
ContinuousRegulator(String id) {
super(id);
this.resistance = 0;
}
void setLevel(double level) {
this.level = Math.floor(level * 100) / 100; // 保留两位小数
}
@Override
String getStatus() {
return String.format("%.2f", level);
}
@Override
double getResistance(){
return this.resistance;
}
}
// 灯类
class Lamp extends Device {
int brightness = 0;

Lamp(String id) {
    super(id);
    if (id.startsWith("B")) {
        this.resistance = 10;
    }else if (id.startsWith("R")) {
        this.resistance = 5;
    }
}
void setBrightness(double voltage) {
    if (id.startsWith("B")) {
        // 白炽灯
        if (voltage <= 9) {
            brightness = 0;
        } else {
            brightness = (int) ((15.0 / 21.0) * (voltage - 10) + 50);
        }
    } else if (id.startsWith("R")) {
        // 日光灯
        brightness = voltage > 0 ? 180 : 0;
    }
}
@Override
String getStatus() {
    return String.valueOf(brightness);
}
@Override
double getResistance(){
    return this.resistance;
}

}

// 风扇类
class Fan extends Device {
int speed = 0;
Fan(String id) {
super(id);
this.resistance = 20;
}
void setSpeed(double voltage) {
if (voltage < 80) {
speed = 0;
} else if (id.startsWith("D")) {
// 吊扇
if (voltage >= 150) {
speed = 360;
} else {
speed = (int) ((double) (voltage - 80) / 70 * 280 + 80);
}
} else if (id.startsWith("A")) {
// 落地扇
if (voltage >= 140) {
speed = 360;
} else if (voltage >= 120) {
speed = 260;
} else if (voltage >= 100) {
speed = 160;
} else {
speed = 80;
}
}
}
@Override
String getStatus() {
return String.valueOf(speed);
}
@Override
double getResistance(){
return this.resistance;
}
}
class SelectorSwitch extends Device {
boolean state;
private static final TreeMap<String, SelectorSwitch> H = new TreeMap<>();
SelectorSwitch(String id, String Pin) {
super(id);
if (Pin.equals("2")){
this.resistance = 5;
this.state = true;
H.put(id, this);
}
if (Pin.equals("3")){
this.resistance = 10;
this.state = false;
}
}
public static TreeMap<String, SelectorSwitch> getH() {
return H;
}
void toggle() {
this.state = !this.state;
}
@Override
String getStatus() {
return state ? "closed" : "turned on";
}
@Override
double getResistance(){
return this.resistance;
}
}
//
class CurtainController extends Device{
int openPercentage = 100;
double voltage;
public void setVoltage(double voltage1) {
voltage = voltage1;
}
public double getVoltage() {
return voltage;
}
CurtainController(String id) {
super(id);
this.resistance = 15;
}
void setOpenPercentage(double voltage, double lightIntensity) {
if (voltage < 50) {
openPercentage = 100; // 电压低于50V,窗帘不工作,全开状态
} else {
if (lightIntensity < 50) {
openPercentage = 100;
} else if (lightIntensity < 100) {
openPercentage = 80;
} else if (lightIntensity < 200) {
openPercentage = 60;
} else if (lightIntensity < 300) {
openPercentage = 40;
} else if (lightIntensity < 400) {
openPercentage = 20;
} else {
openPercentage = 0;
}
}
}

@Override
String getStatus() {
    return openPercentage + "%";
}
@Override
double getResistance(){
    return this.resistance;
}

}
//串联类
class Concatenation extends Device{
final Pattern pattern_C = Pattern.compile("#(T\d+)\:\[(IN|VCC) [A-Z]\d+-\d\]( \[[A-Z]\d+-(\d|OUT) [A-Z]\d+-(\d|IN)\])+ \[[A-Z]\d+-(\d|OUT) (OUT|GND)\]");
private final LinkedHashMap<String, Device> concatenationLinkedHashMap;
private static final TreeMap<String, Switch> K = new TreeMap<>();
private static final TreeMap<String, SteppedRegulator> F = new TreeMap<>();
private static final TreeMap<String, ContinuousRegulator> L = new TreeMap<>();
private static final TreeMap<String, Lamp> B_R = new TreeMap<>();
private static final TreeMap<String, Fan> D_A = new TreeMap<>();
private static final TreeMap<String, CurtainController> S = new TreeMap<>();
double voltage;
public void setVoltage(double voltage1) {
voltage = voltage1;
}
public double getVoltage() {
return voltage;
}
Concatenation(String id) {
super(id);
concatenationLinkedHashMap = new LinkedHashMap<>();
}
public LinkedHashMap<String, Device> getConcatenationLinkedHashMap(){
return concatenationLinkedHashMap;
}
public TreeMap<String, Switch> getK(){
return K;
}
public TreeMap<String, SteppedRegulator> getF(){
return F;
}
public TreeMap<String, ContinuousRegulator> getL(){
return L;
}
public TreeMap<String, Lamp> getB_R(){
return B_R;
}
public TreeMap<String, Fan> getD_A(){
return D_A;
}
public TreeMap<String, CurtainController> getS(){
return S;
}

public LinkedHashMap<String, Device> processLines(String T, Concatenation concatenation, LinkedHashMap<String, Concatenation> ConcatenationMap, LinkedHashMap<String, Parallel> ParallelLinkedHashMap) {
    Matcher matcher_C = pattern_C.matcher(T);
    if(matcher_C.find()){
        Pattern pattern_T = Pattern.compile("\\[(.*?) (.*?)\\]");
        Matcher matcher_T = pattern_T.matcher(T);
        while (matcher_T.find()) {
            String[] parts =  matcher_T.group(2).split("-");
            String[] parts1 = matcher_T.group(1).split("-");
            String name =parts[0];
            String name1 = parts1[0];
            String Pin = "";
            String Pin1 = "";
            if (parts.length > 1 && parts[1] != null && !parts[1].isEmpty()) {
                Pin = parts[1];
            }

// 判断并赋值给 Pin1
if (parts1.length > 1 && parts1[1] != null && !parts1[1].isEmpty()) {
Pin1 = parts1[1];
}
if(name1.startsWith("H")) {
if(Pin1.equals("2") || Pin1.equals("3")){
SelectorSwitch selectorSwitch = new SelectorSwitch(name1,Pin1);
concatenation.concatenationLinkedHashMap.put(name1, selectorSwitch);
}
}
if(name.startsWith("K")) {
Switch mySwitch = new Switch(name);
concatenation.concatenationLinkedHashMap.put(name, mySwitch);
K.put(name, mySwitch);
}else if (name.startsWith("F")) {
SteppedRegulator steppedRegulator = new SteppedRegulator(name);
concatenation.concatenationLinkedHashMap.put(name, steppedRegulator);
F.put(name, steppedRegulator);
}else if (name.startsWith("L")) {
ContinuousRegulator continuousRegulator = new ContinuousRegulator(name);
concatenation.concatenationLinkedHashMap.put(name, continuousRegulator);
L.put(name, continuousRegulator);
}else if (name.startsWith("B")) {
Lamp lamp = new Lamp(name);
concatenation.concatenationLinkedHashMap.put(name, lamp);
B_R.put(name, lamp);
}else if (name.startsWith("R")) {
Lamp lamp = new Lamp(name);
concatenation.concatenationLinkedHashMap.put(name, lamp);
B_R.put(name, lamp);
}else if (name.startsWith("D")) {
Fan fan = new Fan(name);
concatenation.concatenationLinkedHashMap.put(name, fan);
D_A.put(name, fan);
}else if (name.startsWith("A")) {
Fan fan = new Fan(name);
concatenation.concatenationLinkedHashMap.put(name, fan);
D_A.put(name, fan);
}else if(name.startsWith("H")) {
if(Pin.equals("2") || Pin.equals("3")){
SelectorSwitch selectorSwitch = new SelectorSwitch(name,Pin);
concatenation.concatenationLinkedHashMap.put(name, selectorSwitch);
}

            }else if(name.startsWith("S")) {
                CurtainController curtainController = new CurtainController(name);
                concatenation.concatenationLinkedHashMap.put(name, curtainController);
                S.put(name, curtainController);
            }else if (name.startsWith("M")) {
                concatenation.concatenationLinkedHashMap.put(name, ParallelLinkedHashMap.get(name));
            }else if (name.startsWith("T")) {
                concatenation.concatenationLinkedHashMap.put(name, ConcatenationMap.get(name));
            }
        }
        this.resistance = ResistanceCalculator(concatenation);
    }
    return concatenationLinkedHashMap;
}
public double ResistanceCalculator(Concatenation concatenation){
    for (Map.Entry<String, Device> entry : concatenation.concatenationLinkedHashMap.entrySet()) {
        String deviceId = entry.getKey();
        Device device = entry.getValue();
        resistance += device.resistance;
    }
    return resistance;
}
public void setResistance(double resistance){
    this.resistance = resistance;
}
@Override
String getStatus() {
    return null;
}

@Override
double getResistance() {
    return 0;
}

}
//并联类
class Parallel extends Device{
final Pattern pattern_P = Pattern.compile("#M\d+\:\[(T\d+)( T\d+)+\]");
private final LinkedHashMap<String, Concatenation> parallelLinkedHashMap;

Parallel(String id) {
    super(id);
    parallelLinkedHashMap = new LinkedHashMap<>();
}
public LinkedHashMap<String, Concatenation> getParallelLinkedHashMap() {
    return parallelLinkedHashMap;
}
public void setResistance(double resistance){
    this.resistance = resistance;
}
public double processLines(String M, LinkedHashMap<String, LinkedHashMap<String, Device>> deviceMap, LinkedHashMap<String, Concatenation> ConcatenationMap) {
    double resistance = 0;
    Matcher matcher_P = pattern_P.matcher(M);
    if (matcher_P.find()){
        Pattern pattern_M = Pattern.compile("T\\d+");
        Matcher matcher_M = pattern_M.matcher(M);
        while (matcher_M.find()) {
            double resistance1 = 0;
            if (deviceMap.containsKey(matcher_M.group())) {
                parallelLinkedHashMap.put(matcher_M.group(), ConcatenationMap.get(matcher_M.group()));
                LinkedHashMap<String, Device> innerMap = deviceMap.get(matcher_M.group());
                for (Map.Entry<String, Device> entry : innerMap.entrySet()) {
                    String innerKey = entry.getKey();
                    Device device = entry.getValue();
                    resistance1 += device.getResistance();
                    // 在这里使用innerKey和device进行处理
                }
                resistance += 1 / resistance1;
            }
        }
        resistance = 1 / resistance;
    }
    return resistance;
}

@Override
String getStatus() {
    return null;
}
@Override
double getResistance(){
    return resistance;
}

}

标签:return,String,double,resistance,name,Blog,id,第三次
From: https://www.cnblogs.com/yushang257368419/p/18277005

相关文章

  • 第三次blog
    前言:第7次作业和第8次作业设计到的知识点,如下类和对象:需要设计各种设备的类,如开关、调速器、灯、风扇等,并为每个设备创建对象以便进行操作和状态记录。数据类型和运算:使用double类型来保存电压、转速等数值,进行计算时需要注意不进行类型转换,最终输出时按截尾规则保留整数。数......
  • 第三次BLOG
    转眼已经到了学期末,面向对象课程也迎来结课,这是oop第三次作业的大总结,题目集的难度都是由浅入深,循序渐进的,越往后写越是有点力不从心的感觉。但是这门课是我们接触软件的开始,并不是结束,这门课结束了不代表学习就要结束,所以在结课之后,对于最后两次题目集做一次总结,以便巩固自己所学......
  • 第三次Blog
    一,前言本次Blog是对题目集7-8的总结,正则表达式,类型转换,字符串处理,链表,以及例如Arraylist等包的知识,需要合理运用继承多态来设计类,需要对题目的逻辑过程,具体实现有充分的认知.特别是用map将类之间联系起来对于逻辑的考验非常大.难度很大,在写代码之前要对实现的过程,思......
  • 第三次Blog
    7-1家居强电电路模拟程序-3分数100作者蔡轲单位南昌航空大学智能家居是在当下家庭中越来越流行的一种配置方案,它通过物联网技术将家中的各种设备(如音视频设备、照明系统、窗帘控制、空调控制、安防系统、数字影院系统、影音服务器、影柜系统、网络家电等)连接到一起,提供家......
  • 第三次学习总结
    Java,作为一门广泛应用的编程语言,已经深深地影响了软件开发领域。随着互联网技术的飞速发展,Java在服务器端、移动端、嵌入式系统等领域都发挥着重要的作用。本文将从Java的基本概念、特性、核心技术以及实际应用等方面进行总结,以期帮助初学者更好地掌握Java编程技能。Java是一种面......
  • 第三次blog
    一.前言1.题目集7~8的知识点:(1)类与对象:各个电路设备类型被建模为类,每个类封装了设备的状态和行为。(2)继承与多态:使用继承来建立设备类之间的关系,并通过方法重写实现多态性,确保同一方法针对不同类型的设备表现出不同行为。(3)接口与排序:接口被用于定义设备类的排序规则,确保设备对象......
  • 23201826-熊锋-第三次blog
    一.前言这两次pta作业第一次为家居强电电路模拟程序-3,这次题型,是在家居强电电路模拟程序-2的基础上进行迭代,虽然仅仅只增加了互斥开关等,但带来的改变同上一次十分巨大,通路的判断不再仅仅局限于各个控制设备0与1,而是要判断多个控制设备的开闭,其位置乃至互斥开关的实际情况,使得并联......
  • 第三次blog
    1.前言一、知识点关于类1:类似于C语言的结构体,但比C语言中的结构体作用更多,且适用范围更广2:懂得封装和公开的区别,即private和public的区别,用private可以防止数据的泄露3:用类把题目分解,以便更好的解决题目,创立多个类,来分解题目。4:在Java开发中,类是实现功能的基础。通过定义类,......
  • 第三次Blog
    (1) 前言:第七次大作业与第八次大作业知识点要是继承与多态并且对类方法的运用,题量比较大,难度也是不断上升。第七次大作业在第六次大作业基础上增加了受控窗帘这一受控设备与互斥开关这一控制开关,并考虑了一条总电路上有多个并联电路,输入信息也变得与第六次大作业不一样,但输出信息大......
  • 23201115-邓俊豪-第三次blog
    目录blog2前言关于难度和题目量关于知识点设计与分析pta-7一、项目简介二、项目实现三、项目测试四、代码示例五、总结六、代码分析pta-8一、项目简介二、项目实现三、项目测试四、代码示例五、总结六、代码分析改进建议blog2前言关于难度和题目量前三次大作业难度属于偏难水......