Question

In: Computer Science

PYTHON Exercise: Accelerate Method ------------------------- ### Description In this exercise, you will add to your `Car`...

PYTHON

Exercise: Accelerate Method
-------------------------

### Description

In this exercise, you will add to your `Car` class
a method to accelerate the speed of an instance.

### Class Name

`Car`

### Method

`accelerate()`

### Parameters

* `self` : the `Car` object to use
* `delta_speed` : a number, the desired value to add to the speed data member.

### Action

Adds `delta_speed` to the speed of the object. If the
new speed is too fast, then set the speed to the
maximum allowed speed.

Only do this action of `delta_speed` is positive, and
the `Car` isn't already at maximum speed.

### Return Value

`True` if a change occurred, `False` otherwise.

class Car:
def __init__(self):
self.speed = 0.0

def getSpeed(self):
return self.speed
  
def setSpeed(self, speed):
if speed >= 0 and speed <= 80:
self.speed=speed
return True
else:
return False
  
def accelerate(self, delta_speed):

Solutions

Expert Solution

class Car:
    def __init__(self):
        self.speed = 0.0
    
    def getSpeed(self):
        return self.speed
      
    def setSpeed(self, speed):
        if speed >= 0 and speed <= 80:
            self.speed=speed
            return True
        else:
            return False
          
    def accelerate(self, delta_speed):
        if delta_speed > 0:
            if self.speed >= 80:
                self.speed = 80
                return False
            elif self.speed < 80:
                self.speed = self.speed + delta_speed
                if self.speed > 80:
                    self.speed = 80
                return True
        else:
            return False

I have put if-else condition to check if delta_speed is positive or not and if its already in max speed then no change. If after adding delta_speed, the speed is greater than max speed, i.e., 80, then speed is set to the
max allowed speed. Since, change in speed occurs, so it will return True otherwise False.

Jupyter Notebook sample run: -


Related Solutions

Exercise: Add Monster Cast Member ------------------------- ### Description In this series of exercises, you will create...
Exercise: Add Monster Cast Member ------------------------- ### Description In this series of exercises, you will create functions to create, modify and examine dictionaries that represent characters in an animated film, and the cast members who voice the characters. The keys of the dictionary will be character names. The values in the dictionary will be voice actor names. For this exercise, you will create a function that adds an entry to a dictionary. They key is a character name, and the...
Calculator in Assembly Language (Please add comments to explain your steps) Description: You are responsible to...
Calculator in Assembly Language (Please add comments to explain your steps) Description: You are responsible to implement several assembly functions to perform the simple arithmetic calculations for 2 64-bit integers. These functions will use the C function signature but the main logic within this function should be inline assembly code using the ASM block similar to the assembly example shown in class. Program Specification: 1. long mult ( long op1, long op2 ) - Can’t use the MUL/IMUL instructions, meaning...
A 2000 kg car is sitting at a stop sign and begins to accelerate to a...
A 2000 kg car is sitting at a stop sign and begins to accelerate to a speed of 5km/hr at that moment a 1000kg car smashed into the rear end of thus car and stuck together in the collision. A cop shows up to the scene and assumes the 1000 kg car was speeding at 50km/hr by measuring the tire marks that are 3 m long. Assume the the coefficient of friction is 0.8 what is the speed that the...
I. Describe one DNA extraction method. In your description compare and contrast the method you chose...
I. Describe one DNA extraction method. In your description compare and contrast the method you chose with the other DNA extraction methods. Additionally, describe what types of forensic samples are better suited for this type of extraction and why. a)Given the three types of DNA techniques (STR, mtDNA, and Y-STRs) briefly compare and contrast all of them and in your opinion state why you believe one to be better than the other. YOU MUST SUPPORT YOUR ANSWER WITH AN EXAMPLE...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
Python Please debug each python block. Do not change the algorithm. You can add statements or...
Python Please debug each python block. Do not change the algorithm. You can add statements or you can modify existing python statements. #2) The below code prints all numbers from 5 to 20. Modify the below while loop to print odd numbers from 5 to 21, # including 21. Correct syntax errors as well. i = 5 while(i<21): print(i) i = i+1
a. For your senior project, you would like to build a cyclotron that will accelerate protons...
a. For your senior project, you would like to build a cyclotron that will accelerate protons to 10% of the speed of light. The largest vacuum chamber you can find is 60 cm in diameter.What magnetic field strength will you need? b.What magnetic field strength will levitate the 2.0 g wire in the figure(Figure 1)? Assume that I = 1.9 A and d = 12 cm . c. The magnetic field strength at the north pole of a 2.0-cm-diameter, 8-cm-long...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your summer internship with ImmunityPlus based in La Crosse, Wisconsin. You are working with the forecasting team to estimate how many doses of an immunization drug will be needed. For each drug estimation, you will be provided the following information: corona.txt 39 20 31 10 42 49 54 21 70 40 47 60 - The size of the target population. - The life expectancy, in...
It's cold--check your tires. When it gets cold, you have to add more air to car...
It's cold--check your tires. When it gets cold, you have to add more air to car tires. If a 1.90×10−2 m3m3  car tire at 299 KK  has an *absolute* pressure of only 213 kPa How many moles of air do you have to add to increase its *absolute* pressure to 271 kPa? The temperature and volume of the tire remain constant.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT