Java and Python Programming Worksheet

User Generated

noqnyz1

Other

Description

Unformatted Attachment Preview

Exercise 1 A simple software system for a library models a library as a collection of books and patrons. A patron can have at most three books out on loan at any given time. A book also has a list of patrons waiting to borrow it. Each book has a title, an author, a patron to whom it has been checked out, and a list of patrons waiting for that book to be returned. Each patron has a name and the number of books it has currently checked out. Develop the classes Book and Patron to model these objects. Think first of the interface or set of methods used with each class and then choose appropriate data structures to maintain the data/state of the objects. Also, write a driver program to test these classes. Here is an example of a driver program: from book import Book from patron import Patron def main(): book1 = Book("Of Mice and Men", "Steinbeck") book2 = Book("The Great Gatsby", "Fitzgerald") book3 = Book("1984", "Orwell") book4 = Book("One Flew Over the Cuckoo's Nest", "Kesey") patron1 = Patron("Ivan") patron2 = Patron("Jimmy") patron3 = Patron("Bob") book1.borrow(patron1) patron1.addBook(book1) book1.borrow(patron2) book1.borrow(patron3) book2.borrow(patron1) patron1.addBook(book2) book3.borrow(patron1) patron1.addBook(book3) patron1.addBook(book4) book4.borrow(patron2) patron2.addBook(book4) print("Book1 : " + str(book1)) print("Patron1: " + str(patron1)) if __name__ == '__main__': main() and the associated output: Can't borrow more books--MAX REACHED! //This occurred when patron1 tried to add a 4th book Book1: Of Mice and Men, Steinbeck in care of: Ivan has 3 books Waiting: 1. Jimmy has 1 books 2. Bob has 0 books Patron1: Ivan has 3 books Exercise 1 A simple software system for a library models a library as a collection of books and patrons. A patron can have at most three books out on loan at any given time. A book also has a list of patrons waiting to borrow it. Each book has a title, an author, a patron to whom it has been checked out, and a list of patrons waiting for that book to be returned. Each patron has a name and the number of books it has currently checked out. Develop the classes Book and Patron to model these objects. Think first of the interface or set of methods used with each class and then choose appropriate data structures to maintain the data/state of the objects. Also, write a driver program to test these classes. Here is an example of a driver program: from book import Book from patron import Patron def main(): book1 = Book("Of Mice and Men", "Steinbeck") book2 = Book("The Great Gatsby", "Fitzgerald") book3 = Book("1984", "Orwell") book4 = Book("One Flew Over the Cuckoo's Nest", "Kesey") patron1 = Patron("Ivan") patron2 = Patron("Jimmy") patron3 = Patron("Bob") book1.borrow(patron1) patron1.addBook(book1) book1.borrow(patron2) book1.borrow(patron3) book2.borrow(patron1) patron1.addBook(book2) book3.borrow(patron1) patron1.addBook(book3) patron1.addBook(book4) book4.borrow(patron2) patron2.addBook(book4) print("Book1 : " + str(book1)) print("Patron1: " + str(patron1)) if __name__ == '__main__': main() and the associated output: Can't borrow more books--MAX REACHED! //This occurred when patron1 tried to add a 4th book Book1: Of Mice and Men, Steinbeck in care of: Ivan has 3 books Waiting: 1. Jimmy has 1 books 2. Bob has 0 books Patron1: Ivan has 3 books Develop a Library class that can manage the books and patrons from Exercise 1. This class should include methods for adding, removing, and finding books and patrons: addBook, addPatron, removeBook, removePatron, findBook, and findPatron, respectively for each. There should also be methods for borrowing and returning a book: borrowBook and returnBook, respectively. If you have yet to add a data member to keep track of the list of books a patron has borrowed, now would be a good time to do so. Now, write a driver program to test all of these methods. Here is an example driver program: from library import Library from book import Book from patron import Patron def main(): book1 = Book("Of Mice and Men", "Steinbeck") book2 = Book("The Great Gatsby", "Fitzgerald") book3 = Book("1984", "Orwell") book4 = Book("One Flew Over the Cuckoo's Nest", "Kesey") libraryBooks = [] libraryBooks.append(book1) libraryBooks.append(book2) libraryBooks.append(book3) libraryBooks.append(book4) patron1 = Patron("Ivan") patron2 = Patron("Jimmy") patron3 = Patron("Bob") myLibrary = Library(libraryBooks) myLibrary.addPatron(patron1) myLibrary.addPatron(patron2) myLibrary.addPatron(patron3) myLibrary.borrowBook(book1, patron2) myLibrary.borrowBook(book1, patron3) print(str(myLibrary)) myLibrary.returnBook(book1) print(str(myLibrary)) if __name__ == '__main__': main() and, the associated output: Books: Of Mice and Men, Steinbeck in care of: Jimmy has 1 books. Waiting: 1. Bob has 0 books. The Great Gatsby, Fitzgerald and has not been borrowed. Waiting: 1984, Orwell and has not been borrowed. Waiting: One Flew Over the Cuckoo's Nest, Kesey and has not been borrowed. Waiting: Patrons: Ivan has 0 books. Jimmy has 1 books. Bob has 0 books. Returned: Of Mice and Men, Steinbeck in care of: Jimmy has 0 books. Waiting: 1. Bob has 0 books. Books: Of Mice and Men, Steinbeck and has not been borrowed. Waiting: 1. Bob has 0 books. The Great Gatsby, Fitzgerald and has not been borrowed. Waiting: 1984, Orwell and has not been borrowed. Waiting: One Flew Over the Cuckoo's Nest, Kesey and has not been borrowed. Waiting: Patrons: Ivan has 0 books. Jimmy has 0 books. Bob has 0 books.
Purchase answer to see full attachment
Explanation & Answer:
1 Script
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Hey check it out. Everything is attached

Develop a Library class that can manage the books and patrons from Exercise 1. This class
should include methods for adding, removing, and finding books and patrons: addBook,
addPatron, removeBook, removePatron, findBook, and findPatron, respectively for
each. There should also be methods for borrowing and returning a book: borrowBook and
returnBook, respectively. If you have yet to add a data member to keep track of the list of
books a patron has borrowed, now would be a good time to do so.
Now, write a driver program to test all of these methods.
Here is an example driver program:
from library import Library from book import Book
from patron import Patron
def main():
book1 = Book("Of Mice and Men", "Steinbeck")
book2 = Book("The Great Gatsby", "Fitzgerald")
book3 = Book("1984", "Orwell")
book4 = Book("One Flew Over the Cuckoo's Nest", "Kesey")
libraryBooks = []
libraryBooks.append(book1)
libraryBooks.append(book2)
libraryBooks.append(book3)
libraryBooks.append(book4)
patron1 = Patron("Ivan") patron2 = Patron("Jimmy") patron3 = Patron("Bob")
myLibrary = Library(libraryBooks)
myLibrary.addPatron(patron1)
myLibrary.addPatron(patron2)
myLibrary.addPatron(patron3)
myLibrary.borrowBook(book1, patron2)
myLibrary.borrowBook(book1, patron3)
print(str(myLibrary))
myLibrary.returnBook(book1)
print(str(myLibrary))
if __name__ == '__main__': main()

and, the associated output:
Books:
Of Mice and Men, Steinbeck in care of: Jimmy has 1 books. Waiting:
1. Bob has 0 books.
The Great Gatsby, Fitzgerald and has not been borrowed. Waiting:
1984, Orwell and has not been borrowed.
Waiting:

One Flew Over the Cuckoo's Nest, Kesey and has not been borrowed. Waiting:
Patrons:
Ivan has 0 books.
Jimmy has 1 books.
Bob has 0 books.
Returned: Of Mice and Men, Steinbeck in care of: Jimmy has 0 books. Waiting:
1....


Anonymous
This is great! Exactly what I wanted.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags