End-of-Course Assessment – January Semester 2019
Object Oriented Programming
INSTRUCTIONS TO STUDENTS:
1. This End-of-Course Assessment paper contains THREE (3) questions and comprises
FIFTEEN (15) pages (including the cover page).
2. You are to include the following particulars in your submission: Course Code, Title of the ECA, SUSS PI No., Your Name, and Submission Date.
3. Late submission will be subjected to the marks deduction scheme. Please refer to the Student Handbook for details.
ECA Submission Guidelines
Please follow the submission instructions stated below:
This ECA carries 70% of the course marks and is a compulsory component. It is to be done individually and not collaboratively with other students.
You are to submit the ECA assignment in exactly the same manner as your tutor-marked assignments (TMA), i.e. using Canvas. Submission in any other manner like hardcopy or any other means will not be accepted.
Electronic transmission is not immediate. It is possible that the network traffic may be particularly heavy on the cut-off date and connections to the system cannot be guaranteed. Hence, you are advised to submit your assignment the day before the cut-off date in order to make sure that the submission is accepted and in good time.
Once you have submitted your ECA assignment, the status is displayed on the computer screen. You will only receive a successful assignment submission message if you had applied for the e-mail notification option.
ECA Marks Deduction Scheme
Please note the following:
a) Submission Cut-off Time – The cut-off time for ECA submission will be at 2355 hrs on the day of the deadline. All submission timings will be based on the time recorded by Canvas.
b) Start Time for Deduction – Students are given a grace period of 12hours. Hence calculation of late submissions of ECAs will begin at 12:00 noon the following day (this applies even if it is a holiday or weekend) after the deadline.
c) How the Scheme Works – From 12:00 noon the following day after the deadline, 10 marks will be deducted for each 24-hour block. Submissions that are subject to more than 50 marks deduction will be assigned zero mark. For examples on how the scheme works, please refer to Section 5.2 Para 1.7.3 of the Student Handbook.
Any extra files, missing appendices or corrections received after the cut-off date will also not be considered in the grading of your ECA assignment.
Plagiarism and Collusion
Plagiarism and collusion are forms of cheating and are not acceptable in any form of a student’s work, including this ECA assignment. You can avoid plagiarism by giving appropriate references when you use some other people’s ideas, words or pictures (including diagrams). Refer to the American Psychological Association (APA) Manual if you need reminding about quoting and referencing. You can avoid collusion by ensuring that your submission is based on your own individual effort.
The electronic submission of your ECA assignment will be screened through a plagiarism detecting software. For more information about plagiarism and cheating, you should refer to the Student Handbook. SUSS takes a tough stance against plagiarism and collusion. Serious cases will normally result in the student being referred to SUSS’s Student Disciplinary Group. For other cases, significant marking penalties or expulsion from the course will be imposed.
Note to Students:
Submit your solution document in the form of a single MS Word file. You are to include the following particulars in your submission: Course Code, Title of the ECA, SUSS PI No., Your Name, and Submission Date. Put this information in the first page of your solution document. Use the template word document provided – SUSS_PI-No_FullName_ECA.docx. Rename the file with your suss PI and full name join with “_ECA” e.g. “SUSS_PI- TomTanKinMeng_ECA.docx” (without the quotes). Do NOT submit as a pdf document.
You should make only one submission for ECA.
You are to copy and paste the source codes of each program you write into your solution document, provided in the template word document. If you submit the source codes as a screenshot, you will be penalized. Submit screenshots for only the output of your program, where applicable.
Answer all questions. (Total 100 marks)
A library wishes to allow members to search for library items based on title, keywords and authors, and to make enquiry on fines for items should they be returned late.
Write the following classes that construct the necessary class hierarchy and associations for the library application:
(a) Item class
This class is an abstract superclass.
·There are five object attributes: isbn, title, price, authors and year of publication. Write a constructor for the class. The constructor has five parameters. Make every word in the title start with an uppercase letter. You should choose an appropriate collection type for author(s).
·There is one class attribute: an administrative charge of $30.
·Write the accessor and mutator methods for this class attribute.
·Write the accessor and mutator methods for the object attribute price.
·Write only accessor methods for all other object attributes. The accessor method for the object attribute authors should return a string containing the authors, separated by commas (,).
·Write TWO (2) abstract methods: getLateCharges and
getLateCharges method has one parameter: number of days.
lossSurchargeRate method does not have parameter.
·Write the method lossPenalty which returns the sum of 3 values: the item price, the loss surcharge (or loss surcharge rate × item price) and the administrative charge.
·Write the method matchKeyWords which has a parameter: a list of keywords. The method returns true if the title contains all the keywords.
· Write the method isWrittenBy which has a parameter: an author. The method returns true if the author is the author or one of the authors.
·Write a string method that returns a representation of the item as a string.
(b) Book class.
The Book class is a subclass of the Item class.
· There are six object attributes: isbn, title, price, authors, year of publication and edition. Write a constructor for the class. The constructor has six parameters.
· There is one class attribute: loss surcharge rate which is 0.2 (that is, 20%). Write the getter and setter method for the class attribute.
· Implement the abstract method getLateCharges defined in the superclass. The method returns the late charges in dollars. The fine per day is 20 cents for the first 7 days, 50 cents per day for the next 7 days (day 8 to day 14), and $1 per day for subsequent days (day 15 onwards). The fines should not exceed the loss penalty.
·Write the method lossSurchargeRate which returns the value of class attribute for loss surcharge rate.
·Write a string method to return a string representation of the book.
(c) Library class
The Library class has a collection of items such as books.
·There is one object attribute: the collection of items.
Write a constructor for the class. The constructor does not have any parameter.
·Write the method searchItem which has a parameter: the isbn of an item (a unique number). The method returns the item if there is an item with the matching isbn. Otherwise, the method returns None.
· Write the method addItem which has a parameter: an item. The method adds the item to the collection if there is currently no item in the collection with the same isbn. The method returns true if the add is successful and false otherwise.
· Write the method removeItem which has a parameter: an isbn of an item. The method removes the item from the collection if there is an item in the collection with the isbn. The method returns true if the remove is successful and false otherwise.
·Write a class method showResult. This method has one parameter: a collection of items. The method returns a string representation of the items, including the item position starting from 1.
·Write the methods itemsByAuthor, itemsWithTitle, itemsWithKeywords and listItems.
The method itemsByAuthor has a parameter: an author. It locates the items that are written by the author, and returns a string representation of these items. The method itemsWithTitle has a parameter: a title. It locates items with the title, and returns a string representation of these items.
The method itemsWithKeywords has a parameter: a list of keywords. It locates items whose title contains all the keywords, and returns a string representation of these items.
The method listItems does not have any parameter. It returns a string representation of all the items in the library.
The methods itemsByAuthor, itemsWithTitle, itemsWithKeywords
and listItems must call the method showResult.
(d) Write a main function to test your classes. The main function should do the following:
#create a Library object
#create 2 Book objects with these details add them to the library
# First Book ISBN: 9781234567890 Title: Python For Beginners Price: $29.99 # Authors: Tom Smart, Alice Alanis Year Published: 2019 Edition: 2
# Second Book ISBN: 9781234567891 Title: Fun With Python Price: $10.00 # Author: Tom Smart Year Published: 2018 Edition: 1
#display the list of items
#search and display items with title Fun with python #search and display items with author Tom Smart #search and display items with keywords fun and python #search and display items with keyword python
# remove the item with isbn 9781234567890
# display the late charges when the book with isbn 9781234567891 is returned # 1, 8, 19 and 100 days late
(e) Use examples from your solutions to parts a) to d) to explain and illustrate how to apply the concepts of THREE (3) SOLID principles.
NOTE TO STUDENTS: Copy and paste the Python code for classes Item, Book and Library and the function main IN THIS ORDER. Write your answer to Q1(e) in the word document. Copy and paste the screenshot showing the output from running the function main.
The cost of a travel pass includes a $5 administrative charge and the cost of one unit of trips (or 10 trips) at $1.50 per trip. A passenger who has a travel pass can perform these operations:
·make trips as long as there is at least one unconsumed trip,
·top up in multiples of units of trips without incurring administrative charge
·get a refund at $1.20 per unconsumed trip.
When a refund is made, the travel pass is surrendered. Thus, the passenger must repurchase a travel pass if he subsequently wishes to make a trip.
The following describe the structure of the classes. To demonstrate how classes can be used to organize information, write the following classes and an application that provides a menu to ONE (1) passenger.
(a) InsufficientAmountException, InputError and TravelException, classes.
InsufficientAmountException and InputError are subclasses of
TravelException. These classes do not require any additional attributes or methods.
An exception from the InsufficientAmountException class is raised whenever the amount tendered to buy or to top up a travel pass is insufficient.
An exception from InputError class is raised whenever the user enters an invalid value when prompted for a menu option or when prompted for an amount for buying or for topping up a travel pass.
An exception from TravelException class is raised whenever these requests cannot be carried out: purchase travel pass, top up, get refund and make a trip.
(b) TravelPass class
The class attributes of this class are cost of a trip ($1.50), the number of trips (10) sold as a unit, the refund amount per unconsumed trip ($1.20), and the administration charge ($5). You can introduce any other necessary class attribute(s) where necessary.
Write the class methods:
· getNumberOfTripsPerUnit that returns the number of trips in one unit
· getCostPerUnit that returns the cost of one unit of trips.
· costOfTravelPass that returns sum of the cost of a unit and the administration charge.
·getRefundPerTrip that returns the refund amount per unconsumed trip
You must call the class methods for the values in the class attributes as the values in the class attributes may change. Do not hardcode their values in your processing or output statements.
There are two object attributes: travel pass id and the number of unconsumed trips. Write Python code to complete the class definition for TravelPass.
· constructor with a parameter: an amount in dollars. The constructor raises an InsufficientAmountException exception with the message Minimum amount to buy a pass is $20.00 if the amount is less than the cost of a travel pass. The travel pass id is auto-generated. The id of first travel pass is id 1, the second is 2, and so on.
·getter method for the number of unconsumed trips.
· method makeTrip that raises an TravelException exception with the message Travel pass has 0 trips left if there is no unconsumed trip left. Otherwise, the method decrements the number of unconsumed trip(s) by 1.
· method topup with a parameter: the amount to top up. The method raises an InsufficientAmountException exception with the message Min amount to top up is $15.00 if the amount is less than the cost of a unit of trips. If the amount is sufficient, the method computes the number of units possible, and increases the number of unconsumed trips by the number of trips in the units. The method returns a string containing the travel pass details before and after the top up, as well as the amount of change. For example, if the amount is 22 and the travel pass has 10 unconsumed trips, the method returns the following string:
Before top up: Travel Pass: 0000001 Number of trips left: 10 After 1 unit top up: Travel Pass: 0000001 Number of trips left: 20 Change = $7.00
·method getRefund that raises an TravelException exception with the message There is no trip refund if there is no unconsumed trip left. Otherwise, the method computes the refund amount of all unconsumed trips and sets the number of unconsumed trips to zero. It returns the refund amount.
· method str that returns a string representation of the travel pass. An example is shown here:
Travel Pass: 0000001 Number of trips left: 20
(c) Passenger class
The object attributes of this class are: passenger id and a travel pass. Write Python code for the class definition for Passenger:
· constructor with two parameters: passenger id and a travel pass with default value None. A passenger may be created with or without a travel pass.
·getter method for the travel pass using the property decorator.
·method hasTravelPass that returns True if the passenger has a travel pass and False otherwise.
·method getTravelPass with a parameter amount. The method raises an TravelException exception with the message Please use existing xx trips in current pass or get a refund first if there are xx unconsumed trips left in the passenger has a travel pass with unconsumed trips. Otherwise, it creates a travel pass for him with amount.
·method topup with a parameter amount. The method raises an TravelException exception with the message No travel pass! Please get a travel pass if the passenger does not have a travel pass. Otherwise, the travel pass is topped up with the amount and the result of the top up operation is returned. The method does not handle exception.
· method refund that raises an TravelException exception with the message No travel pass! Please get a travel pass if the passenger does not have a travel pass. Otherwise, the method gets the refund amount of the travel pass, sets the travel pass of the passenger to None, and returns the refund amount. The method does not handle exception.
·method makeTrip that raises an TravelException exception with the message No travel pass! Please get a travel pass if the passenger does not have a travel pass. Otherwise, the travel pass is used to make trip. The method does not handle exception.
·method str that returns a string representation of the passenger and his travel pass. An example is shown here:
Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 10
(d) Write the following functions with relevant parameter(s), if necessary for the application:
· function getMenuOption that displays a menu and prompts the passenger for a choice. If the passenger enters non-numeric value non-whole numbers, the function raises an InputError exception with the message Error in choice. Please enter a whole number 0 to 4. The function returns the passenger’s choice as a whole number.
·function getAmount that prompts the passenger for an amount. If the passenger enters a non-numeric value or a number not greater than 0, it raises an InputError exception with the message Error in amount. Please enter a positive decimal number. The function returns the amount as a decimal number.
· function getTravelPass that raises a a TravelException exception with the message Please use existing xx trips in current pass or get a refund first if there is xx unconsumed trip left in the passenger’s travel pass. Otherwise, the function prompts the passenger for an amount. If the passenger has a travel pass with no unconsumed trips, the function prompts the passenger whether he wishes to top up instead. If so, it invokes a top up for the passenger. Otherwise, the function gets a travel pass for the passenger and displays the amount of change.
· function topup that raises a TravelException exception with the message No travel pass! Please get a travel pass if the passenger does not have a travel pass. Otherwise, the function prompts the passenger for an amount and invokes a top up for the passenger.
· function main that creates a passenger with id T1234X but without a travel pass. The function displays a menu repeatedly for the passenger to choose an operation until he chooses to exit.
For the following descriptions for function main, note that xx in the output message refers to the whole number that the passenger has entered for a menu choice, and yy refers to the operation corresponding to the menu choice. For example, if xx is 1, then yy is Get Travel Card.
If the passenger enters an invalid option, that is, not between 0 and 4, print the message Invalid choice: xx If the choice is valid, the function calls the relevant functions to perform the selected operation, and handles these exceptions:
· InputError by simply printing the exception message
·InsufficientAmountException by appending a prefix Insufficient amount to yy operation. nReason: to the exception message
· TravelException by appending a prefix yy operation is unsuccessful.nReason: to the exception message
o If the operation is successful, the function prints the passenger detail and the message yy operation is successful on separate lines.
NOTE TO STUDENTS: Copy and paste all required Python code for parts Q2(a), Q2(b), Q2(c) and Q2(d) to the word document. Copy and paste ONE (1) screenshot to show a test run of the functions and methods in your application.
Develop a GUI using tkinter framework to allow a user to enquire on the rental cost of a specific class of car according to the daily rates in Table Q3(a). For simplicity, ignore public holidays.
Implement the necessary callback function(s) for the button Enquire Cost.
(ii) When the button Clear Data is clicked, the GUI reverts to its initial state as shown in Figure Q3.1. Implement the necessary callback function(s) for the button Clear Data.
(a) Make a copy of solutions to part a) and b). To the copy, introduce modifications to your GUI so that the user will be able to see the rates for the different classes of cars. Justify the widgets that you use.
NOTE TO STUDENTS: Copy and paste the Python code for part Q3(a) and Q3(b) as ONE (1) program, and part Q3(c) as ANOTHER program. Copy and paste screenshot showing the output of one valid enquiry.
—– END OF ECA PAPER —–