Question

python Design a class named Car that has the following fields: yearModel: The yearModel field is...

python
Design a class named Car that has the following fields:

yearModel: The yearModel field is an Integer that holds the car’s year model.

make: The make field references a String that holds the make of the car.

speed: The speed field is an Integer that holds the car’s current speed.

In addition, the class should have the following constructor and other methods:

Constructor: The constructor should accept the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field.

Accessors: Design appropriate accessor methods to get the values stored in an object’s yearModel, make, and speed fields.

accelerate: The accelerate method should add 5 to the speed field each time it is called.

brake: The brake method should subtract 5 from the speed field each time it is called.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Points to consider:

  1. We need to create the Car Class.
  2. Then we need to make the object out of it.
  3. Then we need to print the speed each time we accelerate and then brake for five times and display speed each time.

Code


class Car:
    def __init__(self, year_model, make):
        self.year_model = year_model
        self.make = make
        self.speed = 0

    def getYearModel(self):
        return self.year_model

    def setYearModel(self, year_model):
        self.year_model = year_model

    def getMake(self):
        return self.make

    def setMake(self, make):
        self.make = make

    def getSpeed(self):
        return self.speed

    def setSpeed(self, speed):
        self.speed = speed

    def accelerate(self):
        self.speed += 5

    def brake(self):
        if self.speed >= 5:
            self.speed -= 5
        else:
            self.speed = 0


def main():
    c = Car(2015, "Honda")
    for i in range(5):
        c.accelerate()
        print("Speed: ", c.getSpeed())

    for i in range(5):
        c.brake()
        print("Speed: ", c.getSpeed())

main()

Code Snippet

Output Snippet

Friend, That was a nice question to answer
If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like it if you think effort deserves like.
Thanks

Add a comment
Know the answer?
Add Answer to:
python Design a class named Car that has the following fields: yearModel: The yearModel field is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Program Write a class named Car that has the following fields: yearModel- The yearModel field...

    Java Program Write a class named Car that has the following fields: yearModel- The yearModel field is an int that holds the car’s year model. make- The make field is a String object that holds the make of the car, such as “Ford”, “Chevrolet”, etc. speed- This speed field is an int that holds the car’s current speed. In addition, the class should have the following methods: Constructor- The constructor should accept the car’s year model and make as arguments....

  • In Python Programming: Write a class named Car that has the following data attributes: _ _year_model...

    In Python Programming: 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 accepts 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...

  • java Write a class named Car that has the following fields: • yearModel: The yearModel field...

    java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...

  • C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a...

    C++ Program 1a. Purpose Practice the creation and use of a Class 1b. Procedure Write a class named Car that has the following member variables: year. An int that holds the car’s model year. make. A string object that holds the make of the car. speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. Constructor. The constructor should accept the car’s year and make as arguments and assign these values...

  • Car Class Background: Write a class named Car that will be used to store information and...

    Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake of a car. Functionality: 1. Write a class named "Car” that has the following member variables: • year Model - an int that holds the car's year model • make-A string that holds the make of the car • speed - an int that holds the car's current speed 2. In addition the class should have the following member...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Write a java program that creates a class Car. Class Car contains the following private member...

    Write a java program that creates a class Car. Class Car contains the following private member fields: make, model, year, and miles. The class Car has a constructor that takes as arguments the car make, model, and year. In addition, the constructor sets miles to 100 as default. Class Car has accessors and mutators to get and set the make, model, year and miles on the car. The program should complete the following tasks: Ask the user to enter make,...

  • In Python - Design a class named Time. The class contains: -Data fields hour, minute, and...

    In Python - Design a class named Time. The class contains: -Data fields hour, minute, and second that represent a time. -A no-arg constructor that creates a Time object for the current time.(the values of the data fields will respresent the current time.) -A constructor that constructs a Time object with a specified hour, minute, and second, respectively. -A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds. (The values of...

  • Design a class called Building. The class should keep the following information in the fields: String...

    Design a class called Building. The class should keep the following information in the fields: String address double property_value create several constructors including copy constructor , create accessor and mutator methods        Part 2 (50 points) Define interface Taxable which has one method getPropertyTax() Design a class called Private_Property that extends Building and implements Taxable It should have the following field : double value_of_improvements Create a constructor for this class to include newly added fields. The tax for the private property...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT