Procedure Complete the class definitions as described; you can test your code wi
ID: 3835072 • Letter: P
Question
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 classname 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 oumey classes: python3 tester6p.py yourcode.py Train python3 tester6p.py yourcode.py unload passengers python 3 tester6p.py yourcode.py Train city Journey add destination o 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: python 3 -i yourcode.py Scenario You will create world with cities, trains, and exciting journeys!This will require some proscribed class definitions. Create all the classes as defined below, all in your single py file. Some of the classes build upon each other this is an example of aggregation. Many of the methods required below are very regular-after you've seen one Linit method on this program, you've sort of seen them all. While there might be a bit more typing than usual, the amount ofmental work to "solve the puzzle" should be a bit minimal once you get used to the patterns. Classes You will be writing four classes (Train, City. Journey, and TrainCapacityException) Make sure you understand what these represent and how they relate to each other before continuing. Train class: Instance variables: string the name of the train the maximum number of passengers the train can transport max passengers num passengers int the number of passengers currently on the train int speed fps how fast the train travels (in feet per second) Methods: init (self, name, max passengers, speed fps) constructor, initializes all instance variables, num passengers should default to 0 when a train is created with the constructor. o Assumption speed fps and ax passengers will always be a non-negative number name will be anon-empty stringExplanation / Answer
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_passengers(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 i in range(1, len(self.destinations)):
time = time + self.destinations[i-1].stop_time
if city == self.destinations[i-1]:
departure_time = time
time = time +int((self.destinations[i].distance_to_city(self.destinations[i-1]))/self.train.speed_fps)
if self.destinations[-1] == city:
departure_time = time + city.stop_time
return departure_time
def city_arrival_time(self, city):
if not city in self.destinations:
return
time = self.start_time
arrival_time = time
for i in range(1, len(self.destinations)):
if self.destinations[i-1] == city:
return arrival_time
arrival_time = arrival_time + int((self.destinations[i].distance_to_city(self.destinations[i-1]))/self.train.speed_fps) + self.destinations[i-1].stop_time
if self.destinations[-1] == city:
return arrival_time
return
def add_destination(self,c):
self.destinations.append(c)
def city_in_journey(self,c):
return c in self.destinations
def check_journey_includes(self, start_city, dest_city):
found_start = False
for dest in self.destinations:
if found_start and dest == dest_city:
return True
if dest==start_city:
found_start = True
return False
def total_journey_time(self):
if not self.destinations:
return 0
return self.city_departure_time(self.destinations[-1]) - self.start_time
def total_journey_distance(self):
if len(self.destinations) < 2:
return 0
distance = 0
for i in range(1, len(self.destinations)):
distance += self.destinations[i].distance_to_city(self.destinations[i-1])
return distance
def all_passengers_accommodated(self, unload_list, load_list):
for i in range(0, len(self.destinations)):
if unload_list[i] != 0:
try:
self.train.unload_passengers(unload_list[i])
except:
return False
if load_list[i] != 0:
try:
self.train.load_passengers(load_list[i])
except:
return False
return True
def __str__(self):
result = "Journey with " + str(len(self.destinations)) + " stops: "
for destination in self.destinations:
result += " " + str(destination) + " "
result += "Train Information: " + str(self.train) + " "
return result
# pastebin link for code : https://pastebin.com/rc817r8D
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.