Programming代写 | Constructor | function代写 | code

0 Comments

ICT162

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.

IMPORTANT NOTE

ECA Submission Deadline: Sunday, 12 May 2019, 2355 hours

Page 1 of 15

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.

Submission

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)

Question 1

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 lossSurchargeRate.

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.

Two examples are shown below:

ISBN: 9781234567890 Title: Python For Beginners Price: $29.99

Authors: Tom Smart, Alice Alanis
Year Published: 2019

ISBN: 9781234567891 Title: Fun With Python Price: $10.00

Author: Tom Smart

Year Published: 2018

Use the header Author if there is only one author, and Authors if otherwise.
(11 marks)

(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.

Two examples are shown below:

ISBN: 9781234567890 Title: Python For Beginners Price: $29.99
Authors: Tom Smart, Alice Alanis

Year Published: 2019 Edition: 2

ISBN: 9781234567891 Title: Fun With Python Price: $10.00

Author: Tom Smart
Year Published: 2018 Edition: 1

(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.

An example is shown below:

Item 1:

ISBN: 9781234567890 Title: Python For Beginners Price: $29.99
Authors: Tom Smart, Alice Alanis

Year Published: 2019 Edition: 2

Item 2:

ISBN: 9781234567891 Title: Fun With Python Price: $10.00
Author: Tom Smart
Year Published: 2018 Edition: 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.

(9 marks)

ICT162 Copyright © 2019 Singapore University of Social Sciences (SUSS)
ECA – January Semester 2019

Page 5 of 15

(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

(4 marks)

(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.

(9 marks)

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.

Question 2

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.

(1 mark)

(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:

Page 7 of 15

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
(14 marks)

(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.

(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.

oIf 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

oIf the operation is successful, the function prints the passenger detail and the message yy operation is successful on separate lines.
(9 marks)

An example run (characters in green are user input):

Passenger Id: T1234X None

Menu
1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: abc

Error in choice. Please enter a whole number 0 to 4
*** same error message for all non-numbers and non whole numbers: ***

Error in choice. Please enter a whole number 0 to 4

Passenger Id: T1234X None

Menu
1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass

0.Exit
Enter choice: -1

Invalid choice: -1
*** same error message for all whole numbers no between 0 and 4 where xx is user input: ***

Invalid choice: -xx

Passenger Id: T1234X None

Menu
1.Get Travel Card

2.Get refund

3.Make a trip
4.Top up Travel Pass

0.Exit
Enter choice: 2

Get refund operation is unsuccessful.
Reason: No travel pass! Please get a travel pass

*** choice 3 has the error message when passenger has no travel pass ***
Make a trip operation is unsuccessful.
Reason: No travel pass! Please get a travel pass
*** choice 4 has error message when passenger has no travel pass ***

Top up Travel Pass operation is unsuccessful. Reason: No travel pass! Please get a travel pass

Passenger Id: T1234X None

Menu
1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass

0.Exit
Enter choice: 1

Enter amount: abc
Error in amount. Please enter a positive decimal number
*** same error message for all invalid amount such as -1 and 0 and non-numbers: ***

Error in amount. Please enter a positive decimal number

Passenger Id: T1234X None
Menu

  1. Get Travel Card

2.Get refund

3.Make a trip
4.Top up Travel Pass
0.Exit
Enter choice: 1

Enter amount: 10

Insufficient amount to Get Travel Card operation. Reason: Minimum amount to buy a pass is $20.00

Passenger Id: T1234X None

Menu
1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass

0.Exit
Enter choice: 1

Enter amount: 25 Change = $5.00

Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 10 Get Travel Card operation is successful.

Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 10 Menu

1.Get Travel Card
2.Get refund

3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: 1
Get Travel Card operation is unsuccessful.

Reason: Please use existing 10 trips in current pass or get a refund first

Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 10 Menu
1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: 4

Enter amount: 10

Insufficient amount to Top up Travel Pass operation. Reason: Min amount to top up is $15.00

Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 10 Menu

1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: 4
Enter amount: 22

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

Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 20 Top up Travel Pass operation is successful.

ICT162 Copyright © 2019 Singapore University of Social Sciences (SUSS)
ECA – January Semester 2019

Page 11 of 15

Passenger Id: T1234X Travel Pass: 0000001 Number of trips left: 20 Menu

1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: 2

Refund amount at $1.20 per trip: $24.00
Passenger Id: T1234X None

Get refund operation is successful.

Passenger Id: T1234X None
Menu

1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit
Enter choice: 1

Enter amount: 20 Change = $0.00

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 10 Get Travel Card operation is successful.

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 10 Menu

1.Get Travel Card
2.Get refund

3.Make a trip
4.Top up Travel Pass

0.Exit
Enter choice: 3

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left:  9

Make a trip operation is successful.
*** similar output for choice 3 repeatedly until unconsumed trips is 0 ***

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 0 Menu

1.Get Travel Card
2.Get refund

3.Make a trip
4.Top up Travel Pass

0.Exit

Enter choice: 3

Make a trip operation is unsuccessful. Reason: Travel pass has 0 trips left

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 0 Menu

1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: 2

Get refund operation is unsuccessful. Reason: There is no trip refund

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 0 Menu

1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass
0.Exit

Enter choice: 1

Enter amount: 35
To top up instead, enter T. Enter any key to purchase new pass: t

Before top up:  Travel Pass: 0000002 Number of trips left:  0

After 2 unit top up: Travel Pass: 0000002 Number of trips left: 20 Change = $5.00

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 20 Get Travel Card operation is successful.

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 20 Menu

1.Get Travel Card

2.Get refund
3.Make a trip

4.Top up Travel Pass

0.Exit
Enter choice: 3

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 19
Make a trip operation is successful.
*** similar output for choice 3 repeatedly until unconsumed trips is 0 ***

Passenger Id: T1234X Travel Pass: 0000002 Number of trips left: 0 Menu

1.Get Travel Card

2.Get refund

3.Make a trip
4.Top up Travel Pass

0.Exit
Enter choice: 1

Enter amount: 20

To top up instead, enter T. Enter any key to purchase new pass: Change = $0.00

Passenger Id: T1234X Travel Pass: 0000003 Number of trips left: 10 Get Travel Card operation is successful.

Passenger Id: T1234X Travel Pass: 0000003 Number of trips left: 10 Menu

1.Get Travel Card
2.Get refund

3.Make a trip
4.Top up Travel Pass

0.Exit
Enter choice: 0

Application Ended

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.

ICT162 Copyright © 2019 Singapore University of Social Sciences (SUSS)
ECA – January Semester 2019

Page 13 of 15

Question 3

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.

(11 marks)

(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.

(3 marks)

(c)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.

(4 marks)

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.

Comments