In Python: What\'s Allowed? We\'ve finally learned a substantial bit of programm
ID: 3825098 • Letter: I
Question
In Python:
What's Allowed? We've finally learned a substantial bit of programming! On this assignment, don't import anything except math, other than that you should be able to use any built-ins that you want, the project focuses on class structure, so there's not likely to be things that would make for any shortcuts. Procedure Complete the class definitions as described, you can test your code with the included testing file: Invoke it as with prior assignments: python3 tester6p.py yourcode.py You can also test individual parts of the code, but note that you either want to type a class name or the name of a batch of test cases. You can either use the class name to test init str ,and eq (any "basic functionality' tests), or the other method names from Train, City, and Journey classes python tester6p.py yourcode.py Train python tester6p.py yourcode.py unload passengers python tester6p.py yourcode.py Train City Journey add destination In addition to method and class names, these three names can be used to test exception behavior of relevant methods/classes: unload passengers exception, load passengers exception, and journey type error. You can also run your code in interactive mode python3 i yourcode.pyExplanation / Answer
Please find a link for the code at end from where you can easily copy without indentation issue.
import math
class TrainCapacityException(Exception):
def __init__(self, number, issue="full"):
self.number = number
self.issue = issue
def __str__(self):
if self.issue == "full":
return str(self.number) + " passengers cannot be loaded because the train is full!"
else:
return str(self.number) + " passengers cannot be unloaded because the train is empty!"
class Train:
def __init__(self, name, max_passengers, speed_fps):
self.name = name
self.max_passengers = max_passengers
self.num_passengers = 0
self.speed_fps = speed_fps
def __str__(self):
train = "Train named " + self.name + " with " + str(self.num_passengers)
speed_mps = 0.681818*self.speed_fps
train += " passengers will travel at " + "{0:.2f}".format(speed_mps) + "mph"
return train
def time_to_travel(self, distance_feet):
return distance_feet//self.speed_fps
def load_passengers(self, num_people):
if self.num_passengers + num_people <= self.max_passengers:
self.num_passengers += num_people
else:
over_capacity = (self.num_passengers + num_people) - self.max_passengers
raise TrainCapacityException(over_capacity)
def unload_passenger(self, num_people):
if self.num_passengers - num_people >= 0:
self.num_passengers -= num_people
else:
under_capacity = num_people - self.num_passengers
raise TrainCapacityException(under_capacity, "empty")
class City:
def __init__(self, name, loc_x, loc_y, stop_time):
self.name = name
self.loc_x = loc_x
self.loc_y = loc_y
self.stop_time = stop_time
def __str__(self):
result = self.name + " (" + str(self.loc_x) + "," + str(self.loc_y) + ")."
minutes = self.stop_time/60.0
result += "Exchange time: " + "{0:.2f}".format(minutes) + " minutes"
return result
def __eq__(self, other):
return (self.loc_x == other.loc_x) and (self.loc_y == other.loc_y)
def distance_to_city(self, city):
distance = math.sqrt((self.loc_x - city._loc_x)**2 + (self.loc_y - city._loc_y)**2)
return distance
class Journey:
def __init__(self, train, destinations=None, start_time=0):
if not isinstance(train, Train):
raise TypeError("train should be instance of Train")
self.train = train
if not destinations:
self.destinations = []
else:
if not type(destinations) is list:
raise TypeError("destinations should be list")
for city in destinations:
if not isinstance(city, City):
raise TypeError("city should be instance of City")
self.destinations = destinations
self.start_time = start_time
def city_departure_time(self, city):
if not city in self.destinations:
return
time = self.start_time
departure_time = time
for destination_city in self.destinations:
if city == destination_city:
departure_time = time
time = time + city.stop_time
return departure_time
def total_journey_time(self):
if not self.destinations:
return 0
return self.city_departure_time(self.destinations[-1]) - self.start_time
def all_passengers_accomodated(self, unload_list, load_list):
for i in range(0, len(self.destinations)):
if unload_list[i] != 0:
try:
train.unload_passenger(unload_list[i])
except:
return False
if load_list[i] != 0:
try:
train.load_passenger(load_list[i])
except:
return False
return True
# pastebin link for code : https://pastebin.com/yNL6C3dZ
# Can you please provide tester code in a copyable format (may be upload to pastebin and comment here) so that i can test it fully and fix any bug if its there.
# please rate if this helped you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.