Question
IN JAVA: Write a parent class, Device, which implements a generic computer device. Create two child classes, Disk and Printer, which specialize a Device with
IN JAVA: Write a parent class, Device, which implements a generic computer device.
Create two child classes, Disk and Printer, which specialize a Device with added functionality.
Device Task:
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:
- public Device(String name, int id) Initialize the device object with the given name and ID. Also, initially the device should not be enabled (the enabled flag should begin as false)
- public final String getName() Retrieves the name of this device. This method is labelled final, meaning that future subclasses cannot change how it behaves.
- public final int getID() Retrieves the ID of this device. This method is also final.
- public String getCategory() Retrieves the specific category of this device, which should be the string "generic"
- public void enable() Sets the state of the device to enabled.
- public void disable() Sets the stae of the device to not enabled.
- public boolean isEnabled() Retrieves the current state of the device.
- @Override public String toString() Ret
Now, we will write a Disk device, which is more specific than a generic device, and thus Disk should extend Device. Since we are extending the class, it will automatically inherit all of the functionality of the Device class. We will just add some specifics associated with disks - the notion of disk size, which should be stored as a long because disk sizes can be relatively large.
Something to think about: since we are not defining enabled() or disabled() here, what should happen if we try to call them for some Disk object?
The class should implement the following public methods:
- public Disk(String name, int id, long size) Initialize the device with the given name, ID, and now disk size. Be sure to use the parent class's constructor for part of this, because it's the only way you'll be able to initialize the parent's fields.
- @Override public String getCategory() This should return the value "disk"
- public long getSize() Retrieves the size of the disk in bytes.
- @Override public String toString() Returns the same result as the Device's version of toString() with the addition of the size in bytes in parenthesis. So for example,
disk 1, 7500rpm HDD (1099511627776 bytes) Now, we will write a Printer device, which also derives from generic devices, but this time includes the notion of jobs to be printed. Like Disk, a Printer should extend from Device. Additionally, a Printer should have an int which represents the number of jobs currently in the printer's print queue. We can add or complete print jobs, or check the number currently in the queue, but for this assignment that is the extent of what we expect (we will not be passing full-fledged print jobs to the class, we will just keep track of the number of jobs).
-
The class should implement the following public methods:
- public Printer(String name, int id) Initializes the device with name and ID as before. Make sure to use the parent's constructor, otherwise the parent's fields will not be set. Additionally, the number of print jobs should initially be set to zero.
- @Override public String getCategory() This should return the value "printer".
- @Override public void disable() Whenever a printer device is disabled, the number of active print jobs should be reset to zero, in addition to anything that the disable() method was already doing in the parent class.
-
Example:
Below is a sample run demonstrating how the class would function using the following input: 1 abc 2.0 3 def 4.0 6 7 xyz 5.5
- public void submitJob() Increase the number of print jobs by one, but only if the printer device is currently enabled.
- public int numJobs() Report the number of currently active print jobs.
- public void completeJob() Complete one of the currently active print jobs (i.e. reduce the number of jobs by one if there are more than zero jobs to print).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started