Design a class named Book that holds a stock number,
author, title, price, and number of pages for a book.
Include methods to set and get the values for each data
field. Also include a displayInfo() method that displays
each of the Book’s data fields with explanations.
b. Design a class named TextBook that is a child class of
Book. Include a new data field for the grade level of the
book. Override the Book class displayInfo() method so
that you accommodate the new grade-level field.
c. Design an application that instantiates an object of each
type and demonstrates all the methods.
To fulfill this requirement, you can design three classes: `Book`, `TextBook`, and an application class to demonstrate them. Here's the implementation of each class:
1. Class `Book`:
```python
class Book:
def __init__(self, stock_number, author, title, price, num_pages):
self.stock_number = stock_number
self.author = author
self.title = title
self.price = price
self.num_pages = num_pages
def set_stock_number(self, stock_number):
self.stock_number = stock_number
def set_author(self, author):
self.author = author
def set_title(self, title):
self.title = title
def set_price(self, price):
self.price = price
def set_num_pages(self, num_pages):
self.num_pages = num_pages
def get_stock_number(self):
return self.stock_number
def get_author(self):
return self.author
def get_title(self):
return self.title
def get_price(self):
return self.price
def get_num_pages(self):
return self.num_pages
def display_info(self):
print("Stock Number:", self.stock_number)
print("Author:", self.author)
print("Title:", self.title)
print("Price:", self.price)
print("Number of Pages:", self.num_pages)
```
2. Class `TextBook` (child class of `Book`):
```python
class TextBook(Book):
def __init__(self, stock_number, author, title, price, num_pages, grade_level):
super().__init__(stock_number, author, title, price, num_pages)
self.grade_level = grade_level
def set_grade_level(self, grade_level):
self.grade_level = grade_level
def get_grade_level(self):
return self.grade_level
def display_info(self):
super().display_info()
print("Grade Level:", self.grade_level)
```
3. Application class to demonstrate the classes:
```python
class Application:
def run(self):
# Creating an object of class Book
book = Book("B001", "John Doe", "Sample Book", 19.99, 250)
book.display_info()
print("------")
# Creating an object of class TextBook
textbook = TextBook("T001", "Jane Smith", "Math Textbook", 49.99, 400, 8)
textbook.display_info()
# Running the application
app = Application()
app.run()
```
In this implementation, the `Book` class represents a book and includes methods to set and get the values for each data field. It also includes a `display_info()` method to display all the book's data fields.
The `TextBook` class is a child class of `Book`, which adds a new data field called `grade_level`. It overrides the `display_info()` method to include the additional field.
Finally, the `Application` class instantiates an object of each type (a `Book` object and a `TextBook` object) and calls their respective `display_info()` methods to demonstrate the functionality.
You can modify the class names, attribute names, and display format according to your specific needs.