In: Computer Science
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).
Please run the following test cases to make sure the program works:
import org.junit.*; import static org.junit.Assert.*; import java.util.*; import java.io.*; public class E4tester { public static void main(String args[]){ org.junit.runner.JUnitCore.main("E4tester"); } @Test public void device_construct() { Device dev = new Device("test device", 10); assertEquals("device name improperly reported", "test device", dev.getName()); assertEquals("device ID improperly reported", 10, dev.getID()); } @Test public void disk_construct() { Disk disk = new Disk("test disk", 11, 1024); assertEquals("disk name improperly reported", "test disk", disk.getName()); assertEquals("disk ID improperly reported", 11, disk.getID()); assertEquals("disk size improperly reported", 1024, disk.getSize()); } @Test public void printer_construct() { Printer p = new Printer("test printer", 12); assertEquals("printer name improperly reported", "test printer", p.getName()); assertEquals("printer ID improperly reported", 12, p.getID()); } @Test public void test_inheritance() { Device d1 = new Disk("",0,0); Device d2 = new Printer("",0); assertFalse("a disk should not be a printer", d1 instanceof Printer); assertFalse("a printer should not be a disk", d2 instanceof Disk); } @Test public void device_methods() { Device dev = new Device("dev check", 5); assertEquals("incorrect device category", "generic", dev.getCategory()); assertEquals("incorrect device string", "generic 5, dev check", dev.toString()); assertFalse("device should begin disabled", dev.isEnabled()); dev.enable(); assertTrue("device does not enable properly", dev.isEnabled()); dev.disable(); assertFalse("device does not disable properly", dev.isEnabled()); } @Test public void disk_methods() { Disk disk = new Disk("disk check", 6, 555); assertEquals("incorrect device category", "disk", disk.getCategory()); assertEquals("incorrect device string", "disk 6, disk check (555 bytes)", disk.toString()); assertEquals("incorrect disk size", 555, disk.getSize()); assertFalse("device should begin disabled", disk.isEnabled()); disk.enable(); assertTrue("device does not enable properly", disk.isEnabled()); disk.disable(); assertFalse("device does not disable properly", disk.isEnabled()); } @Test public void printer_methods() { Printer p = new Printer("print check", 7); assertEquals("incorrect device category", "printer", p.getCategory()); assertEquals("incorrect device string", "printer 7, print check", p.toString()); assertFalse("device should begin disabled", p.isEnabled()); assertEquals("device should begin with no jobs", 0, p.numJobs()); p.submitJob(); assertEquals("device should not accept jobs while disabled", 0, p.numJobs()); p.completeJob(); assertEquals("device should not print jobs while disabled", 0, p.numJobs()); p.enable(); assertTrue("device does not enable properly", p.isEnabled()); for (int i = 0; i < 5; i++) { assertEquals("incorrect number of jobs after submitting", i, p.numJobs()); p.submitJob(); } for (int i = 5; i > 0; i--) { assertEquals("incorrect number of jobs after printing", i, p.numJobs()); p.completeJob(); } p.completeJob(); assertEquals("device should not print jobs unless there are available jobs", 0, p.numJobs()); p.submitJob(); assertEquals(1, p.numJobs()); p.disable(); assertFalse("device does not disable properly", p.isEnabled()); assertEquals("print jobs should clear when device is disabled", 0, p.numJobs()); }
The output and screenshot are at the end of this answer.
If you have any questions or need clarifications, leave me a comment.
If this answer helps you with your assignment, spare a moment and upvote this answer.
//File: Device.java
public class Device {
//member variables
private String name;
private int id;
private boolean enabled;
//Parameterized constructor
public Device(String newName, int newID) {
name = newName;
id = newID;
enabled = false;
}
//Getter and Setter methods
public final String getName() {
return name;
}
public int getID() {
return id;
}
public String getCategory() {
return "generic";
}
public void enable() {
enabled = true;
}
public void disable() {
enabled = false;
}
public boolean isEnabled() {
return enabled;
}
@Override
public String toString() {
return getCategory() + " " +
getID() + ", " + getName();
}
}
//File: Disk.java
//class extends the base class - Device
public class Disk extends Device {
//Member variable
private long size;
//Parameterized constructor
public Disk(String newName, int newID, long newSize)
{
super(newName, newID);
size = newSize;
}
//Getters
@Override
public String getCategory() {
return "disk";
}
public long getSize() {
return size;
}
@Override
public String toString() {
return super.toString() + " (" +
size + " bytes)";
}
}
//File: Printer.java
//Class extends teh base class - Device
public class Printer extends Device {
//Member variable
private int numJobs;
//Parameterized constructor
public Printer(String newName, int newID) {
super(newName, newID);
numJobs = 0;
}
//Getters and Setters
@Override
public String getCategory() {
return "printer";
}
@Override
public void disable() {
super.disable();
numJobs = 0;
}
public void submitJob() {
if(isEnabled()) {
numJobs++;
}
}
public int numJobs() {
return numJobs;
}
public void completeJob() {
if(numJobs > 0) {
numJobs--;
}
}
}
//File: E4tester.java (unmodified)
/**
* On Mac/Linux:
* javac -cp .:junit-cs211.jar *.java # compile everything
* java -cp .:junit-cs211.jar E4tester # run tests
*
* On windows replace colons with semicolons: (: with ;)
* javac -cp .;junit-cs211.jar *.java # compile everything
* java -cp .;junit-cs211.jar E4tester # run tests
*/
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import java.io.*;
public class E4tester {
public static void main(String args[]){
org.junit.runner.JUnitCore.main("E4tester");
}
@Test
public void device_construct() {
Device dev = new Device("test device", 10);
assertEquals("device name improperly reported", "test device",
dev.getName());
assertEquals("device ID improperly reported", 10,
dev.getID());
}
@Test public void disk_construct() {
Disk disk = new Disk("test disk", 11, 1024);
assertEquals("disk name improperly reported", "test disk",
disk.getName());
assertEquals("disk ID improperly reported", 11,
disk.getID());
assertEquals("disk size improperly reported", 1024,
disk.getSize());
}
@Test public void printer_construct() {
Printer p = new Printer("test printer", 12);
assertEquals("printer name improperly reported", "test printer",
p.getName());
assertEquals("printer ID improperly reported", 12,
p.getID());
}
@Test public void test_inheritance() {
Device d1 = new Disk("",0,0);
Device d2 = new Printer("",0);
assertFalse("a disk should not be a printer", d1 instanceof
Printer);
assertFalse("a printer should not be a disk", d2 instanceof
Disk);
}
@Test public void device_methods() {
Device dev = new Device("dev check", 5);
assertEquals("incorrect device category", "generic",
dev.getCategory());
assertEquals("incorrect device string", "generic 5, dev check",
dev.toString());
assertFalse("device should begin disabled", dev.isEnabled());
dev.enable();
assertTrue("device does not enable properly",
dev.isEnabled());
dev.disable();
assertFalse("device does not disable properly",
dev.isEnabled());
}
@Test public void disk_methods() {
Disk disk = new Disk("disk check", 6, 555);
assertEquals("incorrect device category", "disk",
disk.getCategory());
assertEquals("incorrect device string", "disk 6, disk check (555
bytes)", disk.toString());
assertEquals("incorrect disk size", 555, disk.getSize());
assertFalse("device should begin disabled",
disk.isEnabled());
disk.enable();
assertTrue("device does not enable properly",
disk.isEnabled());
disk.disable();
assertFalse("device does not disable properly",
disk.isEnabled());
}
@Test public void printer_methods() {
Printer p = new Printer("print check", 7);
assertEquals("incorrect device category", "printer",
p.getCategory());
assertEquals("incorrect device string", "printer 7, print check",
p.toString());
assertFalse("device should begin disabled", p.isEnabled());
assertEquals("device should begin with no jobs", 0,
p.numJobs());
p.submitJob();
assertEquals("device should not accept jobs while disabled", 0,
p.numJobs());
p.completeJob();
assertEquals("device should not print jobs while disabled", 0,
p.numJobs());
p.enable();
assertTrue("device does not enable properly", p.isEnabled());
for (int i = 0; i < 5; i++) {
assertEquals("incorrect number of jobs after submitting", i,
p.numJobs());
p.submitJob();
}
for (int i = 5; i > 0; i--) {
assertEquals("incorrect number of jobs after printing", i,
p.numJobs());
p.completeJob();
}
p.completeJob();
assertEquals("device should not print jobs unless there are
available jobs", 0, p.numJobs());
p.submitJob();
assertEquals(1, p.numJobs());
p.disable();
assertFalse("device does not disable properly",
p.isEnabled());
assertEquals("print jobs should clear when device is disabled", 0,
p.numJobs());
}
}
//Output