首页 > > 详细

Python辅导:CSC148 Basic Object Oriented Programming调试Python

Python,。

Requirement

In this exercise, you’ll gain some practice designing and implementing a class, according to an English problem description. You will also practice using this class from another class, or, in other words, relating two classes through composition.

Note: completing this exercise requires Wednesday’s lecture content, so please don’t worry if you aren’t sure how to complete the exercise now. It’s a good idea to read through this handout, the starter code, and the sample tests early, so that you understand what it is you will accomplish.

Starter code

Save these files to your ex1 folder (inside exercises).

  • ex1.py [submit]
  • ex1_test.py

Task 1: A Car simulation

In this task, you’ll implement a basic car simulation, which tracks the positions and amounts of fuel of a set of cars in a city.

Super Duper is a new car ridesharing service which uses a computer system to manage its autonomous vehicles. The managing system contains a collection of cars, each uniquely identified by a string. The manager can add a new car to the collection, move a car, and get the current location of a car.

When a Super Duper car is created in the system, it is given an initial amount of fuel (specified individually per car), and starts at position (0,0) on the grid, representing the Super Duper headquarters. Cars can move to a new point (any integer coordinates are allowed) on the grid, but only move in horizontal and vertical lines. Cars cannot travel diagonally.

When cars move, they lose 1 unit of fuel per 1 unit of distance moved. If you try to move a car farther than it can go with its remaining fuel, the car stays in its original position, and does not use any fuel. (In other words, this method will “fail silently.” In general this is a bad practice, and we’ll explore better ways of handling situations like this later in the course.) Cars can move multiple times, as long as they have enough fuel.

In the starter code, we have created a SuperDuperManager class, which keeps track of all the cars in the Super Duper system. It is responsible for creating new cars, looking up a particular car and retrieving its position or fuel information, and directing cars to move to new locations. Read through the docstrings of the SuperDuperManager class and its methods.

The class has an instance attribute _cars which you must use to complete the Car simulation. (This is a “private” attribute, a concept we’ll cover in Friday’s lecture. Treat it like any other attribute for now.) Its methods correspond to the actions we just described, and you may not change their signatures.

You have two responsibilities:

  1. Design and implement a Car class, which represents a single car according to the above description. We have begun this design for you, but it is your responsibility to complete it.
  2. Implement the methods of the SuperDuperManager class to make use of your Car class.

Frequently-asked question: What’s the difference between SuperDuperManager and Car?

This exercise involves two classes, and part of your work here is to understand how they fit together. Conceptually, the Car class is used to represent a single carthink about this as just one driver in the ride-share system. The SuperDuperManager is responsible for coordinating all cars in the system. Many of its methods require the client code to specify a particular car by id, and should be implemented by using this id to look up a specific car to perform some action or access some data.

However, the most complex method, dispatch, requires looking at all cars in the system. This is similar to what real ride-share services like Uber do: when you requests a ride, the central application needs to look at all of its registered cars and figure out which car to send to pick you up!

Submission instructions

Submit your ex1.py file on MarkUs. You should not need to submit any other files (e.g., the testing file). For more detailed instructions, please consult our Exercise policies page.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!