Question

1. Extending the answer from Python HW #7 for question #1, write the following python code to expand on the Car class. Include the python code you developed when answering HW #7.

  1. Add class “getter” methods to return the values for model, color, and MPG

  1. Add class “setter” methods to change the existing values for model, color, and MPG. The value to be used in the methods will be passed as an input argument.

  1. Add class method that will calculate and return the number of miles driven, where the method is provided the size of the gas tank as an integer input

  1. Below your Car class, create a new child class called mySportsCar that inherits from Car class. Make sure you call the Car class constructor with the appropriate inputs in the child class constructor.

  1. Instantiate an object of the mySportsCar class. Use whatever appropriate input values that you would like.

HW#7:

class Car: condition = new def __init_(self, model, color, MPG): self.model = model self.color = color self.MPG = MPG my_ca

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

Code

class Car:
condition="new"

def __init__(self,model,color,MPG):
self.model=model
self.color=color
self.MPG=MPG

#A. getter method
def getter(self):
return [ self.model , self.color , self.MPG ]
  
#B. setter method
def setter(self,model,color,MPG):
self.model=model
self.color=color
self.MPG=MPG

#C. Calculate No of Miles Driven
def miles(self,size):
return self.MPG/size

#D. Creating child class called mySportsCar
class mySportsCar( Car ):
def __init__(self , model , color , MPG , name):
self.name = name
Car.__init__(self, model , color , MPG )

  

my_car= Car("Corvette","Black",30)
print("Model is:",my_car.model)
print("Color is:",my_car.color)
print("MPG is:",my_car.MPG)

print(my_car.getter())
my_car.setter("Ford","Red",40)
print("Model is:",my_car.model)
print("Color is:",my_car.color)
print("MPG is:",my_car.MPG)

print("Miles Driven :",my_car.miles(40))

#E. Instantiate an object of the mySportsCar class
new_car = mySportsCar("Forge","Blue",50,"New")
print("Model is:",new_car.model)
print("Color is:",new_car.color)
print("MPG is:",new_car.MPG)
print("Name is:",new_car.name)

Output:

Model is: Corvette Color is: Black MPG is: 30 [Corvette, Black, 30] Model is: Ford Color is: Red MPG is: 40 Miles Driven

Add a comment
Know the answer?
Add Answer to:
1. Extending the answer from Python HW #7 for question #1, write the following python code...
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
  • 1) Using the Object-Oriented Programming Paradigm,implement the necessary code in Python creating a ‘Car’ class with...

    1) Using the Object-Oriented Programming Paradigm,implement the necessary code in Python creating a ‘Car’ class with a constructor that stores all the Car information (ID, MAKE, MODEL, YEAR , COLOR, MILEAGE, PRICE_TO_DEALER, SALE_PRICE, PROFIT) as attributes. 2) Implement and add the following methods to the ‘Car' class in Question 1. a) necessary getter methods b) necessary setter methods c) method to display all the information to the screen d) a method to calculate the profit

  • Using Python. Complete the following: Create a class called Pet that has two attributes: name and...

    Using Python. Complete the following: Create a class called Pet that has two attributes: name and breed Provide a constructor that accepts the data to initialize these attributes Provide the appropriate getter and setter methods Create a Pet object called dog and set its name to "Otis" and breed to "Pug". Print the dog's name and breed to the console.

  • please help with the marked ones the programming language is python code. 23.16 Write an RGB...

    please help with the marked ones the programming language is python code. 23.16 Write an RGB class that represents an RGB color. Store the red, green. and blue components. Include a _str_0 method and a luminance method that returns the luminance of the color. Write a main() function to test your code. 23.17 Write a CD class that represents a single music cd. Store the artist, title, genre, year of release, and playing time in minutes and seconds. However, instead...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • You will be creating a driver class and 5 class files for this assignment. The classes...

    You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle). Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Within the driver class, please complete the following tasks: Instantiate a Circle object with 1 side and a radius of 4; print it Update...

  • Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student...

    Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student and “PostGraduate” that inherits form “Graduate”. Write the class definition for the abstract class “Student”. The class definition should include private instance variables of type String to hold the student’s first name, a string for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. Also...

  • how would i write code in java/netbeans for this: You will be creating the driver class...

    how would i write code in java/netbeans for this: You will be creating the driver class for this homework (PA05_TVs.java) and the object/element class, TV.java. Your TV class should include only a channel value for instance data (3-99) and the following methods: constructor [requires an initial channel value], getter, setter, randomChannel() [does not return the new channel value], and toString(). The driver class must complete the following: Instantiate a TV object and set the initial channel to 97. Instantiate a...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Please show all work and answer all parts using python, thanks! Instructions You will need to...

    Please show all work and answer all parts using python, thanks! Instructions You will need to create four files: • Shape2D.py - file containing a class definition containing properties all Shapes could possibly have. • Circle.py - file containing a class definition of a Circle that inherits from the Shape2D class. Square.py - file containing a class definition of a Square that inherits from the Shape2D class. • testFile.py - file containing pytest functions testing the Shape2D, Circle, and Square...

  • 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...

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