I have no clue how OOP works in Python. I tried to get ideas from online tutorials but I couldn't understand clearly and my due date for this assignment is tomorrow morning. :( I would really appreciate if you guys could help me in solving this!
Question 1:
Write a class named Car that has the following data attributes:
__year_model
(for the car's year model)__make
(for the make of the car)__speed
(for the car's current speed)The Car class should have an
__init__
method that accept the car's year model and make as arguments. These values should be assigned to the object's__year_model
and_make
data attributes. It should also assign 0 to the__speed
data attributes.The class should also have the following methods:
accelerate
The accelerate method should add 5 to the speed data attribute each time it is called.brake
The brake method should subtract 5 from the speed data attribute each time it is called.get_speed
The get_speed method should return the current speed.Next, design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.
Read this example of a car class from Jeff Ondich [1], and to execute this script you should read this first [2]
'''
car.py
Feb 14, 2011
Written by Jeff Ondich and friends to illustrate
classes.
'''
import random
import time
from graphics import *
class Car:
def __init__(self, left, color):
upperLeft = Point(left, 410)
carWidth = 50
bottomRight = Point(left + carWidth, 435)
self.body = Rectangle(upperLeft, bottomRight)
self.body.setFill(color)
tireRadius = carWidth / 6
self.frontTire = Circle(Point(left + tireRadius, 435), tireRadius)
self.frontTire.setFill(color_rgb(0, 0, 0))
def draw(self, window):
self.body.draw(window)
self.frontTire.draw(window)
def move(self, dx):
self.body.move(dx, 0)
self.frontTire.move(dx, 0)
if __name__ == '__main__':
# Initialize city window
windowHeight = 500
windowWidth = 700
windowTitle = 'City'
backgroundColor = color_rgb(255, 255, 255)
window = GraphWin(windowTitle, windowWidth, windowHeight)
window.setBackground(backgroundColor)
# Create the buildings
cars = []
for k in range(20):
color = color_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
left = random.randint(-10, windowWidth - 40)
newCar = Car(left, color)
cars.append(newCar)
# Draw the cars
for car in cars:
car.draw(window)
while True:
time.sleep(0.10)
for car in cars:
car.move(5)
s = raw_input('Hit Enter to quit')
[1] http://www.cs.carleton.edu/faculty/jondich/courses/cs111_w11/Here you have the foundation of everything you need. YOu must extend it with the other attributes you need
class Car():
def __init__(self, model):
self.__model = model
self.__speed = 0
def accelerate(self):
self.__speed = self.__speed + 5
#how to use it:
mycar = Car('Ford Scort')
mycar.accelerate()
object
so you have a new-style class. Doesn't matter in this case but it's good to always do it. - ThiefMaster