For this exercise, you are going to 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.
Assistant
Given your requirements, here's a simple way to design the Computer, Laptop, and Desktop classes while adhering to the principles of inheritance. I'll also provide a simple Tester class as requested. Please note that the batteryLife attribute will only make sense in the context of a Laptop, and the monitor attribute will primarily concern a Desktop.
java
// Superclass Computer
public class Computer {
protected int screenSize;
protected int memory;
// Constructor
public Computer(int screenSize, int memory) {
this.screenSize = screenSize;
this.memory = memory;
}
// Getters and Setters
public int getScreenSize() {
return screenSize;
}
public void setScreenSize(int screenSize) {
this.screenSize = screenSize;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
}
// Subclass Laptop
public class Laptop extends Computer {
protected double batteryLife; // unique to laptops
// Constructor
public Laptop(int screenSize, int memory, double batteryLife) {
super(screenSize, memory);
this.batteryLife = batteryLife;
}
// Getter and Setter for batteryLife
public double getBatteryLife() {
return batteryLife;
}
public void setBatteryLife(double batteryLife) {
this.batteryLife = batteryLife;
}
}
// Subclass Desktop
public class Desktop extends Computer {
protected boolean monitor; // unique to desktops
// Constructor
public Desktop(int screenSize, int memory, boolean monitor) {
super(screenSize, memory);
this.monitor = monitor;
}
// Getter and Setter for monitor
public boolean hasMonitor() {
return monitor;
}
public void setMonitor(boolean monitor) {
this.monitor = monitor;
}
}
// Tester Class
public class Tester {
public static void main(String[] args) {
Laptop laptop = new Laptop(15, 16, 10);
Desktop desktop = new Desktop(27, 32, true);
// Testing Laptop
System.out.println("Laptop Screen Size: " + laptop.getScreenSize() + " inches");
System.out.println("Laptop Memory: " + laptop.getMemory() + " GB");
System.out.println("Laptop Battery Life: " + laptop.getBatteryLife() + " hours");
// Testing Desktop
System.out.println("Desktop Screen Size: " + desktop.getScreenSize() + " inches");
System.out.println("Desktop Memory: " + desktop.getMemory() + " GB");
System.out.println("Desktop Includes Monitor: " + desktop.hasMonitor());
}
}
In this design, both Laptop and Desktop inherit from Computer, reusing the screenSize and memory attributes and adding their unique properties (batteryLife for Laptop and monitor for Desktop). The Tester class demonstrates the creation of Laptop and Desktop objects and accesses their properties through the provided getters.