IT 117: Introduction to Scripting
Homework 9
Due
Sunday, November 10th at 11:59 PM
What You Need to Do
- Create the script hw9.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw9
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw9.py
Specification
- Define the class Book that has the following attributes
- title
- subtitle
- author
- publisher
- This class must have the following methods
- get_title
- get_subtitle
- get_author
- get_publisher
- __str__
- All attributes in this class MUST be hidden
Methods
__init__
- Header
def __init__(self, title, subtitle, author, publisher):
- This method must set all attributes
get_title
- Header
def get_title(self):
- This method returns the value of the title
attribute
get_subtitle
- Header
def get_subtitle(self):
- This method returns the value of the
subtitle attribute
get_author
- Header
def get_author(self):
- This method returns the value of the
author attribute
get_publisher
- Header
def get_publisher(self):
- This method returns the value of the
publisher attribute
__str__
- Header
def __str__(self):
- This method returns a string with the values of
title, subtitle,
author and
publisher, each separated by a comma
Script for this assignment
Open an a text editor and create the file
hw9.py.
You can use the editor built into IDLE or a program like Sublime.
Test Code
Your hw9.py file must contain the following test code at the
bottom of the file:
book = Book("Guns, Germs, and Steel", "The Fates of Human Societies", "Jared Diamond", "Norton")
print(book.get_title())
print(book.get_subtitle())
print(book.get_author())
print(book.get_publisher())
print(book)
You should see
Guns, Germs, and Steel
The Fates of Human Societies
Jared Diamond
Norton
Guns, Germs, and Steel: The Fates of Human Societies, Jared Diamond, Norton
Suggestions
Write this program in a step-by-step fashion using the technique of
incremental development.
In other words, write a bit of code, test it, make whatever changes you need to get it
working, and go on to the next step.
-
Make a copy of your hw8.py file in your hw9
directory.
Change the name of this file to hw9.py
Remove all the test code from hw9.py
Run the script.
You should see nothing.
Fix any problem you find.
-
Change __init__ by adding the parameter publisher.
Copy the first line of the test code to the bottom of your script.
Run the script.
You should see nothing.
Fix any problem you find.
-
Change __init__ and get_title so the attribute
title is hidden.
Copy the 2nd line of the test code to the bottom of your script.
Run the script.
Fix any problem you find.
-
Change __init__ and get_subtitle so the attribute
subtitle is hidden.
Copy the 3rd line of the test code to the bottom of your script.
Run the script.
Fix any problem you find.
-
Change __init__ and get_author so the attribute
author is hidden.
Copy the 4th line of the test code to the bottom of your script.
Run the script.
Fix any problem you find.
-
Change __init__ so it sets the value of the attribute publisher.
Create the method get_publisher that returns the value of this attribute.
Copy the 5th line of the test code to the bottom of your script.
Run the script.
Fix any problem you find.
-
Remove the method print_book.
Replace it with the method __str__ which
returns a string containing the values of each of the attributes.
Copy the last line of the test code to the bottom of your script.
Run the script.
Fix any problem you find.
Testing on Your Machine
- Open IDLE
- Use the Open command in IDLE to open hw9.py
- Under the Run menu, select Run Module
- Your output should look something like this
Guns, Germs, and Steel
The Fates of Human Societies
Jared Diamond
Norton
Guns, Germs, and Steel: The Fates of Human Societies, Jared Diamond, Norton
Copy the file to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
You will have to connect using your Unix username and password.
- Go to your 117 directory
- Go to your hw directory
- Inside this directory create the directory hw9
- Copy your hw9.py file from your machine to the
hw9 directory on pe15
Make the File Executable
- Connect to pe15.cs.umb.edu
Use an ssh client.
- Go to your it117 directory
cd 117
- Go to your hw directory
cd hw
- Go to your hw9 directory
cd hw9
- Change the permissions on the script
chmod 755 hw9.py
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.