Boat Race Define a class named BoatRace that contains the following information about a Boat Race: race_name: string race_id: int distance: int racers: List of Boat objects Write a constructor that allows the programmer to create an object of type BoatRace with a race_name, race_id, list of racers objects, and distance. The constructor will only take in one parameter, a string representing the name of a CSV file. The file will have the following format: Each row will always have exactly two columns. The first row will always contain the name of the race. The second row will always contain the id number for the race. The third row will always contain the distance for the race. All remaining rows contain information about the boats involved in the race: the first column will be the name of the boat, and the second column is that boat’s top speed. For example, the race in the file below has two boats: The Fire Ball with top speed 12, and The Leaf with top speed 100. Name,The Big One ID,11 Distance,120 The Fire Ball,12 The Leaf,100 The constructor must read the information from the file and use it to initialize the appropriate instance variables. Keep in mind that the race id and distance for the race should be converted to an integer, as should the top speed for each boat. The racers instance variable is a list of Boat objects. This means that you will need to use the Boat constructor from problem A to create a Boat object from the information in each row past the third. Provide a getter for the following instance variables with no input parameters (other than self) passed in: get_race_name returns the race_name get_race_id returns the race_id get_distance returns the distance get_racers returns the racers Examples: Copy the following if __name__ == "__main__" block into your hw11.py file, and comment out tests for parts of the class you haven’t implemented yet. The lines that have output include the expected value next to them as a comment (note that you will likely not exactly match the memory addresses shown for the Boat objects). You will need to extract the sample CSV files from the hw11files.zip folder on Canvas, into the directory you’re running Python from. if __name__ == '__main__': the_race = BoatRace('the_big_one.csv') print(the_race.get_race_name()) #The Big One print(the_race.get_race_id()) #11 print(type(the_race.race_id)) # print(the_race.get_distance()) #120 print(type(the_race.distance)) # print(the_race.get_racers()) #[<__main__.Boat object at 0x03A2E4C0>, <__main__.Boat object at 0x03A2E4F0>] print(type(the_race.get_racers()[0].top_speed)) # Boat Race Methods Add the following methods to your Boat Race class: Write a method called add_racer which takes in a Boat object and adds it to the end of the racers list. The function does not return anything. Write a method called print_racers which loops through racers and prints the Boat objects. This function takes in no parameters (other than self) and returns nothing. Write a method called count that returns the number of racers. Write a method called race. The race function calls the move function for all of the racers in the BoatRace. Once all the racers have moved, call the print_racers method to display information about the progress of each boat. Then, check if any of the racer’s current_progress is greater than or equal to the race’s distance. If so, then return a list of all of the racers whose current_progress is greater than or equal to distance. If no racer has finished the race then repeat the calls to move and check until at least one racer has finished the race. Examples: Copy the following if __name__ == "__main__" block into your hw11.py file, and comment out tests for parts of the class you haven’t implemented yet. The lines that have output include the expected value next to them as a comment (note that some lines are random and your output will not match exactly). The first race example in the code below should finish within a few turns, and unless you get extremely unlucky, The Leaf should win a solo victory almost every time due to its top speed. The second race, on the other hand, should almost always finish in a single round with three or four boats tied for victory. if __name__ == '__main__': the_race = BoatRace('the_big_one.csv') the_race.add_racer(Boat("Late", 2)) the_race.print_racers() #The Fire Ball: 0 #The Leaf: 0 print(type(the_race.get_racers()[0].top_speed)) #
Boat Race
Define a class named BoatRace that contains the following information about a Boat Race:
- race_name: string
- race_id: int
- distance: int
- racers: List of Boat objects
- Write a constructor that allows the programmer to create an object of type BoatRace with a race_name, race_id, list of racers objects, and distance.
- The constructor will only take in one parameter, a string representing the name of a CSV file. The file will have the following format:
- Each row will always have exactly two columns.
- The first row will always contain the name of the race.
- The second row will always contain the id number for the race.
- The third row will always contain the distance for the race.
- All remaining rows contain information about the boats involved in the race: the first column will be the name of the boat, and the second column is that boat’s top speed. For example, the race in the file below has two boats: The Fire Ball with top speed 12, and The Leaf with top speed 100.
Name,The Big One
ID,11
Distance,120
The Fire Ball,12
The Leaf,100
- The constructor must read the information from the file and use it to initialize the appropriate instance variables. Keep in mind that the race id and distance for the race should be converted to an integer, as should the top speed for each boat.
- The racers instance variable is a list of Boat objects. This means that you will need to use the Boat constructor from problem A to create a Boat object from the information in each row past the third.
- Provide a getter for the following instance variables with no input parameters (other than self) passed in:
- get_race_name returns the race_name
- get_race_id returns the race_id
- get_distance returns the distance
- get_racers returns the racers
Examples:
Copy the following if __name__ == "__main__" block into your hw11.py file, and comment out tests for parts of the class you haven’t implemented yet. The lines that have output include the expected value next to them as a comment (note that you will likely not exactly match the memory addresses shown for the Boat objects).
You will need to extract the sample CSV files from the hw11files.zip folder on Canvas, into the directory you’re running Python from.
if __name__ == '__main__':
the_race = BoatRace('the_big_one.csv')
print(the_race.get_race_name()) #The Big One
print(the_race.get_race_id()) #11
print(type(the_race.race_id)) #<class 'int'>
print(the_race.get_distance()) #120
print(type(the_race.distance)) #<class 'int'>
print(the_race.get_racers()) #[<__main__.Boat object at 0x03A2E4C0>, <__main__.Boat object at 0x03A2E4F0>]
print(type(the_race.get_racers()[0].top_speed)) #<class 'int'>
Boat Race Methods
Add the following methods to your Boat Race class:
- Write a method called add_racer which takes in a Boat object and adds it to the end of the racers list. The function does not return anything.
- Write a method called print_racers which loops through racers and prints the Boat objects. This function takes in no parameters (other than self) and returns nothing.
- Write a method called count that returns the number of racers.
- Write a method called race.
- The race function calls the move function for all of the racers in the BoatRace.
- Once all the racers have moved, call the print_racers method to display information about the progress of each boat.
- Then, check if any of the racer’s current_progress is greater than or equal to the race’s distance.
- If so, then return a list of all of the racers whose current_progress is greater than or equal to distance.
- If no racer has finished the race then repeat the calls to move and check until at least one racer has finished the race.
Examples:
Copy the following if __name__ == "__main__" block into your hw11.py file, and comment out tests for parts of the class you haven’t implemented yet. The lines that have output include the expected value next to them as a comment (note that some lines are random and your output will not match exactly).
The first race example in the code below should finish within a few turns, and unless you get extremely unlucky, The Leaf should win a solo victory almost every time due to its top speed. The second race, on the other hand, should almost always finish in a single round with three or four boats tied for victory.
if __name__ == '__main__':
the_race = BoatRace('the_big_one.csv')
the_race.add_racer(Boat("Late", 2))
the_race.print_racers() #The Fire Ball: 0
#The Leaf: 0
print(type(the_race.get_racers()[0].top_speed)) #<class 'int'>
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images