Electronic Device
Submit solution
Points:
50
Time limit:
1.0s
Memory limit:
256M
Authors:
Problem types
Allowed languages
Java 19, Java 8
題目說明
在ElectronicDevice類別中新增抽象方法turnOn()、turnOff()
覆寫Laptop和Smartphone類別中的turnOn()、turnOff()方法,分別輸出以下資訊:
Laptop:
// turnOn
"Laptop is turning on."
"Laptop is activating its hard drive."
"Laptop is initializing its keyboard."
"Laptop is connecting to the network via an Ethernet cable."
// turnOff
"Laptop is turning off."
"Laptop is shutting down its hard drive."
"Laptop is disconnecting from the Ethernet cable."
Smartphone:
// turnOn
"Smartphone is turning on."
"Smartphone is using its flash storage."
"Smartphone is enabling its virtual keyboard."
"Smartphone is connecting to the network via Wi-Fi."
// turnOff
"Smartphone is turning off."
"Smartphone is disabling its virtual keyboard."
"Smartphone is disconnecting from Wi-Fi."
使用以下程式碼作答:
// ElectronicDevice.java
abstract class ElectronicDevice {
// 新增抽象方法
abstract void turnOn();
abstract void turnOff();
// 非抽象方法
void checkBatteryStatus() {
System.out.println("Checking battery status...");
}
}
// Laptop.java
class Laptop extends ElectronicDevice {
// 覆寫turnOn()、turnOff()
@Override
/*
作答在此
*/
}
// Smartphone.java
class Smartphone extends ElectronicDevice {
// 覆寫turnOn()、turnOff()
@Override
/*
作答在此
*/
}
// MyProgram.java
public class MyProgram {
public static void main(String[] args) {
ElectronicDevice device1 = new Laptop();
ElectronicDevice device2 = new Smartphone();
ElectronicDevice[] devices = {device1, device2};
for (ElectronicDevice device : devices) {
device.turnOn();
device.checkBatteryStatus();
System.out.println();
}
}
}
輸入
這題不用輸入
輸出
依主程式執行輸出
測試資料輸入
這題不用輸入
測試資料輸出
Laptop is turning on.
Laptop is activating its hard drive.
Laptop is initializing its keyboard.
Laptop is connecting to the network via an Ethernet cable.
Checking battery status...
Smartphone is turning on.
Smartphone is using its flash storage.
Smartphone is enabling its virtual keyboard.
Smartphone is connecting to the network via Wi-Fi.
Checking battery status...
Comments