Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[CSE 007] Classwork: Classes, Inheritance & Polymorphism Date: 27 Apr 2022 Objective: The purpose of this activity is to revisit the Book and Encyclopedia

image text in transcribedimage text in transcribed

[CSE 007] Classwork: Classes, Inheritance & Polymorphism Date: 27 Apr 2022 Objective: The purpose of this activity is to revisit the Book and Encyclopedia classes in order to intentionally incorporate polymorphism. Submission: Book.java, Encyclopedia.java, PolyBook.java The third pillar of object-oriented programming is polymorphism. This is the idea that given derived classes (created through the use of inheritance), we can use a reference variable of a superType to hold a reference to a subType object. For this assignment, you will revisit 2 classes related by inheritance (showcasing an IS-A relationship): Book.java and Encyclopedia.java. You will create a third program to instantiate these classes and incorporate the use of polymorphism (PolyBook.java). public class Book {}: This program is provided for you under the Week 14 Resources, and serves as the parent class to Encyclopedia. You used this file to complete BookInformation in Week 14, but take a few moments to review the code. TO DO: o Add comments throughout the program explaining/identifying different components of OOP Create a 4 argument constructor that expects String title, String author, String publisher, String publicationDate o Change the print Info () method to override the toString() method from Object. Instead of having a void return type and simply printing out the message with relevant information related to the book object, you should create an equivalent String object and return that value. O public class Encyclopedia {}: This is a direct child/subclass to Book that is also provided. Again, review this code to familiarize yourself with it's members. TO DO: o Add comments throughout the program explaining/identifying different components of OOP Create three constructors: A 6 argument constructor that expects String title, String author, String publisher, String publicationDate, String edition, int volumes O A 4 argument constructor that expects String title, String author, String publisher, String publication Date A 2 argument constructor that expects String edition, int volumes o Change the print Info () method to override the toString() method from Book (described above). Instead of having a void return type and simply printing out the message with relevant information related to the book object, you should create an equivalent String object and return that value. Note: you should still invoke the super version of the overridden toString method. public class PolyBookInformation {}: To complete this class, you will update your code to incorporate constructor chaining and polymorphism. The main method you create should be simple enough to test your code, but also highlight the power of polymorphism. Create the Constructors as indicated above in both Book and Encyclopedia o Change code in getBook and getEncyclo to utilize these constructors. Change the printAll() method to accept an array of Books and iterate through that array to display each book O Note: You do not need to explicitly invoke .toString() but you can O Both are fine/provide same output: System.out.println (books [i]); System.out.println (books [i].toString()); Modify the main method to call printAll with each array (books and encyclopedias) separately. O Add a comment to explain how calling this method with an array of Encyclopedias is an example of polymorphism. Optional Challenges (good practice for exam): 1. A single array of type Books to hold references to both Books and Encyclopedias a. Use calls to various constructors that you created to create references to both/either class 2. Showcase the use of polymorphism with various calls to SubClass methods using a reference variable of declared type SuperClass. 3. Create method(s) in Book that will allow you to showcase inheritance within PolyBook.java a. Inheritance is leveraged when we use SubClass reference to invoke SuperClass methods. 4. Come up with your own challenge: a. How can you incorporate/highlight elements of object-oriented programming into the program? b. What functionality could you add to Book and Encyclopedia? i. Should you override any methods? ii. Should you overload any methods? c. Are there any other derived classes that you could create?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Auditing A Business Risk Approach

Authors: Karla Johnstone, Audrey Gramling, Larry Rittenberg

8th edition

538476230, 978-0538476232

More Books

Students also viewed these Programming questions

Question

UNIT 1 - MILESTONE 1 Q o. O O 1.0 O 10 0

Answered: 1 week ago