32.7k views
2 votes
Your solution should for this assignment should consist of five (5)files:============================================== FuelGauge.h (class specification file)FuelGauge.cpp (class implementation file)Odometer.h (class specification file)Odometer.cpp (class implementation file)200_assign6.cpp (application program)=================================================★ For your sixth programming assignment, you will be implementing a program that uses two(2)classes that work together to simulate a car’s fuel gauge and odometer. The classes you will design are: TheFuelGaugeClass: 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.TheOdometerClass: 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 execeeded, the odometer resets the current mileage to 0.↘To be able to work with aFuelGaugeobject. It should decrease theFuelGaugeobject’s current amount of fuel by 1 gallon for every 24 miles traveled. (The car’s fuel economy is 24 miles per gallon)The FuelGaugeclass should have at least one constructor, and the appropriate member functions to get the number of gallons, increment the number of gallons, and decrement the number of gallons.★ The odometer class should have at least one constructor, and the appropriate member functions to get the mileage and increment the mileage (unfortunately you cannot decrement the mileage!).★ In the odometer class, you can have a member variable that is a pointer to a FuelGauge object. This is how you can establish a relationship between the two classes that allow them to work together. For every mile the car is driven, the odometer is increased by 1, and for every 24 miles the car is driven, the number of gallons is reduced by one.★ So in the Odometer class, one of the private member variables should be of type fuel gauge* (pointer to a FuelGauge object).★ Demonstrate the two classes by creating instances(objects)of each. Simulate filling 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.

1 Answer

4 votes

Answer: provided in the explanation section

Step-by-step explanation:

This was implemented in C++

Filename: Car.cpp

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include "Odometer.h"

using namespace std;

int main()

{

char response;

do

{

FuelGuage* myFuel = new FuelGuage(6);

int fuelLevel = myFuel->getFuel();

cout << "Car gas level: " << fuelLevel << endl;

cout << "Car is filling up" << endl;

while(myFuel->getFuel() < 15)

{

myFuel->addFuel();

}

Odometer* car = new Odometer(myFuel, 999990);

cout << "Car gas level: " << car->getFuelGuage()->getFuel() << endl;

cout << "Car is off!" << endl;

cout << "--------------------------" << endl;

while(car->getFuelGuage()->getFuel() > 0)

{

car->addMile();

int miles = car->getMileage();

cout << "Mileage: " << setw(6) << miles << ", Fuel Level: " << car->getFuelGuage()->getFuel() << endl;

cout<<"--------------------------------------------------------------"<<endl;

}

delete myFuel;

delete car;

cout << "Would you like to run the car again(Y/N)? ";

cin >> response;

system("cls||clear");

} while(toupper(response) != 'N');

return 0;

}

Filename: FuelGuage.cpp

#include "FuelGuage.h"

FuelGuage::FuelGuage()

{

init();

}

FuelGuage::FuelGuage(int fuel)

{

init();

this->fuel = fuel;

}

FuelGuage::FuelGuage(const FuelGuage& copy)

{

this->fuel = copy.fuel;

}

void FuelGuage::init()

{

this->fuel = 0;

}

int FuelGuage::getFuel()

{

return fuel;

}

void FuelGuage::addFuel()

{

fuel++;

}

void FuelGuage::burnFuel()

{

fuel--;

}

Filename:FuelGuage.h

#ifndef FUEL_GUAGE_H

#define FUEL_GUAGE_H

class FuelGuage

{

private:

int fuel;

void init();

public:

FuelGuage();

FuelGuage(int fuel);

FuelGuage(const FuelGuage& copy);

int getFuel();

void addFuel();

void burnFuel();

};

#endif

Filename : Odometer.cpp

#include "Odometer.h"

Odometer::Odometer()

{

init();

}

Odometer::Odometer(FuelGuage* inFuel, int inMileage)

{

init();

this->fuel = new FuelGuage(*inFuel);

this->mileage = inMileage;

this->milesSinceAddingFuel = 0;

}

void Odometer::init()

{

fuel = new FuelGuage();

mileage = 0;

milesSinceAddingFuel = 0;

}

FuelGuage* Odometer::getFuelGuage()

{

return fuel;

}

int Odometer::getMileage()

{

return mileage;

}

void Odometer::addMile()

{

if(mileage < 999999)

{

mileage++;

milesSinceAddingFuel++;

}

else

{

mileage = 0;

milesSinceAddingFuel++;

}

if((milesSinceAddingFuel % 24) == 0)

{

fuel->burnFuel();

}

}

void Odometer::resetMiles()

{

milesSinceAddingFuel = 0;

}

Odometer::~Odometer()

{

delete fuel;

}

Filename: Odometer.h

#ifndef ODOMETER_H

#define ODOMETER_H

#include "FuelGuage.h"

class Odometer

{

private:

void init();

int mileage;

int milesSinceAddingFuel;

FuelGuage* fuel;

public:

Odometer();

Odometer(FuelGuage* inFuel, int inMileage);

FuelGuage* getFuelGuage();

int getMileage();

void addMile();

void resetMiles();

~Odometer();

};

#endif

User Dropbeardan
by
3.6k points