Run update21, if you haven't already, to create the cs21/labs/10 directory. Then cd into your cs21/labs/10 directory and create the python programs for lab 10 in this directory (handin21 looks for your lab 10 assignments in your cs21/labs/10 directory).
$ update21 $ cd cs21/labs/10
Printed books are so old school. The *it* technology product everyone is clamoring for are electronic readers, or e-readers as they call them on the street. In this lab, you are going to throw your hat into the e-reader market and develop your very own Swarthmore e-reader a.k.a. the Swindle. Any resemblance to current e-readers in the market is mere coincidence.
In this lab, you will learn object-oriented programming to create a virtual e-reader program. You will define two classes and create several instances of these classes. The first class you will develop is the Book class which encapsulates one electronic format book (or ebook). We have made several free ebooks available for you to load and experiment with once you get your Book class working.
Second, you will create your Swindle class which encapsulates the concept of an e-reader. You will be able to perform several core operations of an e-reader, including buying books, picking a book from your virtual shelf, and finally reading a book.
To get started, take note of the files we have provided for you:
For this lab you should implement each class and test them before you move on to the next step. Start with the Book class.
A Book object represents an electronic book to read on your e-reader. You should define your class such that each book maintains the following data (also called instance variables):
def __init__(self, title, author, published, filename):where the parameters specify the title, author, publication date (year), and the name of the file containing the book. You should initialize all data fields for the object (self) in this function. The bookmark data field for a newly created Book object should be zero, since it's a new book.
text = "Roses are red,\n Violets are blue.\n Sugar is sweet,\n and so are you!"
Here's an example of testing the Book class:
>>> from book import * >>> mybook = Book("Gettysburg Address", "Abe Lincoln", 1863, '/usr/local/doc/GettysburgAddress') >>> print mybook Title: Gettysburg Address Author: Abe Lincoln Year: 1863 Bookmark: on page 0 Filename: /usr/local/doc/GettysburgAddress >>> print mybook.getText() Four Score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated ...
BOOK API SUMMARY:
#TODO: using the information just obtained from the file, # create a Book object with this data and add it to the # appropriate listWe have also provided a display_text() method that takes care of some details involved in displaying a certain number of lines from the book. You should read swindle.py and understand these methods before proceeding.
To finish up your class definition, we need to define the interface for the Swindle; that is, the list of the methods defined for the object. You should define methods that:
Here's an example of testing the Swindle class:
>>> from swindle import * >>> myreader = Swindle("George Washington") >>> print myreader Owner: George Washington Number of Books: 0 Titles:
BOOK API SUMMARY:
SWINDLE API SUMMARY:
Once you have your Book and Swindle classes written, according to the above requirements, you should be able to run ereader.py. An example is shown below. Please test out all features of your classes by trying all options in ereader.py.
$ python ereader.py Since this is the first time you've used it, let's customize your Swindle... Please enter your name: Lisa Meeden Welcome to Lisa Meeden's Swindle v0.1!! ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 3 You don't own any books! Why don't you buy one now? ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 7 Please choose an option from the list! ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 1 Available Books: --------------- 1: Alice in Wonderland by Lewis Carroll (1865) 2: Pride and Prejudice by Jane Austen (1813) 3: Peter Pan by J. M. Barrie (1911) 4: Ulysses by James Joyce (1922) 5: Mary Had A Little Lamb by Sarah Josepha Hale (1830) Which book would you like to buy? (0 to skip) ---> 1 ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 1 Available Books: --------------- 1: Pride and Prejudice by Jane Austen (1813) 2: Peter Pan by J. M. Barrie (1911) 3: Ulysses by James Joyce (1922) 4: Mary Had A Little Lamb by Sarah Josepha Hale (1830) Which book would you like to buy? (0 to skip) ---> 2 ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 2 Books You Own: --------------- 1: Alice in Wonderland by Lewis Carroll (1865) 2: Peter Pan by J. M. Barrie (1911) ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 3 Books You Own: --------------- 1: Alice in Wonderland by Lewis Carroll (1865) 2: Peter Pan by J. M. Barrie (1911) Which book would you like to R-E-A-D? (0 to skip) ---> 1 CHAPTER I. Down the Rabbit-Hole Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time (showing...page 1 out of 167, lines 1-20 out of 3334) enter (continue); q (quit); p (previous); b (bookmark): it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge. In another moment down went Alice after it, never once considering how in the world she was to get out again. The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well. Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she (showing...page 2 out of 167, lines 21-40 out of 3334) enter (continue); q (quit); p (previous); b (bookmark): b enter (continue); q (quit); p (previous); b (bookmark): q ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 3 Books You Own: --------------- 1: Alice in Wonderland by Lewis Carroll (1865) 2: Peter Pan by J. M. Barrie (1911) Which book would you like to R-E-A-D? (0 to skip) ---> 1 it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge. In another moment down went Alice after it, never once considering how in the world she was to get out again. The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well. Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she (showing...page 2 out of 167, lines 21-40 out of 3334) enter (continue); q (quit); p (previous); b (bookmark): q ---------------------------------------- Would you like to: 1) Buy/See available books 2) See owned books 3) Read a book 4) Exit ---> 4 $
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.