Python 3 Writing Classes: I need help writing three Python classes. They are sho
ID: 3826643 • Letter: P
Question
Python 3 Writing Classes:
I need help writing three Python classes. They are shown below. It's a bit of a long one so if you're up to the task, go for it. Thank you!
Train class Instance variables: the name of the train String name the maximum number of passengers the train can transport max passengers the number of passengers currentl on the train num passengers speed fps how fast the train travels (in feet per second) Methods name, max passengers, speed fps) constructor, initializes all instance variables, init (self, num passengers should default to 0 when a train is created with the constructor o Assumptions speed fps and max passengers will always be a non-negative number name will be a non-empty string (self) returns string representation with this format str Train named Orient Express with 5 passengers will travel at 10.20mph Notes: The quotes in the example are just our string boundaries! The first character in the above example is T This method reports num passengers (NOT max passengers) Train speed is converted to mile per hour and always presented with 2 digits after the decimal point. time to travel(self, distance feet) Returns how long (in whole seconds) this train will take to travel distance feet (distance in feet) o Notes and Assumptions This always returns an integer- you could use integer division or normal division followed by int conversion to truncate the fractional part. You can assume that distance feet is always a non-negative integer load passengers(self, num people) Attempts to put num people onto the train and update num passengers. If the number of people supplied would not fit on the train, raise a TraincapacityException and do not change the number of passengers currently on the train. [You may imagine this as "the passengers get into a fight and no one is allowed on the train".J o Note and Assumptions: You can assume that num people is always a non-negative integer. When raising the exception, the number to raise is the number of people that cannot fit on the train (not the number of people that try to board) This function should return None. Remember: None is the default return if you don't return anything! You don't need to explicitly return None unload passengers(self, num people) Attempts to remove num people from the train. If the number of people supplied is larger than the number of people on the train currently, raise a TraincapacityException and do not change the number of passengers currently on the train. [You may imagine this as the passengers are so confused by the request that they just stay where they are looking confused" o Note and Assumptions: You can assume that num people is always a non-negative integer When raising the exception, the number to raise is the number of people that cannot unload, not the number of people that try to exit the train This function should return None. Remember: None is the default return if you don't return anything! You don't need to explicitly return NoneExplanation / Answer
It was indeed length but enjoyable journey (;)). Don't worry about indentation, I have given link of my code so that you can copy it from there.
Do give a thumbs up. Also if instrcutor has provided a tester code then can you give a online link for the code (may be paste it to pastebin) so that I can check if there are any error.
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
# pastebin code link: https://pastebin.com/4Uk1C9X8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.