SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Classes And Object Oriented Programming
Exercise:
Programming Exercises
Question:2 | ISBN:9780132576376 | Edition: 2

Question

Car Class
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 attribute.


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 t

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Car Class Program code:

#Create a class name, Car
class Car:
    #Create data attributes for class, Car
    # __init__ method that accept the car’s 
    #year model and make as arguments.
    def __init__(self, year_model, make):
        self.__year_model = year_model
        self.__make = make
        self.__speed = 0
    #accelerate method should add 5 to the speed data 
    #attribute each time it is called.
    def accelerate(self):
        self.__speed += 5
    #brake method should subtract 5 from the 
    #speed data attribute each time it is called.
    def brake(self):
        if self.__speed >= 5:
            self.__speed -= 5
    # get_speed method should return the current speed.
    def get_speed(self):
        return self.__speed

# Create a Car object
car = Car("2023", "Hyndai")

# Accelerate the car five times
for _ in range(5):
    car.accelerate()
    print("Current speed after acceleration:", car.get_speed())

# Brake the car five times
for _ in range(5):
    car.brake()
    print("Current speed after braking:", car.get_speed())

Executed Output:

Current speed after acceleration: 5
Current speed after acceleration: 10
Current speed after acceleration: 15
Current speed after acceleration: 20
Current speed after acceleration: 25
Current speed after braking: 20
Current speed after braking: 15
Current speed after braking: 10
Current speed after braking: 5
Current speed after braking: 0

 

0 0

Discussions

Post the discussion to improve the above solution.