@[TOC](6-1 sdust-Java-模拟主板、USB口、TypeC口、PS2口设备 (30 分))
题目描述
使用Java编码设计实现模拟计算机硬件中的主板、USB口、TypeC口、PS2口及其组合方式。通常,计算机主板中提供了各种不同类型的对外接口,可以对接不同类型的设备。例如,可以接USB口的U盘、鼠标等,可以接口TypeC口的鼠标、键盘,可以接PS2口的键盘。要求:通过下面Main类中main方法给出的代码及其注释,设计相关的类、抽象类或接口(具体需要的类、接口或抽象类,根据程序样例给出的代码和注释来编写),使得main方法中的代码能够正确运行,且能够实现样例所示的输入输出功能。
Main类的main方法测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String mainBoardModel = scan.next();//用户输入主板的型号
String mousePS2Model = scan.next();//用户输入PS2口鼠标的型号
String keyBoardPS2Model = scan.next();//用户输入PS2口键盘的型号
String flashDiskModel = scan.next();//用户输入U盘的型号
MainBoard mainboard = new MainBoard(mainBoardModel);//生成主板对象
KeyBoardPS2 keyboard = new KeyBoardPS2(keyBoardPS2Model);//生成PS2口键盘对象
mainboard.setPs2(keyboard);//将键盘接入主板的PS2口
mainboard.inputFromPS2();//获取从键盘录入的数据(注意:是模拟从键盘录入数据,不需要编码利用Scanner接收数据)
mainboard.setPs2(null);//拔出PS2口键盘
MousePS2 mouse = new MousePS2(mousePS2Model);//生成PS2口的鼠标对象
mainboard.setPs2(mouse);//将鼠标接入主板的PS2口
mainboard.inputFromPS2();//获取从键盘录入的数据
//
FlashDisk flashdisk = new FlashDisk(flashDiskModel);//生成U盘对象
mainboard.setUsb(flashdisk);//在主板上插入U盘的USB口
mainboard.read();//从外接设备(USB或TypeC口设备上读取数据,只要接入相关设备,则从该设备读取数据)
mainboard.setUsb(null);//拔下U盘:flashdisk
mainboard.setTypeC(flashdisk);//将U盘:flashdisk插入主板的TypeC口(FlashDisk的对象同时支持USB和TypeC口)
mainboard.read();//从外接设备(USB或TypeC口设备上读取数据,只要接入相关设备,则从该设备读取数据)
MouseUSB mouseUSB = new MouseUSB();//生成USB鼠标
mainboard.setUsb(mouseUSB);//在主板上插入USB鼠标的USB口
scan.close();
}
}
/*
答案
*/
输入样例:
MicroStar
A4TECH
Logitech
Kingston
输出样例:
MicroStar MainBoard:Get data from Logitech PS2 keyboard
MicroStar MainBoard:Get data from A4TECH PS2 Mouse
MicroStar MainBoard:Read data through Kingston USB U盘
MicroStar MainBoard:Read data through Kingston TypeC U盘
参考答案1
interface PCI{
void start();
}
class MainBoard {
private String mainBoardModel;
private PCI o = null;
public MainBoard(String mainBoardModel) {
super();
this.mainBoardModel = mainBoardModel;
}
public void setPs2(PCI o) {
this.o = o;
}
public void setUsb(Object o) {
if(o instanceof FlashDisk)
{
((FlashDisk) o).usb = true;
}
this.o = (PCI)o;
}
public void setTypeC(Object o) {
if(o instanceof FlashDisk)
{
((FlashDisk) o).usb = false;
}
this.o = (PCI)o;
}
public void inputFromPS2() {
System.out.print(mainBoardModel + " MainBoard:");
o.start();
}
public void read() {
System.out.print(mainBoardModel + " MainBoard:");
o.start();
}
}
class KeyBoardPS2 implements PCI{
private String keyBoardPS2Model;
public KeyBoardPS2(String keyBoardPS2Model) {
super();
this.keyBoardPS2Model = keyBoardPS2Model;
}
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("Get data from " + keyBoardPS2Model + " PS2 keyboard");
}
}
class MousePS2 implements PCI{
private String mousePS2Model;
public MousePS2(String mousePS2Model) {
super();
this.mousePS2Model = mousePS2Model;
}
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("Get data from " + mousePS2Model + " PS2 Mouse");
}
}
class FlashDisk implements PCI{
private String flashDiskModel;
public boolean usb = false;
public FlashDisk(String flashDiskModel) {
super();
this.flashDiskModel = flashDiskModel;
}
@Override
public void start() {
// TODO Auto-generated method stub
if(usb) {
System.out.println("Read data through " + flashDiskModel + " USB U盘");
}
else {
System.out.println("Read data through " + flashDiskModel + " TypeC U盘");
}
}
}
class MouseUSB implements PCI{
@Override
public void start() {
// TODO Auto-generated method stub
}
}
参考答案2
interface PCI{
void start();
}
class MainBoard {
private String mainBoardModel;
private PCI o = null;
public MainBoard(String mainBoardModel) {
super();
this.mainBoardModel = mainBoardModel;
}
public void setPs2(PCI o) {
this.o = o;
}
public void setUsb(PCI o) {
if(o instanceof FlashDisk)
{
((FlashDisk) o).usb = true;
}
this.o = o;
}
public void setTypeC(PCI o) {
if(o instanceof FlashDisk)
{
((FlashDisk) o).usb = false;
}
this.o = o;
}
public void inputFromPS2() {
System.out.print(mainBoardModel + " MainBoard:");
o.start();
}
public void read() {
System.out.print(mainBoardModel + " MainBoard:");
o.start();
}
}
class KeyBoardPS2 implements PCI{
private String keyBoardPS2Model;
public KeyBoardPS2(String keyBoardPS2Model) {
super();
this.keyBoardPS2Model = keyBoardPS2Model;
}
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("Get data from " + keyBoardPS2Model + " PS2 keyboard");
}
}
class MousePS2 implements PCI{
private String mousePS2Model;
public MousePS2(String mousePS2Model) {
super();
this.mousePS2Model = mousePS2Model;
}
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("Get data from " + mousePS2Model + " PS2 Mouse");
}
}
class FlashDisk implements PCI{
private String flashDiskModel;
public boolean usb = false;
public FlashDisk(String flashDiskModel) {
super();
this.flashDiskModel = flashDiskModel;
}
@Override
public void start() {
// TODO Auto-generated method stub
if(usb) {
System.out.println("Read data through " + flashDiskModel + " USB U盘");
}
else {
System.out.println("Read data through " + flashDiskModel + " TypeC U盘");
}
}
}
class MouseUSB implements PCI{
@Override
public void start() {
// TODO Auto-generated method stub
}
}