Memory 内存类,包含以下属性 type :类型,例如”ddr”\”ddr2”\”ddr4”等 speed:速度,例如1333\1600等 voltage:电压,例如”高电压”\”低电压” spec:规格,每条的容量,例如4\8\16 Cpu 处理器类,包含以下属性 cores:核心数,如1\2\4等 speed:速度,如2.5\3.5\4.0等 socket:插槽类型,例如”am3”\”fm1”\”lga1155”等 Mainboard主板类,包含以下属性 socketType:cpu插槽类型,例如”am3”\”fm1”\”lga1155”等 memoryType :内存类型,例如”ddr”\”ddr2”\”ddr4”等 memoryCount:最大支持内存条数,如2,4,8等 memoryCapacity:最大支持内存容量,如2,4,8,32等 Computer电脑类,包含以下属性: 一块主板 Mainboard mainboard。 一组内存:Memory [] memories 一个处理器:Cpu cpu; Factory 组装工厂类,包含一个方法 public static Computer makeComputer(Mainboard mainboard,Memory [] memories,Cpu cpu){} 这个方法返回一个Computer实例,组成computer的配件都在圆括号中。 注意:Cpu插槽和主板插槽类型不匹配返回null 内存type和主板内存类型不匹配返回null 单条内存容量大于主板单条内存最大容量返回null(主板单条内存最大容量=最大支持内存容量/最大支持内存条数) 若给定内存条数量大于主板支持内存条数,则以主板支持为准。 public static String showDetail(Computer computer){} 这个方法返回该电脑的配置单,需要显示cpu的全部属性,内存总量,内存条数量,内存类型,内存类型,格式可以自定义 。这道题该怎么写
时间: 2023-05-28 15:02:22 浏览: 128
public class Memory {
private String type;
private int speed;
private String voltage;
private int spec;
public Memory(String type, int speed, String voltage, int spec) {
this.type = type;
this.speed = speed;
this.voltage = voltage;
this.spec = spec;
}
// getters and setters
}
public class Cpu {
private int cores;
private double speed;
private String socket;
public Cpu(int cores, double speed, String socket) {
this.cores = cores;
this.speed = speed;
this.socket = socket;
}
// getters and setters
}
public class Mainboard {
private String socketType;
private String memoryType;
private int memoryCount;
private int memoryCapacity;
public Mainboard(String socketType, String memoryType, int memoryCount, int memoryCapacity) {
this.socketType = socketType;
this.memoryType = memoryType;
this.memoryCount = memoryCount;
this.memoryCapacity = memoryCapacity;
}
// getters and setters
}
public class Computer {
private Mainboard mainboard;
private Memory[] memories;
private Cpu cpu;
public Computer(Mainboard mainboard, Memory[] memories, Cpu cpu) {
this.mainboard = mainboard;
this.memories = memories;
this.cpu = cpu;
}
// getters and setters
}
public class Factory {
public static Computer makeComputer(Mainboard mainboard, Memory[] memories, Cpu cpu) {
if (!cpu.getSocket().equals(mainboard.getSocketType()) || !memories[0].getType().equals(mainboard.getMemoryType())) {
return null;
}
int maxMemorySpec = mainboard.getMemoryCapacity() / mainboard.getMemoryCount();
int actualMemoryCount = Math.min(mainboard.getMemoryCount(), memories.length);
for (int i = 0; i < actualMemoryCount; i++) {
if (memories[i].getSpec() > maxMemorySpec) {
return null;
}
}
Memory[] actualMemories = Arrays.copyOfRange(memories, 0, actualMemoryCount);
return new Computer(mainboard, actualMemories, cpu);
}
public static String showDetail(Computer computer) {
StringBuilder sb = new StringBuilder();
sb.append("CPU: ").append("Cores: ").append(computer.getCpu().getCores())
.append(" Speed: ").append(computer.getCpu().getSpeed())
.append(" Socket: ").append(computer.getCpu().getSocket()).append("\n");
int totalMemory = 0;
for (Memory memory : computer.getMemories()) {
totalMemory += memory.getSpec();
}
sb.append("Total Memory: ").append(totalMemory).append("\n")
.append("Memory Count: ").append(computer.getMemories().length).append("\n")
.append("Memory Type: ").append(computer.getMemories()[0].getType()).append("\n");
return sb.toString();
}
}
阅读全文