Question
All code should be in Python 3. In this problem you will create a class called Car. Your car class should include the following attributes
All code should be in Python 3.
In this problem you will create a class called Car. Your car class should include the following attributes (and no others):
fuelEfficiency (miles per gallon as a floating point number)
fuelCapacity (floating point)
fuelLevel (floating point)
odometer (integer)
Your car class should include the following methods
An initializer that allows the fuel efficiency and fuel capacity to be specified. The default fuel efficiency to 20 mpg and the default fuel capacity is 35 gallons. All new cars have an odometer reading of zero and an empty gas tank.
Accessors for each attribute. The accessor methods should be called getFuelEfficiency, getFuelCapacity, getFuelLevel, and getOdometer.
A method called drive that drives the car a specified number of miles. If there is enough fuel in the tank, the car will be driven the specified number of miles. If there is not enough fuel in the tank, the car will drive as far as it can with the amount of fuel currently in the tank. The odometer and fuel level should be adjusted accordingly. The miles specified must be a positive number. If an invalid parameter is provided the method simply returns (the car is not driven).
A method called addFuel that adds a specified number of gallons of fuel to the tank. The gallons specified must be a positive number. Your method must verify this. If the number of gallons won't fit in the tank then the method adds no fuel. This is to avoid spills at the gas station.
A method called tripRange that returns the number of miles that can be driven given the amount of fuel in the tank.
An __str__ method that returns a meaningful string representation.
A __repr__ method that gives the values of all the attributes.
Note that there are no "set" methods in the Car class.
Tester Program
Here is a tester program that I wrote for the Car class. You should download this program and save it in a directory along with your program. If your Car class is working right the tester program should display the message No Errors Found.
from Car import * errorCount = 0 def check(car, eff, cap, level, odo, message): global errorCount foundError = False errorMessage = message + ' ' if car.getFuelEfficiency() != eff: foundError = True errorMessage += '\tfuelEfficiency: Expected {}, got {} '.format(eff, car.getFuelEfficiency()) if car.getFuelCapacity() != cap: foundError = True errorMessage += '\tfuelCapacity: Expected {}, got {} '.format(cap, car.getFuelCapacity()) if car.getFuelLevel() != level: foundError = True errorMessage += '\tfuelLevel: Expected {}, got {} '.format(level, car.getFuelLevel()) if car.getOdometer() != odo: foundError = True errorMessage += '\tOdometer: Expected {}, got {} '.format(odo, car.getOdometer()) if foundError: errorCount += 1 print(errorMessage) def check1(value, expected, message): global errorCount if value != expected: errorCount += 1 print('{} \texpected {}, got {} '.format(message, expected, value)) def carTester(): global errorCount car = Car() check(car, 20, 35, 0, 0, 'Test 1') car = Car(25, 15) check1(car.tripRange(), 0, 'Test 2') check(car, 25, 15, 0, 0, 'Test 3') car.addFuel(-1) check(car, 25, 15, 0, 0, "Test 4") car.addFuel(1000) check(car, 25, 15, 0, 0, "Test 5") car.addFuel(0) check(car, 25, 15, 0, 0, "Test 6") car.addFuel(15) check(car, 25, 15, 15, 0, "Test 7") car.drive(1000) check(car, 25, 15, 0, 375, "Test 9") car.drive(5) check(car, 25, 15, 0, 375, "Test 10") car.addFuel(10) check(car, 25, 15, 10, 375, "Test 11") car.drive(-1) check(car, 25, 15, 10, 375, "Test 12") car.drive(0) check(car, 25, 15, 10, 375, "Test 13") check1(car.tripRange(), 250, "Test 14"); if errorCount == 0: print('No Errors Found') else: print(' {} test(s) failed'.format(errorCount)) carTester()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started