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.
public class Computer
{
private int screenSize;
private int memory;
code
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;
}
} public class ComputerTester
{
public static void main(String[] args)
{
Laptop macBook = new Laptop();
macBook.setBatteryLife(8.5);
macBook.setMemory(4);
code
Desktop dell = new Desktop();
dell.setScreenSize(18);
Computer surface = new Computer();
surface.setScreenSize(11);
surface.setMemory(8);
System.out.println(macBook.getBatteryLife());
System.out.println(macBook.getMemory());
System.out.println(dell.getScreenSize());
System.out.println(surface.getScreenSize());
System.out.println(surface.getMemory());
}
}public class Desktop
{
}public class Laptop
{
}
Assistant
To fulfill the requirements, we need to correctly implement the Laptop and Desktop classes as subclasses of the Computer class. Each subclass will have its exclusive instance variable(s) along with getters and setters for those variables. Here's how you can design these classes:
java
// Superclass Computer
public class Computer {
private int screenSize;
private int memory;
// Constructor
public Computer() {}
// 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 {
private double batteryLife; // Exclusive variable for Laptop
// Constructor
public Laptop() {}
// 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 {
private boolean monitor; // Exclusive variable for Desktop
// Constructor
public Desktop() {}
// Getter and setter for monitor
public boolean isMonitorIncluded() {
return monitor;
}
public void setMonitor(boolean monitor) {
this.monitor = monitor;
}
}
// Tester class to execute without error
public class ComputerTester {
public static void main(String[] args) {
Laptop macBook = new Laptop();
macBook.setBatteryLife(8.5);
macBook.setMemory(4);
Desktop dell = new Desktop();
dell.setScreenSize(18);
dell.setMonitor(true); // Assuming you want to set this property for testing
Computer surface = new Computer();
surface.setScreenSize(11);
surface.setMemory(8);
System.out.println(macBook.getBatteryLife());
System.out.println(macBook.getMemory());
System.out.println(dell.getScreenSize());
System.out.println(dell.isMonitorIncluded()); // Additional print to show monitor's status
System.out.println(surface.getScreenSize());
System.out.println(surface.getMemory());
// The added print statement for dell.isMonitorIncluded() ensures completeness of testing for all added features.
}
}
In this implementation, the Computer class is the superclass with two common properties: screenSize and memory.
The Laptop class extends Computer and adds a specific property batteryLife.
The Desktop class also extends Computer and adds its specific property monitor to indicate whether a monitor is included.
Each class has its respective constructors, which are default constructors in this case. Additionally, we included getters and setters for all properties of the classes.
The ComputerTester class then creates instances of Laptop, Desktop, and Computer and sets their properties via the setter methods. It finally prints out the values of these properties to demonstrate the functionality of our classes.
This design leverages inheritance by having Laptop and Desktop classes inherit common properties from Computer, while each adding their unique attribute, adhering to the principles of objected-oriented programming.