Program name: main2.cpp, odometer.cpp, ,odometer.hpp Rubrics that apply: All exc
ID: 3743900 • Letter: P
Question
Program name: main2.cpp, odometer.cpp, ,odometer.hpp Rubrics that apply: All except makefile Points Possible: 100 Description You are going to write an Odometer class that will track fuel and mileage for an automotive vehicle. The class should have member variables to track the miles driven (odometer reading), the fuel efficiency of the vehicle in miles per gallon, and the number of gallons used. Include a function to reset the number of miles driven to 0. This should also reset the other variables. You should have a constructor that accepts the miles driven for a trip and number of gallons used. This function should add the miles to the miles driven and the gallons to the gallons used. It should calculate the fuel efficiency. You should have accessor functions to return the odometer reading, fuel efficiency, and gallons used since the odometer was last reset. There should be a function that will accept the number of miles to drive and return the number of gallons of gas needed based on the fuel efficiency to drive that number of miles. You are to write test code to make sure that vour Odometer class works. You need to test at least 3 cases. This means you must use at least 3 Odometers and make sure they each work correctly. All of your functions in the Odometer class must be tested to show that they work. Testing the Odometer class is very open ended. This means that the proof of it working depends on you to make sure that in main you have tested all possibilities. The input and output is your responsibility to demonstrate a working class Class member variables all should be able to hold decimal numbers: number of miles driven number of gallons used fuel efficiency Class member functions . Default constructor Constructor that will accept miles driven and gallons Reset resets miles and gallons to 0 Accessor functions for each of the data Given a number of miles driven, return the amount of fuel that will be needed based on the fuel efficiency Requirements Odometer must be implemented in a class with the above requirements. *No global variables are to be used.Explanation / Answer
Odometer.cpp
/*
* odometer.cpp
*
* Created By: Mohit
* Date: 07/Sep/2018
*
* Class for creating an Odometer which helps in getting fuel efficiency
* given the miles driven and gallons of fuel used.
*
*/
#include <iostream>
#include "Odometer.h"
using std::cout;
// Calculate the fuel efficiency by applying the formula
// fuel efficiency = miles driven / fuel used
void Odometer::calculateFuelEfficiency(void)
{
double miles = getMiles();
double gallons = getGallons();
if (gallons <= 0 || miles <= 0)
{
cout << "Input data cannot be 0 ";
Reset();
}
else
{
double average = miles / gallons;
fuelEfficiency = average;
}
}
//Reset the values of variables when the object is created
Odometer::Odometer(void)
{
Reset();
}
// Set the curresponding values alongside checking for less than or
// equal to 0. And also calculate the efficiency
Odometer::Odometer(double milesDriven, double gallons)
{
noOfMilesDriven = milesDriven;
noOfGallonsUsed = gallons;
if (milesDriven <= 0 || gallons <= 0)
{
cout << "Input value cannot be less than or equal to 0 ";
Reset();
}
else
calculateFuelEfficiency();
}
//Resets all the variables to 0
void Odometer::Reset(void)
{
noOfMilesDriven = 0;
noOfGallonsUsed = 0;
fuelEfficiency = 0;
}
//Fetch available miles
double Odometer::getMiles(void)
{
return noOfMilesDriven;
}
//Fetch available gallons
double Odometer::getGallons(void)
{
return noOfGallonsUsed;
}
//Fetch available fuel efficiency
double Odometer::getFuelEfficiency(void)
{
return fuelEfficiency;
}
//Fetch required fuel for miles to be driven
double Odometer::requiredFuel(double milesDriven)
{
if (milesDriven <= 0 || getFuelEfficiency() <= 0)
{
cout << "Input data cannot be less than or equal to 0 ";
Reset();
return 0;
}
double amountOfFuelRequired = milesDriven / getFuelEfficiency();
return amountOfFuelRequired;
}
Odometer.h
#ifndef ODOMETER_H
#define ODOMETER_H
class Odometer
{
private:
double noOfMilesDriven; //Number of miles driven
double noOfGallonsUsed; //Fuel used(in gallons)
double fuelEfficiency; //Fuel Efficiency
void calculateFuelEfficiency(void);
public:
Odometer(void);
Odometer(double, double);
void Reset(void);
double getMiles(void);
double getGallons(void);
double getFuelEfficiency(void);
double requiredFuel(double);
};
#endif
main.cpp
#include <iostream>
#include "Odometer.h"
using std::cout;
//Driver function
int main()
{
Odometer object = Odometer(50, 10);
cout << "The required fuel is: " << object.requiredFuel(20) << " gallons ";
Odometer object1 = Odometer(0, 10);
Odometer object2 = Odometer(10, 0);
Odometer object3 = Odometer(-5, 10);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.