04 Prepare : Checkpoint A
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,
displaythat 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,
displaythat 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.