Question

In: Computer Science

Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing)...

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.

Solutions

Expert Solution

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!


Related Solutions

03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing...
03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing (or while completing) the preparation material for this week, complete the following exercise. Introduction Recall from your mathematics classes that a complex number is composed of two parts, a real part and an imaginary part. We could write a complex number in the form "3 + 4i" where 3 is the real part and 4 is the imaginary part. For this program, we will...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
7. When a class contains objects of another class, the relationship is called a whole-part relationship...
7. When a class contains objects of another class, the relationship is called a whole-part relationship or composition. The relationship created is also called a has-a relationship. Discuss some of the possible issues with composition, such CIS216 – Programming Principles Object-Oriented Programming as the long statement: output sales.getHighestPaidEmployee().getHireDate().getYear(). 8. Describe the rules that govern the attributes of a parent class that can be accessed by a child class and the reverse 9. Discuss the advantages of using inheritance and why...
Create a new Python program (you choose the filename) that contains a main function and another...
Create a new Python program (you choose the filename) that contains a main function and another function named change_list. Refer to the SAMPLE OUTPUT after reading the following requirements. In the main function: create an empty list. use a for loop to add 12 random integers, all ranging from 50 to 100, to the list. use second for loop to iterate over the list and display all elements on one line separated by a single space. display the 4th element,...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types described above Create and Add...
Python Objective Develop an interface system using techniques and design patterns seen in class through a...
Python Objective Develop an interface system using techniques and design patterns seen in class through a problem with realistic constraints. Specs   Write an interactive program for a board game called Parcheesi. Use just the basic rules. It can be consulted on Wikipedia Make two implementations of user interface, one text-based and the other graphically. Use the case study "Dice Poker" in Chapter 12 as a reference It should not include any other graphics system other than graphics.py Use inheritance that...
After successfully completing your corporate finance class, you feel the next challenge ahead is to serve...
After successfully completing your corporate finance class, you feel the next challenge ahead is to serve on the board of directors of Schenkel Enterprises. Unfortunately, you will be the only individual voting for you. If the company has 530,000 shares outstanding and the stock currently sells for $35, how much will it cost you to buy a seat if the company uses straight voting? (Do not round intermediate calculations and round your answer to the nearest whole number, e.g., 32.)...
After successfully completing your corporate finance class, you feel the next challenge ahead is to serve...
After successfully completing your corporate finance class, you feel the next challenge ahead is to serve on the board of directors of Schenkel Enterprises. Unfortunately, you will be the only individual voting for you. Requirement 1: If Schenkel has 350,000 shares outstanding and the stock currently sells for $37, how much will it cost you to buy a seat if the company uses straight voting? (Do not round intermediate calculations.)   Total cost $    Requirement 2: Assume that Schenkel uses cumulative...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT