198k views
4 votes
Design 3 classes: Computer - Superclass

Laptop - Subclass
Desktop - Subclass

You will design these classes to optimize the superclass/subclass relationship by creating instance variables and getter/setter methods.
Include the following instance variables:
int screenSize -
Inches of monitor space
int memory - GB of ram
double batteryLife - Hours of battery life
boolean monitor - Whether or not a monitor is included

Each class should have at least one variable in it.
Once completed, the Tester class should execute without error.

User Tomahim
by
4.0k points

1 Answer

2 votes

Answer:

Step-by-step 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();

}

}

Design 3 classes: Computer - Superclass Laptop - Subclass Desktop - Subclass You will-example-1
User Monika
by
4.5k points