Your cart is currently empty!
Assignment Objectives This lab assignment will give you some practice working with classes and objects. Getting Started Visit Piazza and download the “bare bones” file lab10.py onto your computer. Open lab10.py in PyCharm and fill in the following information at the top: your first and last name as they appear in…
Assignment Objectives
This lab assignment will give you some practice working with classes and objects.
Getting Started
Visit Piazza and download the “bare bones” file lab10.py onto your computer. Open lab10.py in PyCharm and fill in the following information at the top:
Submit your final lab10.py file to Blackboard by the due date and time. Late work will not be graded. Code that crashes and cannot be graded will earn no credit.
Preliminaries
Throughout this lab you will be working with the class given below, which defines the characteristics of a package being shipped in the mail:
class Package:
def init (self, sender, recipient, cost=0, distance=0):
self.sender = sender self.recipient = recipient self.cost = cost self.distance = distance
sender and recipient are strings that specify the cities where the sender and recipient of the package live, respectively. cost is the cost to ship the package, and distance is the distance that the package traveled.
This class definition is available in the file package.py. DO NOT CHANGE THE CONTENTS OF THAT FILE.
Part I: Shipping Packages (20 points)
Complete the function shipping cost(), which takes two arguments, in this order:
Value | Meaning |
cost schedule[0] | the cost to ship a package < 100 miles |
cost schedule[1] | the cost to ship a package ≥ 100 miles, but < 300 miles |
cost schedule[2] | the cost to ship a package ≥ 300 miles, but < 500 miles |
cost schedule[3] | the cost to ship a package ≥ 500 miles |
The function has two purposes:
You may assume that packages always contains at least one Package object.
Example:
Function call:
shipping_cost([Package(“Monmouth”, “Appleby”, cost=0, distance=144), Package(“Larkinge”, “Ballachulish”, cost=0, distance=65), Package(“Malrton”, “Auchtermuchty”, cost=0, distance=872), Package(“Monmouth”, “Anghor Thom”, cost=0, distance=937)], [10, 16, 37, 49])}
Return value: 124
Updated packages list:
[Package(“Monmouth”, “Appleby”, cost=16, distance=144), Package(“Larkinge”, “Ballachulish”, cost=10, distance=65), Package(“Malrton”, “Auchtermuchty”, cost=49, distance=872), Package(“Monmouth”, “Anghor Thom”, cost=49, distance=937)]
Part II: Tracking Packages (20 points)
Complete the function package tracking(), which takes three arguments, in this order:
Value | Meaning |
cost schedule[0] | the cost to ship a package < 100 miles |
cost schedule[1] | the cost to ship a package ≥ 100 miles, but < 300 miles |
cost schedule[2] | the cost to ship a package ≥ 300 miles, but < 500 miles |
cost schedule[3] | the cost to ship a package ≥ 500 miles |
The function iterates over the packages list and incrementally builds a list of Package objects that represent packages being sent with the given senders and recipients. For each new Package object that the function creates, the function consults the location dictionary to help it compute the distance that each package will travel. The function can then set the distance field of each Package object accordingly. Next, using the cost schedule list, the function sets the cost field of each Package object. Finally, the function returns the list of Package objects it created.
We will use the 2D Euclidean distance as the distance between two cities. (This isn’t accurate because the Earth is round, but it’s OK!) Given the coordinate positions (x1, y1 ) and (x2, y2 ) of two points, the distance between the
points is given as p(x1 − x2 )2 + (y1 − y2)2.
Example: Function call: package_tracking(
[(’Appleby’, ’Satbury’),
(’Northpass’, ’Berkton’), (’Eanverness’, ’Satbury’)],
{’Appleby’: (58, 189), ’Berkton’: (84, 13),
’Ballachulish’: (12, 149), ’Aerilon’: (28, 77),
’Garennton’: (173, 68), ’Malrton’: (124, 133),
’Peltragow’: (194, 181), ’Paentmarwy’: (191, 151),
’Eanverness’: (134, 50), ’Satbury’: (25, 181),
’Bracklewhyte’: (47, 124), ’Larkinge’: (8, 157),
’Burnsley’: (71, 5), ’Erith’: (2, 181),
’Monmouth’: (160, 13), ’Northpass’: (84, 45),
’Jedborourgh’: (87, 163), ’Anghor Thom’: (109, 10),
’Auchtermuchty’: (140, 189), ’Murrayfield’: (164, 83)},
[18, 21, 24, 48]
)
Returned list:
[Package(“Appleby”, “Satbury”, cost=18, distance=33.95585369269929), Package(“Northpass”, “Berkton”, cost=18, distance=32.0), Package(“Eanverness”, “Satbury”, cost=21, distance=170.41713528867922)]
Notes:
Consider the Package class again for a moment:
class Package:
def init (self, sender, recipient, cost=0, distance=0):
self.sender = sender self.recipient = recipient self.cost = cost self.distance = distance
We note that the constructor ( init ()) has two default arguments for the cost and distance attributes. In practice this means that we can construct a Package object with only two attributes and set the cost and distance attributes later:
new_package = Package(’New York’, ’Los Angeles’)
# … other code here, possibly … and then:
new_package.cost = 50 new_package.distance = 3000
or we can construct the object with all 4 attributes at once, if we like:
new_package = Package(’New York’, ’Los Angeles’, 50, 3000)
You can use either approach; both are acceptable.
How to Submit Your Work for Grading
To submit your .py file for grading:
.py file.
Oops, I messed up and I need to resubmit a file!
No worries! Just follow the above directions again. We will grade only your last submission.