Question

Now, we will write a Disk device, which is more specific than a generic device, and...

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)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Disk Class:

Code Image:

Code to Copy:

//Disk.java

public class Disk extends Device {

     //additional attribute

    private long size;

  

    //Implementation of Constructor

    public Disk(String name, int id, long size) {

    //passing name and id to super class

       super(name, id);

       this.size = size;

    }

    @Override

    //Implementation of getCategory method

    public String getCategory() {

    //return value should be disk

    return "disk";

    }

    //Implementation of getSize method

    public long getSize() {

    //return the retrieves size of

    //the disk in bytes

    return size;

    }      

    @Override

    //Implementation of toString method

    public String toString() {

    //addition of the size of the bytes

    //in parenthesis.

       return super.toString()+" ("+size+" bytes)";

    }

}

Complete Program:

Code Image:

Sample Output:

  • I have tested the complete code with E4tester.java.

Code to Copy:

//Disk.java

public class Disk extends Device {

     //additional attribute

    private long size;

  

    //Implementation of Constructor

    public Disk(String name, int id, long size) {

    //passing name and id to super class

       super(name, id);

       this.size = size;

    }

    @Override

    //Implementation of getCategory method

    public String getCategory() {

    //return value should be disk

    return "disk";

    }

    //Implementation of getSize method

    public long getSize() {

    //return the retrieves size of

    //the disk in bytes

    return size;

    }      

    @Override

    //Implementation of toString method

    public String toString() {

    //addition of the size of the bytes

    //in parenthesis.

       return super.toString()+" ("+size+" bytes)";

    }

}

//Device.java

public class Device {

     // attributes

    private String name;

    private int id;

    private boolean enabled;

    // Implementation of Device Constructor

    //which is initialize fields

    public Device(String name, int id) {

    this.name = name;

        this.id = id;

        // by default it is disabled state

        enabled = false;

    }

    // Implementation of getName method

    //which returns name

    public final String getName() {

    return name;

    }

    //Implementation of getID method

    //which returns id

    public final int getID(){

    return id;

    }

    // Implementation of getCategory

    //which returns the category,

    public String getCategory() {

    //return generic

    return "generic";

    }

    // Implementation of enable method

    //which enables the device

    public void enable() {

    enabled = true;

    }

    //Implementation of disable method

    //which disables the device

    public void disable() {

    enabled = false;

    }

    //Implementation of isEnabled method

    //which returns the state of device

    public boolean isEnabled() {

    return enabled;

    }

    @Override

    //Implementation of toString() method

    public String toString() {

    return getCategory() + " " + id + ", " + name;

    }        

}

//Printer.java

public class Printer extends Device {

    

    private int numJobs;

    // Implementation of constructor

    public Printer(String name, int id) {

    // passing name and id to super class

        super(name, id);

        // initializing numJobs to 0

        numJobs = 0;

   }

    @Override

    //Implementation of getCategory method

    public String getCategory() {

    //the return value is printer

    return "printer";

    }

    @Override

    //Implementation of disable method

    public void disable() {

    // call disable method

    //which disabling the device

        super.disable();

        // resetting number of jobs

        numJobs = 0;

    }

    // Implementation of submitJob method

    //which submit a job

    public void submitJob() {

    // incrementing numJobs by 1

    //if device is enabled

        if (isEnabled()) {

        numJobs = numJobs +1 ;

        }

    }

    // Implementation of numJobs method

    //which returns the number of jobs

    public int numJobs() {

    //return numJobs

    return numJobs;

    }

    //Implementation of completeJob

    //which decrements the number of jobs

    public void completeJob() {

    if (numJobs > 0) {

          //decrement the numJobs

          numJobs = numJobs -1;

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Now, we will write a Disk device, which is more specific than a generic device, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use JAVA only Write a Printer device, which also derives from generic devices, but this...

    Please use JAVA only 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 intwhich 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...

  • please write program in java and comment the code clearly. I am providing previous classes Media...

    please write program in java and comment the code clearly. I am providing previous classes Media and payment codes to be used for this class.(its not a big code to write , it seems ,because i added previous classes , pleases help. public class Media { String name; int year;    // a constructor which initializes the media with the provided name and publication year. public Media(String name, int year) { this.name = name; this.year = year; }    //...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • **** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. ****...

    **** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. **** *** ALSO MAKE SURE IT PASS THE TESTER FILE PLEASE*** Often when we are running a program, it will have a number of configuration options which tweak its behavior while it's running. Allow text completion? Collect anonymous usage data? These are all options our programs may use. We can store these options in an array and pass it to the program. In its simplest...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • This wont take you more than 15 mins. Comple the two method and besure to test...

    This wont take you more than 15 mins. Comple the two method and besure to test with Junit test that provided below. toArray() -- this method returns a newly allocated array containing the elements in the multiset. The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset. fromArray(E[] arr) -- this method updates the multiset so that...

  • Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank...

    Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank you. What the tester requires: The AccountTester This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method. User and Bot information will be read from a file. A sample file is provided. Use this file format to aid in grading....

  • Introduction In this final programming exercise, you'll get a chance to put together many of the...

    Introduction In this final programming exercise, you'll get a chance to put together many of the techniques used during this semester while incorporating OOP techniques to develop a simple song playlist class. This playlist class allows a user to add, remove and display songs in a playlist. The Base Class, Derived Class and Test Client Unlike the other PAs you completed in zyLabs, for this PA you will be creating THREE Java files in IntelliJ to submit. You will need...

  • Please Write the following program in c# You are to write an application which will create...

    Please Write the following program in c# You are to write an application which will create a company, add agents (their name, ID, and weekly sales) to that company, and printout the details of all agents in the company. The application will contain three classes: Agent.cs, Company.cs, and CompanyTest.cs (this class is already provided). Flow of the application: The CompanyTest class creates an object of the Company class and reserves space for five agents. At this point if you call...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT