Question: Call your class Car. Write an __init__() method with self as the only parameter. This method should do the following: Setanattributex(representingthecar'sxcoordinate)to0. Setanattributey(representingthecar'sycoordinate)to0. Setanattributeorient(representingthecar'sorientation)ton(fornorth). Setanattributegas(representingtheamountofgasinthecar'stank)to10.0. Write
-
Call your class Car.
-
Write an __init__() method with self as the only parameter. This method should do the following: Setanattributex(representingthecar'sxcoordinate)to0. Setanattributey(representingthecar'sycoordinate)to0. Setanattributeorient(representingthecar'sorientation)to"n"(fornorth).
Setanattributegas(representingtheamountofgasinthecar'stank)to10.0.
-
Write a drive() method with parameters self and reverse. reverse should take a Boolean value; give it a default value of False. This method should do the following:
-
Ifthegasattributeislessthan0.2,raiseaRuntimeError.
-
Thismethodshouldchangethexoryattributedependingonthevalueoftheorientattributeandthe
parameter reverse. When driving north, decrease y by 1; south, increase y by 1; west, decrease x by 1; east,
increase x by 1.
-
Thismethodshoulddecreasethegasattributeby0.2.
-
-
Write a turn() method with parameters self and dir (for direction). dir should be 'l' or 'r'. This method should do the following: Changetheorientattributedependingonthevalueofdir.Forexample,iforientwas'n'anddiris'r',
orient should become 'e'.
-
Write a refill() method with self as the only parameter. This method should do the following:
Setthegasattributeto10.0.
-
Write a status() method with self as the only parameter. This method should do the following:
Printamessageindicatingthecar'scurrentpositionandorientationandtheamountofgasinthetank.
-
Write docstrings for each method. (__init__() doesn't need a docstring.)
-
Make an if __name__ == "__main__": statement in which you create an instance of your class and invoke each method (you don't have to explicitly invoke __init__()).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
