Question

python code
DC Final Project Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/

myHybrid.addGas(9) # Add 9 gallons Sample Scenario We know our car can drive 500 miles. If I use the myHybrid.drive(100) func

Program Behavior When the program starts User should see Miles per gallon: 20 Tank Size (in gallons): 25 (Note: these values

The program also writes/appends the user input and associated result to a log file called LogFuel.txt If user inputs 2 progra

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

Solution:

class Car: #class definition
fuel_efficiency=None
fuel_miles=0
tank_size=0
gallons=0
def __init__(self,miles_per_gallon):#constructor of class
self.fuel_efficiency=miles_per_gallon
  
  
def Drive(self,miles):#method that drives car and updates fuel and gallons
self.fuel_miles-=miles
self.gallons=self.fuel_miles/self.fuel_efficiency
return self.fuel_miles #return fuel_miles

def getGasLevel(self): #method that return fuel left
return self.gallons
  
def addGas(self,gas):#method that adds gas
s=self.tank_size
if self.gallons+gas>self.tank_size:#Check level of gas and add upto max level only
temp=self.gallons
self.gallons=self.tank_size
temp=self.gallons-temp
return str(temp)+" gallons of gas added"#return number of gallons added
else:
self.gallons+=gas
return str(gas)+" gallons of gas added"

def main():#main method definition
file=open("FuelEffic.txt","r")#open file in read mode
l=list(file.readlines())
for i in range(len(l)):
l[i]=l[i][:-1]#store data to list
miles_per_gallon=int(l[0].split(" ")[-1])#get the data from list
tank_size=int(l[1].split(" ")[-1])
Car.fuel_miles=miles_per_gallon*tank_size#set class variables
Car.tank_size=tank_size
Car.gallons=tank_size
o=Car(miles_per_gallon)
  
f=open("LogFuel.txt","w")#open output file in write mode
while(1):
print("1. See Current Fuel Level")#print options
print("2. Drive")
print("3. Add Gas")
print("4. Exit")
ch=int(input())#read choice
if ch==1:
print("The current fuel level is "+str(o.getGasLevel())+" gallons")#If ch is 1,the print current fuel
f.write("Input is 1\n")
f.write("The current fuel level is "+str(o.getGasLevel())+"\n")#write to file
elif ch==2:
value=int(input("How many miles to drive"))#read input from user
out=str(o.Drive(value))#call method to drive and update gallons fo fuel
print("You drove "+str(value)+" miles.You can drive another "+str(out)+" miles on this gas")
f.write("Input is 2\n")
f.write("You drove "+str(value)+" miles.You can drive another "+str(out)+" miles on this gas"+"\n")#write to file
elif ch==3:#If ch is 3,add gas
g=int(input("How much gas to add:"))
res=o.addGas(g)#call method to add gas and get the returned value
print(res)
f.write("Input is 3\n")#print output
f.write(res)#write to file
elif ch==4:#If ch is 4, break loop
break
main()#call main method

Screenshots:

The Screenshots are attached below for reference.

Please follow them for proper indentation,.

Make sure that the code file and input file are present in same folder.

Eo vou AWN class Car: fuel_efficiency=None fuel_miles=0 tank_size=0 gallons=0 def __init__(self,miles_per_gallon): self.fuel

def main(): file=open(FuelEffic.txt,r) 1=list(file.readlines()) for i in range(len(1)): 1[i]=1[i][:-1] miles_per_gallon=i

1. See Current Fuel Level 2. Drive 3. Add Gas 4. Exit The current fuel level is 25 gallons 1. See Current Fuel Level 2. Drive

FuelEffic - Notepad File Edit Format View Help Miles per gallon: 20 Tank size(in gallons): 25

The output file is as follows:

LogFuel - Notepad File Edit Format View Help Input is 1 The current fuel level is 25 Input is 2 You drove 100 miles. You can

Add a comment
Know the answer?
Add Answer to:
python code DC Final Project Implement a class Car with the following properties. A car has...
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
  • By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise,...

    By Python 3 please 2. (a) Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods get Name, addQuiz(score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took. (b) Modify the Student class to compute grade point averages. Methods are needed to add a grade and get the current GPA. Specify grades as...

  • In JAVA In this assignment you will use a class Car to represent a car that travels to various de...

    In JAVA In this assignment you will use a class Car to represent a car that travels to various destinations. Your car has a fuel economy rating of 32.3 miles per gallon. The gas tank holds 19.5 gallons. Your program will need to simulate two trips: 1) BC to Yosemite Valley, and 2) BC to Washington, D.C.. For each trip you will start with a full tank of gas. The output should look as follows. Trip one: Bakersfield College to...

  • please help me add on this java code to run public class CarHwMain public static void...

    please help me add on this java code to run public class CarHwMain public static void main(String args 1/ Construct two new cars and one used car for the simulation Cari carl = new Car W"My New Mazda", 24.5, 16.0); Car car2 = new Cart My New Ford" 20.5, 15.0) Cari car) - new Cari ("My Used Caddie", 15.5, 16.5, 5.5, 1/ ADD CODE to completely fill the tanks in the new cars and off the used cars ton //...

  • I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas...

    I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas mileage. The program's window should have Entry widgets that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked,...

  • Write a program to compute miles per gallon of a car, over a long trip. The...

    Write a program to compute miles per gallon of a car, over a long trip. The car begins the trip with a full tank, and each fuel stop along the way fills the tank again. You will not do any math, arithmetic, or computations in the main() function. All output will be displayed from the main() function. Start the program by asking the user for the starting odometer reading. All odometer readings will be in whole miles only. The fuel...

  • *** USING C++ *** Create a file containing the following: car numbers, miles driven, and gallons...

    *** USING C++ *** Create a file containing the following: car numbers, miles driven, and gallons of gas used in each car (do not include the readings): Car Number 25 Gallons Used 65 138 36 Miles Driven 1500 3540 1889 2466 2265 68 87 Write a program that reads the data in the file and displays the car number, miles driven, gallons used and the miles per gallon for each car. The output should also contain the total miles drive,...

  • The Python program will compute total mileage for all trips included : Trip: 1,2,3,4 Mileage: 200,...

    The Python program will compute total mileage for all trips included : Trip: 1,2,3,4 Mileage: 200, 400, 800, 10 Gas: 10, 17, 39, 1 As well as the fuel efficiency in miles per gallon. Your program will open the file, read it line by line and add the miles driven and the gallons gas consumed then calculate the average fuel efficiency. It will then write Once the program has read all the lines, it will write the total miles driven,...

  • Assignment 4 Due Mar 22 by 11:59pmPoints 100 Submitting a file upload A4 OOP 2 "Car...

    Assignment 4 Due Mar 22 by 11:59pmPoints 100 Submitting a file upload A4 OOP 2 "Car Instrument Simulator" Access A4 from pdf assignment file & Turn in the following files: a4main.java FuelGauge.java Odometer.java program compile and run screenshots design document (including UML) A4 10. Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a car's fuel gauge and odometer. The classes you will design are the following: · The Pue|Gauge Class:...

  • Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

    Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging...

  • The assignment is to write a program in unix using C++ environment Car Instrument Simulator For this assignment you w...

    The assignment is to write a program in unix using C++ environment Car Instrument Simulator For this assignment you will design a set of classes that work together to simulate a car’s fuel gauge and odometer. The classes you will design are: • The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are – To know the car’s current amount of fuel, in gallons. – To report the car’s current amount of fuel, in gallons. – To...

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