In: Computer Science
Objective
Demonstrate composition in Python (a class that contains
another).
Overview
After completing (or while completing) the preparation material for
this week, complete the following exercise.
For this exercise, you will create a class to model books and the
author that wrote it. Note the relationship that a book has an
author.
At this point, you should put all of your classes and other code in
the same file. Don't worry about separating it out into separate
modules.
Instructions
Write a Python 3 program according to the following:
Person:
• Create a class for a Person that contains a name and a birth
year. The author of a book will be a Person object.
• Create an initializer for your Person class to set the default
name to be "anonymous" and the year to be "unknown".
• Create a method, display that displays the Person's name and
birth year in the format "name (b. year)"
Book:
• Create a class for a Book that contains a title, an author (of
type Person), and a publisher.
• Create an initializer for your Book class to set the default
title to be "untitled", the author to be a Person, and the
publisher to be "unpublished".
• Create a method, display that displays the Book's information as
follows (don't forget to have the book display method call the
author's display method):
The Great Divorce
Publisher:
Geoffrey Bles
Author:
C.S. Lewis (b. 1898)
Then, create a main function that does the following:
• Create a new book
• Call that book's display function
• Prompts the user for each of the following: author name and birth
year, and the books title and publisher.
• Sets these values for the current book and it's author.
• Calls the book's display function again.
Sample Output
The following is an example of output for this program:
untitled
Publisher:
unpublished
Author:
anonymous (b. unknown)
Please enter the following:
Name: C.S. Lewis
Year: 1898
Title: The Great Divorce
Publisher: Geoffrey Bles
The Great Divorce
Publisher:
Geoffrey Bles
Author:
C.S. Lewis (b. 1898)
Automatic Grading Script (TestBed)
This assignment is pass/fail. In order to receive credit, it must
pass the auto-grading test script (TestBed). You can run this
script as many times as you like while you are working on the
assignment. The same test script will be run by the instructor for
credit, so you should not be surprised at the result.
In addition, because the point of this exercise is to help you
practice the use of classes. You must use classes to receive credit
for this assignment.
To run the test script, log into the CS Department Linux system and
run the testBed command, providing the class and test script to run
as well as the Python program to use, for example:
testBed cs241/check04a check04a.py
Submission
Submit your program by logging into the CS Department Linux system,
and running the submit command with your filename, for
example:
submit check04a.py
You should then select the appropriate course (CS 241), instructor,
and assignment from the list.
class Person: #The object of Person class will be used as
Author
def __init__(self):
self.name='anonymous' #name of
person or author
self.birth_year='unknown' #birth
year of person or author
def display(self):
print('{} (b.
{})\n\n'.format(self.name,self.birth_year)) #display in the format
"Name (b. birth year)"
class Book: #Object of this class will contain all details of
Book
def __init__(self):
self.title='untitled' #title of
book originally "untitled"
self.author=Person() #author of
book of Person type
self.publisher='unpublished'
#Publisher of book originally "unpublished"
def display(self):
print(self.title)
print('Publisher:
\n{}'.format(self.publisher)) # display as per the provided
format
print('Author: ')
self.author.display() #display the
author details
def main(): #main function to do the remaining tasks
NewBook=Book() #Create a new book
NewBook.display() #display the original details of
the book
print("Enter following details for book:") #Take all
details of the book
NewBook.author.name=input('Author Name: ') #Accept and
assign author name
NewBook.author.birth_year=input('Birth Year: ')
#Accept and assign author birth year
NewBook.title=input('Book Title: ') #Accept and assign
book title
NewBook.publisher=input('Publisher: ') #Accept and
assign publisher to the book
print('\n\n')
NewBook.display() #display all details of the book
main() #calling the main function
Save the above program with the required name and .py extension and run it with python3
Thank you!