design a set of C++classes that work together to simulate a car’s fuel gauge and odometer
Download file
design a set of C++classes that work together to simulate a car's fuel gauge and odometer
1 file(s) 3.95 KB
Not a member!
Create a FREE account here to get access and download this file
Car Instrument Simulator
for this assignment you will design a set of C++classes that work together to simulate a car’s fuel gauge and odometer. The classes you will design arc:
• The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities
ace
To know the car’s current amount of fuel, in gallons.
To report the car’s current amount of fuel, in gallons.
To be able to increment the amount of fu el by 1 gallon. This simulates purring
fuel in the car. (The car can hold a maximum of 15 gallons.)
To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel
is greater than 0 gallons. This simulates burning fuel as the car runs.
• The Odometer Class: This class will simulate the ca r’s odometer. Its responsibilities arc:
To know the car’s current mileage.
To report the car’s current mileage.
To be able to increment the current mileage by 1 mile. The maximum mileage
the odometer can Store is 999,999 miles. When this amount is exceeded, the
odometer resets the current mileage to O.
To be able to work with a FuelGuage object. It should decrease the FuelGauge
object’s current amount of fuel by 1 gallon for every 24 miles traveled. (The
ca r’s fuel economy is 24 miles per gallon.)
Demonstrate the classes by creating instances of each. Simulate fi lling the car up with
fuel, and then run a loop that increments the odometer until the car runs out of fuel.
During each loop iteration, print the car ‘s current mileage and amount of fuel.
Answer:
// Car Instrument Simulator #include <iostream> using namespace std; // FuelGauge Class class FuelGauge { // Member variables private: // The amount of fuel, in gallons int gallons; // Member functions public: // Default constructor FuelGauge() { gallons = 0;} // Overloaded constructor that accepts // an argument for gallons. It can hold // no more than 15 gallons. FuelGauge(int g) { if (g <= 15) gallons = g; else gallons = 15; } // Accessor for the gallons int getGallons() { return gallons;} // The incrementGallons function increments // the value of the gallons member, if it // is less than 15. The car holds a maximum // of 15 gallons. void incrementGallons() { if (gallons < 15) gallons++; else cout << "FUEL OVERFLOWING!!!\n"; } // The decrementGallons function decrements // the value of the gallons member, if it // is greater than 0. void decrementGallons() { if (gallons > 0) gallons--; else cout << "OUT OF GAS!!!\n"; } }; // Odometer Class class Odometer { // Member variables private: // The current mileage int mileage; // Mileage set-point to remember when the // FuelGuage gallons were decremented. int setPoint; // Pointer to a FuelGauge object FuelGauge *fuelGauge; // Member functions public: // The constructor accepts arguments // for the mileage and the fuel guage. Odometer(int m, FuelGauge *fp) { mileage = m; setPoint = m; fuelGauge = fp; } // Accessor for the mileage int getMileage() { return mileage;} // The incrementMiles function increments // the value of the mileage member. void incrementMiles() { // Increment the mileage, but rollover // if we go past 999,999. if (mileage < 999999) mileage++; else mileage = 0; // See if we have burned a gallon of gas. This // happens every 24 miles. // // If setPoint is greater than mileage, then // the odometer has rolled over. if (setPoint > mileage) { // Add 1 million to get the actual mileage. int falseMileage = mileage + 1000000; if ( (falseMileage - setPoint) >= 24 ) { fuelGauge->decrementGallons(); setPoint = mileage; } } else { if ( (mileage - setPoint) >= 24 ) { fuelGauge->decrementGallons(); setPoint = mileage; } } } }; // Function prototypes int main() { // Create a FuelGuage object. FuelGauge fuel; // Create an Odometer object to work with the FuelGauge object. Odometer odometer(0, &fuel); // Fill the car up with gas. for (int i = 0; i < 15; i++) fuel.incrementGallons(); // Drive the car until it runs out of gas. while (fuel.getGallons() > 0) { // Drive a mile. odometer.incrementMiles(); // Display the mileage. cout << "Mileage: " << odometer.getMileage() << endl; // Display the amount of fuel. cout << "Fuel level: " << fuel.getGallons() << " gallons\n"; cout << "------------------------------\n"; } return 0; }
Leave a reply