Answer:
Explanation:
The following code is written in Java. It creates the three classes mentioned and a Tester class that contains the main method. The Computer class contains the memory variable since both laptops and Desktops need memory. The screen size variable is placed separately in the Laptop and Desktop class since desktops may or may not have a monitor so this variable cannot be placed in the Computer class. The batteryLife variable is only in the Laptop class because Desktops do not have batteries. Finally, the monitor variable is only placed in the Desktop class since Laptop's come with built-in monitors. The tester class seen in the picture below tests the creation of both of these objects and it executes without any error.
class Computer {
int memory;
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
}
class Laptop extends Computer {
int screenSize;
double batteryLife;
public int getScreenSize() {
return screenSize;
}
public void setScreenSize(int screenSize) {
this.screenSize = screenSize;
}
public double getBatteryLife() {
return batteryLife;
}
public void setBatteryLife(double batteryLife) {
this.batteryLife = batteryLife;
}
}
class Desktop extends Computer {
boolean monitor;
int screenSize;
public boolean isMonitor() {
return monitor;
}
public void setMonitor(boolean monitor) {
this.monitor = monitor;
}
public int getScreenSize() {
return screenSize;
}
public void setScreenSize(int screenSize) {
this.screenSize = screenSize;
}
}
class Tester {
public static void main(String[] args) {
Laptop computer1 = new Laptop();
Desktop computer2 = new Desktop();
}
}