Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Help Scenario You will create world with cities, trains, and exciting jou

ID: 3834699 • Letter: P

Question

Python Help

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 init method on this program, you've sort of seen them all. While there might be a bit more typing than usual, the amount of mental 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 name max passengers int the maximum number of passengers the train can transport num passengers int the number of passengers currently on the train int how fast the train travels (in feet per second) speed fps 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 Assumptions: speed fps and max-passengers will always be a non-negative number name will be a non-empty string

Explanation / Answer

Answer: See the code for classes below:

---------------------------------

import math

#TrainCityException class
class TrainCityException(Exception):
    #__init__ method
    def __init__(self, number, issue="full"):
        self.number=number
        self.issue=issue
  
    #__str__ method
    def __str__(self):
        if self.issue == "full":
            return str(self.number)+"passengers cannot be loaded becasue the train is "+self.issue+"."
        elif self.issue == "empty":
            return str(self.number)+"passengers cannot be unloaded becasue the train is "+self.issue+"."
        else:
            return "TrainCapacityException"

#Train class
class Train:
    #__init__ method
    def __init__(self,name,max_passengers,speed_fps):
        self.name=str(name) #name of train
        self.max_passengers=int(max_passengers) #maximum possible passengers in the train
        self.num_passengers=0 #number of passengers currently in the train
        self.speed_fps=int(speed_fps) #speed of train in feet per second
    #__str__ method  
    def __str__(self):
        return "Train named"+self.name+"with"+str(self.num_passengers)+"passengers will travel at"+str(self.speed_fps*0.682)+"mph"
  
    #time_to_travel method
    def time_to_travel(self,distance_feet):
        return int(distance_feet/self.speed_fps)
  
    #load_passengers method
    def load_passengers(self,num_people):
        passengers=self.num_passengers+num_people
        if passengers > self.max_passengers:
            raise TrainCapacityException(num_people,"Cannot be loaded.")
        else:
            self.num_passengers=passengers
  
    #unload_passengers method
    def unload_passengers(self,num_people):
        passengers=num_people
        if passengers > self.num_passengers:
            raise TrainCapacityException(num_people,"Cannot be unloaded.")

#City class
class City:
    #__init__ method
    def __init__(self, name, loc_x, loc_y, stop_time):
        self.name=str(name) #name of city
        self.loc_x=int(loc_x) #city's x location
        self.loc_y=int(loc_y) #city's y location
        self.stop_time=int(stop_time) #time in seconds train stops in city
      
    #__str_ method
    def __str__(self):
        return self.name+"("+str(self.loc_x)+","+str(self.loc_y)+"). Exchange time:"+str(self.stop_time/60)+" minutes."

    #__eq__ method
    def __eq__(self,other):
        if self.loc_x == other.loc_x and self.loc_y == other.loc_y:
            return True
        else:
            return False

    #distance_to_city method
    def distance_to_city(self,city):
           return math.sqrt(math.abs(self.loc_x-city.loc_x)**2+math.abs(self.loc_y-city.loc_y)**2)

--------------------------------------------