#include "stdstuff.h" #include "GasTank.h" // constructs a GasTank object (for a car) that gets 400km on a tank of gas, with the tank currently empty GasTank::GasTank() { kmPerTank = 400; kmAvail = 0; // tank is empty } // constructs a GasTank object (for a car) that gets the given number of km on a tank of gas, with the tank currently // empty. . // Throws invalid_argument exception if argument is less than or equal to 0. GasTank::GasTank(int numKms) { if (numKms <= 0) { // **must** set fields to get full marks!! kmPerTank = 400; kmAvail = 0; // tank is empty // I used the default -- doesn't really matter as long as they are set to something throw invalid_argument("GasTank::GasTank -- numKms must be greater than 0."); } kmPerTank = numKms; kmAvail = 0; // tank is empty } // the GasTank object has its gas tank filled void GasTank::fill() { kmAvail = kmPerTank; // tank is now full } // the GasTank object drives the given number of kms. If unsuccessful (not enough gas), this method has no effect. // Throws invalid_argument exception if argument is less than 0. void GasTank::drive(int numKms) { if (numKms < 0) throw invalid_argument("GasTank::drive -- numKms must be greater than or equal to 0."); if (numKms > kmAvail) return; // do nothing, as there's not enough gas for that drive // otherwise subtract the number of kilometers driven from the number available kmAvail -= numKms; } // returns the number of kilometers that this GasTank object can drive before running out of gas. // (Does not change the state of the GasTank.) int GasTank::kmLeft() const { return kmAvail; } // prints the percent level of the GasTank object, i.e. 100 = full, 0 = empty, etc. // (Does not change the state of the GasTank.) void GasTank::printLevel() const { cout << 100.0* kmAvail / kmPerTank; }