c++ myprogramminglab.com hw problem. ch10 defining classes Define a class called
ID: 655733 • Letter: C
Question
c++ myprogramminglab.com hw problem.
ch10 defining classes
Define a class called Odometer that will be used to track fuel and mileage
for a vehicle. Include member variables to track the distance driven (in miles)
and the fuel efficiency of the vehicle (in miles per gallon). The class will
have a constructor that accepts one parameter and initializes distance to 0 and
fuel efficiency to the value of its parameter . Include a member function to
reset the odometer to 0 miles, a member function that accepts the miles driven
for a trip and adds it to the odometer's total, and a member function that
returns the number of gallons of gasoline that the vehicle has consumed since
the odometer was last reset.
Use your Odometer class in a program that would ask the user to input fuel
efficiency and then create an Odometer object using your constructor to
initialize the member variables to their appropriate values . Then the program
would ask the user to input the miles driven for two trips and output the
gallons of gas the vehicle consumed on these two trips. Then it will reset the
odometer and repeat the procedure for two more trips.
SAMPLE RUN #1
--- Prompts For Keyboard/Console/Standard Input ---
Enter:
Inputs
--- Keyboard/Console/Standard Input stdin ---
24
56
16
72
24
Outputs
--- Monitor/Console/Standard Output ---
Enter:your fuel efficiency in miles per gallon:Enter:your first trip distance in miles:Enter:your second trip distance in miles:The vehicle consumed 3 gallon(s) of gas for the first and second trips.
Enter:your third trip distance in miles:Enter:your fourth trip distance in miles:The vehicle consumed 4 gallon(s) of gas for the third and fourth trips.
What The Console Looks Like In An Interactive Session:
(Note: this combines standard input with standard output )
>./Odometer
Enter: 24
your fuel efficiency in miles per gallon:Enter: 56
your first trip distance in miles:Enter: 16
your second trip distance in miles:The vehicle consumed 3 gallon(s) of gas for the first and second trips.
Enter: 72
your third trip distance in miles:Enter: 24
your fourth trip distance in miles:The vehicle consumed 4 gallon(s) of gas for the third and fourth trips.
SAMPLE RUN #2
--- Prompts For Keyboard/Console/Standard Input ---
Enter:
Inputs
--- Keyboard/Console/Standard Input stdin ---
20
0
15
15
0
Outputs
--- Monitor/Console/Standard Output ---
Enter:your fuel efficiency in miles per gallon:Enter:your first trip distance in miles:Enter:your second trip distance in miles:The vehicle consumed 0.75 gallon(s) of gas for the first and second trips.
Enter:your third trip distance in miles:Enter:your fourth trip distance in miles:The vehicle consumed 0.75 gallon(s) of gas for the third and fourth trips.
What The Console Looks Like In An Interactive Session:
(Note: this combines standard input with standard output )
>./Odometer
Enter: 20
your fuel efficiency in miles per gallon:Enter: 0
your first trip distance in miles:Enter: 15
your second trip distance in miles:The vehicle consumed 0.75 gallon(s) of gas for the first and second trips.
Enter: 15
your third trip distance in miles:Enter: 0
your fourth trip distance in miles:The vehicle consumed 0.75 gallon(s) of gas for the third and fourth trips.
SAMPLE RUN #3
--- Prompts For Keyboard/Console/Standard Input ---
Enter:
Inputs
--- Keyboard/Console/Standard Input stdin ---
18
189
171
216
45
Outputs
--- Monitor/Console/Standard Output ---
Enter:your fuel efficiency in miles per gallon:Enter:your first trip distance in miles:Enter:your second trip distance in miles:The vehicle consumed 20 gallon(s) of gas for the first and second trips.
Enter:your third trip distance in miles:Enter:your fourth trip distance in miles:The vehicle consumed 14.5 gallon(s) of gas for the third and fourth trips.
What The Console Looks Like In An Interactive Session:
(Note: this combines standard input with standard output )
>./Odometer
Enter: 18
your fuel efficiency in miles per gallon:Enter: 189
your first trip distance in miles:Enter: 171
your second trip distance in miles:The vehicle consumed 20 gallon(s) of gas for the first and second trips.
Enter: 216
your third trip distance in miles:Enter: 45
your fourth trip distance in miles:The vehicle consumed 14.5 gallon(s) of gas for the third and fourth trips.
Explanation / Answer
#include<iostream>
#include <iomanip> //included for format cout to two decimals
using namespace std;
class Odometer{
private:
double odometerTotal;
double gallonsUsedTotal;
public:
Odometer();
void resetOdometer();
void Odometer::addMilesDriven(double (milesDriven));
void Odometer::addGallonsUsed(double (fnGallonsUsed));
double Odometer::getTripMpg(double (milesDriven),double (fnGallonsUsed));
double Odometer::getTotalMiles();
double Odometer::getTotalGallons();
double Odometer::getTotalMpg();
};
Odometer::Odometer(){
odometerTotal = 0;
gallonsUsedTotal = 0;
}
void Odometer::resetOdometer(){
odometerTotal = 0;
gallonsUsedTotal = 0;
}
void Odometer::addMilesDriven(double (milesDriven)){
odometerTotal = odometerTotal + milesDriven;
}
void Odometer::addGallonsUsed(double (fnGallonsUsed)){
gallonsUsedTotal = gallonsUsedTotal + fnGallonsUsed;
}
double Odometer::getTripMpg(double (milesDriven),double (fnGallonsUsed)){
return milesDriven / fnGallonsUsed;
}
double Odometer::getTotalMiles(){
return odometerTotal;
}
double Odometer::getTotalGallons(){
return gallonsUsedTotal;
}
double Odometer::getTotalMpg(){
if(odometerTotal == 0 || gallonsUsedTotal == 0){
return 0;
}else{
return odometerTotal / gallonsUsedTotal;
}
}
int main(){
Odometer odom;
double milesDriven,gallonsUsed;
int option;
std::cout << std::setprecision(2) << std::fixed;
do {
cout << "Select an Option: (1) Enter Trip (2) View Totals (3) Reset Odometer (4) Exit" << endl;
cin >> option;
switch (option){
case 1:
cout << "Enter the number of miles driven: ";
cin >> milesDriven;
odom.addMilesDriven(milesDriven);
cout << "Enter Gallons Used: ";
cin >> gallonsUsed;
odom.addGallonsUsed(gallonsUsed);
cout << "This Trip Averaged " << odom.getTripMpg(milesDriven,gallonsUsed) << " MPG. " << endl;
break;
case 2:
cout << "Number of Miles Driven Since Last Refuel: " << odom.getTotalMiles() << endl;
cout << "Number of Gallons Used Since Last Refuel: " << odom.getTotalGallons() << endl;
cout << "OverAll MPG: " << odom.getTotalMpg() << " " << endl;
break;
case 3:
odom.resetOdometer();
break;
case 4:
break;
}
}while(option != 4);
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.