在计算机主机(Mainframe)中,只需要按下主机的开机按钮(on()),即可调用其他硬件设备和软件的启动方法 ,如内存(Memory)的自检(check())、CPU的运行(run())、硬盘(HardDisk)的读取(read())、操作系统(OS)的载入(load()),如果某一过程发生错误则计算机启动失败。
实验要求:
-
提交类图;
-
提交源代码;
// File: Client.java
package waiguan;public class Client {public static void main(String[] args) {Mainframe mainframe = new Mainframe();mainframe.on();}
}// File: Mainframe.java
package waiguan;public class Mainframe {private boolean operIf = true;private Memory memory;private CPU cpu;private HardDisk hardDisk;private OS os;public Mainframe() {memory = Memory.getInstance();cpu = CPU.getInstance();hardDisk = HardDisk.getInstance();os = OS.getInstance();}public void setOperIf(boolean operIf) {this.operIf = operIf;}public void on() {// 按顺序检查各个组件if (memory.check(operIf) && cpu.run(operIf) && hardDisk.read(operIf) && os.load(operIf)) {System.out.println("计算机启动成功!");} else {System.out.println("计算机启动失败!");}}
}// File: Memory.java
package waiguan;public class Memory {private static Memory instance = new Memory();private Memory() {}public static Memory getInstance() {return instance;}public boolean check(boolean operIf) {if (operIf) {System.out.println("内存自检成功!");return true;} else {System.out.println("内存自检失败!");return false;}}
}// File: CPU.java
package waiguan;public class CPU {private static CPU instance = new CPU();private CPU() {}public static CPU getInstance() {return instance;}public boolean run(boolean operIf) {if (operIf) {System.out.println("CPU运行成功!");return true;} else {System.out.println("CPU运行失败!");return false;}}
}// File: HardDisk.java
package waiguan;public class HardDisk {private static HardDisk instance = new HardDisk();private HardDisk() {}public static HardDisk getInstance() {return instance;}public boolean read(boolean operIf) {if (operIf) {System.out.println("硬盘读取成功!");return true;} else {System.out.println("硬盘读取失败!");return false;}}
}// File: OS.java
package waiguan;public class OS {private static OS instance = new OS();private OS() {}public static OS getInstance() {return instance;}public boolean load(boolean operIf) {if (operIf) {System.out.println("操作系统加载成功!");return true;} else {System.out.println("操作系统加载失败!");return false;}}
}
3.注意编程规范。