Using inheritance, make a basic library that can hold four kinds of materials.
Resource.java should be an abstract class and a parent class for the other four kinds of
materials. It should have instance variables for:
● Type (what kind of resource it is)
● Is it electronic or not
● Is it checked-out or not
● Who it is checked-out by
Use your judgement about what type each field should be. int and boolean are faster to use and
search than String, and take up less space; use simpler types when you can.
Resource should also have the following methods:
● a constructor that requires the following information be passed in when a new Resource
is created: title, publisher, type, if it’s electronic or not. All Resources start as not
● getters for Title, Type, Is it checked-out or not, Is electronic, and Who it is checked-out to
● a toString method that returns a string containing the variable information in an easy-toread way.
● a method to check out a resource. Checking out a resource should require a string of
who it’s going to, set the is-checked-out variable to true, and set who it’s checked out to
to the appropriate name.
● a method to check in a resource. Checking in a resource should set the is-checked-out
variable to false, and who it’s checked out by to null; it doesn’t need any parameters.
Use your judgement about parameters, return values, and similar.
Book, Journal, CD, DVD
Each of these classes should inherit from Resource. Their constructors should call their parent
constructor, and set their respective Types correctly: Book.java’s Type should be “Book”,
Journal.java’s Type should be “Journal”, etc. Each class should overwrite the toString method,
printing out all relevant variables in an easy-to-read way. An example is below:
Each class has different required variables. Be sure to write getters and setters for each!
The Book.java specific fields are:
● Number of pages
● Publication year
The Journal.java specific fields are:
● Range of years published
● How often they are published (publishing frequency -- could be monthly, quarterly, etc.)
● Is it electronic or not: This is not a new field, but all Journals are assumed to be
The CD.java specific fields are:
● Recording company (use Publisher field)
● Is it electronic or not: This is not a new field, but all CDs are assumed to be NOT
The DVD.java specific fields are:
● Is it electronic or not: This is not a new field, but all DVDs are assumed to be NOT
This class should be able to perform different tasks on an array of Resources. Write the
● getAllCheckedOut, which takes an array of Resource objects, and returns a smaller
array of Resource objects. Because you’re using arrays, you’ll need to first count how
many items are checked out, then create an array of that size, then copy all checked-out
items into the new array.
● getAllUserHasCheckedOut, which takes an array of Resource objects and a String, and
returns a smaller array of Resource objects. Similarly to above, you need to count how
many items someone has checked out before creating the array and copying the
Resource objects into it. Additionally, you need to figure out what to do if that particular
person has nothing checked out.
● getAllOfType, which takes an array of Resource objects and a String, and returns a
smaller array of Resource objects. You can check what type an object is either by using
the getType method you wrote
A main method is not required for submission; however, it is recommended so you can test
Hints and Advice
It’s a bad idea to try writing every class at once, and only then compiling. A suggested order of
1. Write Resource.java. Start with the variables, the constructor, and toString.
2. Create Library, but only write the main method. Create an array of four or so Resources.
Set them, and make sure all the methods in Resource work as expected.
3. Keep adding and testing methods in Resource until you have them all.
4. Write one of the child class, such as Book.java. Start with the variables, the constructor,
5. In the main method of Library, replace the Resources with Books. Make sure everything
still works as expected.
a. The array can still be of type Resource, since all Books are Resources
6. Write another child class, such as DVD.
7. In the main method of Library, add some DVDs to the array. Again, make sure things still
a. Your array should be of Resources, since both DVDs and Books are Resources,
so both can be added to arrays.
8. Repeat the above two steps until all child classes are complete.
9. Write one method at a time in Library, testing each after you write it.
10. Compile your code with the test file above. Make any adjustments so they run well
Null Pointer Exceptions
You can’t call a method on an object that’s null, or you’ll get a NullPointerException. A good
check to include in all Library methods is testing if a variable is null before calling methods on it.
The check can be written like so:
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
* Description: This is the Book Class. The class constructors, methods, and
* setting values for the book class. This class is a child to the Resource
* Class. The toString method to assist in outputting.
// Beginning of Book Class
public class Book extends Resource
// Declare class variables
private String author;
private int noOfPages;
private int publishYear;
private String genres;
// Beginning of Book Default Constructor
// Setting variables for the constructor
// Ending of Book Default Constructor
// Beginning of Book Constrctor with parameters
public Book(String title,String author, String publisher,int noOfPages,
int publishYear, String genres,boolean isCheckedOut)
// Setting values for constructor
// Ending of Book Constrctor with parameters
// Beginning of getAuthor method
public String getAuthor()
// Returns author variable
// Ending of getAuthor method
// Beginning of getNoOfPages method
public int getNoOfPages...