Sum a Subclass-Only Value With instanceof and a Cast
mediumQuestion 520A library lends books and DVDs. Both Book and Dvd extend LibraryItem, but only Book has getPages(). Write a method that adds up the pages of the books in a mixed array.
Requirements:
1. In the Library class write public static int totalPages(LibraryItem[] items).
2. It returns the sum of getPages() for every element that is a Book. Elements that are not books add nothing.
3. An array with no books, or an empty array, gives 0.
4. The compiler will not accept getPages() on a LibraryItem variable. Test each element with instanceof, then cast it to Book before you call the method.
Do not change the LibraryItem, Book and Dvd classes.
Do NOT write a main method.
Expected output
886= newline. The output ends with a newline, so print one after the last line.
Hints4 available
Sign in to use hintsTopics
InheritanceClasses
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your output
Run your code to see the output here...
Test cases
Run your code to see test case results.