Instructions of the assignment: (C++) Design a set of classes that work together
ID: 3810945 • Letter: I
Question
Instructions of the assignment: (C++)
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 be able to increment the amount of fuel by 1 gallon. This simulates putting 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 car's odometer. Its responsibilities are:
--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 0.
--To be able to work with a FuelGauge object. It should decrease the FuelGauge object s current amount of fuel by 1 gallon for every 24 miles traveled. (The car's fuel economy is 24 miles per gallon.)
Demonstrate the classes by creating instances of each. Simulate 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.
Explanation / Answer
code:
#include<iostream>
using namespace std;
class FuelGauge{//This class will simulate a fuel gauge.
int fuel;
public:
FuelGauge();
FuelGauge(int);
int getFuel();//get current amount of fuel, in gallons.
void printFuel();//print current amount of fuel, in gallons.
void incrementFuel();//increment the amount of fuel by 1 gallon.
//This simulates putting fuel in the car. (The car can hold a maximum of 15 gallons.)
void decrementFuel();//• 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.
};
FuelGauge::FuelGauge(){ fuel=0;}
FuelGauge::FuelGauge(int filled){
if(filled<=15)
fuel=filled;
else{
cout<<" The tank could only hold a maximum of 15 gallon.";
fuel=15;
}
}
int FuelGauge::getFuel(){
return fuel;
}
void FuelGauge::printFuel(){
cout<<" Current Fuel (gallons): "<<fuel;
}
void FuelGauge::incrementFuel(){
if (fuel<15)
fuel++;
else
cout<<" The tank is already full. Fuel could not be added.";
}
void FuelGauge::decrementFuel(){
if (fuel>=1)
fuel--;
else
cout<<" The tank is already empty. Fuel could not be used.";
}
class odometer{
int mileage;
public:
odometer();
odometer(int);
int getMileage();
void printMileage();
void incrementMileage();
};
odometer::odometer(){
mileage=0;
}
odometer::odometer(int m){
if(m>=0&&m<=999999)
mileage=m;
else{
cout<<endl<<m<<" is outside mileage range. Mileage reset to 0. ";
}
}
int odometer::getMileage(){
return mileage;
}
void odometer::printMileage(){
cout<<" Current Mileage (miles): "<<mileage;
}
void odometer::incrementMileage(){//increment the current mileage by 1 mile.
if(mileage<999999) //The maximum mileage the odometer can store is 999,999 miles.
mileage++;
else
mileage=0; //the odometer resets the current mileage to 0.
}
int main(){
odometer OMeter;
FuelGauge FuelG;
OMeter.printMileage();
FuelG.printFuel();
//filled tank
do{
cout<<"Filling fuel...";
FuelG.incrementFuel();
FuelG.printFuel();
}while(FuelG.getFuel()<=15);
//loop until empty gas
cout<<"Now driving car...";
while(FuelG.getFuel()>0){
OMeter.incrementMileage();
if(OMeter.getMileage()%24==0){
FuelG.decrementFuel();
}
OMeter.printMileage();
FuelG.printFuel();
}
return 0;
}