share
Stack OverflowOOP in Python!! My First assignment in Python
[-5] [2] user708354
[2011-04-20 05:18:22]
[ python ]
[ http://stackoverflow.com/questions/5725829/oop-in-python-my-first-assignment-in-python ] [DELETED]

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.

(1) Please label this as homework. - Blackmoon
(1) There is a question, then your call for help. What constitutes a help here? Have you not written a single line to show here? - vpit3833
(2) In addition: SO is not for write-the-code-for-me - Blackmoon
(1) Double underscores for those data members make me grumpy - JCotton
Starting your homework a little sooner would probably help, too. - martineau
@RestRisiko - forget about the [homework] tag. It doesn't matter what circumstances drive the questioner to ask their question as long as the question is worth asking in the first place. meta.stackoverflow.com/questions/60422/is-homework-an-exception - APC
[0] [2011-04-20 05:28:22] cMinor

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/
[2] http://docs.python.org/release/2.2.3/mac/node6.html

1
[0] [2011-04-20 05:32:19] joaquin

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()

You should inherit from object so you have a new-style class. Doesn't matter in this case but it's good to always do it. - ThiefMaster
2