Question

In: Computer Science

IN JAVA: Write a parent class, Device, which implements ageneric computer device.Create two child...

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());
    
  }

Solutions

Expert Solution

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


Related Solutions

Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create...
Write a Java application that implements the following class(es) as per business requirements mentioned below: Create an abstract Insurance class (Insurance.java) that has the following instance variables: - Insurance number, - customer name, - start date of policy ( This should be represented by an object of predefined date class in java), - customer address [( Create a new Address class ( Address.java ) having following instance data members: House number , Street name, City, Province, Zip code. Implement getter...
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
Design You will need to have at least four classes: a parent class, a child class,...
Design You will need to have at least four classes: a parent class, a child class, a component class, and an unrelated class. The component object can be included as a field in any of the other three classes. Think about what each of the classes will represent. What added or modified methods will the child class have? What added fields will the child class have? Where does the component belong? How will the unrelated class interact with the others?...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT