In: Computer Science
If you've ever opened your computer's device manager, you've likely seen a whole list of devices, ranging from the obvious (keyboards, printers, monitors) to the internal (processors, memory, host controllers), to the subtle (network hosts, laptop lid closing, internal clock). While these devices can fall in a number of categories (input devices, graphics devices, audio devices, etc), they all share some features in common (they all have a name, a device driver, etc). In this section, we will design a very rudimentary implementation of a generic device driver. We are only interested in three things for the time being: the name of the device, an ID number identifying the device, and a flag indicating whether or not it is enabled. Thus, three fields are necessary, a String for the name, a int for the ID, and a boolean for the enabled status.
Any data elements should be declared private. If you think it is necessary you may include helper methods of your own. The class should implement the following public methods:
Device.java
public class Device {
// Data members
private String name;
private int id;
private boolean status;
private String category = "generic";
// Constructor to initialize data members
public Device(String name, int id) {
this.name = name;
this.id = id;
this.status = false;
}
// Getter method for name, id and category
public final String getName() {
return name;
}
public final int getID() {
return id;
}
public String getCategory() {
return category;
}
// Setter method for status
public void enable() {
status = true;
}
public void disable() {
status = false;
}
// Getter method for status
public boolean isEnabled() {
return status;
}
// Return device details
@Override
public String toString() {
return category + " " + id + ", " +
name;
}
}
DeviceTest.java
public class DeviceTest {
public static void main(String[] args) {
// Create device object and print
it's details
Device device = new Device("x64
processor core",0);
System.out.println(device);
}
}
OUTPUT