Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete code/fix in C++: #include #include #include // std::string, std::to_string, std::fixed #include // std::setprecision using namespace std; class Car { private: float mileage; // Total

Complete code/fix in C++:

#include #include #include // std::string, std::to_string, std::fixed #include // std::setprecision

using namespace std;

class Car { private: float mileage; // Total cumulative mileage traveled by the Car float gasInTank; // Total gas in gallons held in the Car's tank float milesPerGallon; // Car's gas mileage in miles per gallon (mpg) float tripMileage; // Mileage traveled by Car on its last trip float tripGasConsumed; // Gallons of gas consumed by Car during last trip

public: // Constructors /** * Car instance with an initial milesPerGallon (mpg). * The other parameters are explicitly set to 0.0; */ Car(float mpg) { milesPerGallon = mpg; mileage = 0.0; gasInTank = 0.0; tripMileage = 0.0; tripGasConsumed = 0.0; }

// Constructor declared Car(float mileage, float gasInTank, float milesPerGallon);

// Getters float getMileage() {return mileage;} float getGasInTank() {return gasInTank;} float getMilesPerGallon() {return milesPerGallon;} float getTripMileage() {return tripMileage;} float getTripGasConsumed() {return tripGasConsumed;}

// Setters void setMileage(double mileage); void setGasInTank(double gasInTank); void setMilesPerGallon(double milesPerGallon); void setTripMileage(double tripMileage) {this->tripMileage = tripMileage;} void setTripGasConsumed(double tripGasConsumed) {this->tripGasConsumed = tripGasConsumed;}

// DO NOT EDIT OR CHANGE THIS METHOD void display() // Used to display test case results { cout cout cout getMileage(); cout getGasInTank(); cout getMilesPerGallon(); cout getTripMileage(); cout getTripGasConsumed() }

// Instance methods provided //Add the given gallons of gas to the target Car void pumpGas(float gallons) { gasInTank += gallons;}

//Resets both the trip mileage and trip gas consumed //to 0 to start measuring the next (new) trip. void resetTrip() { tripMileage = 0; tripGasConsumed = 0; }

// Methods declared void pumpGasInLiters(float liters); void addTripMiles(float miles); void updateMPG(float litersOfGas, float milesTravelled); bool canReach(float miles); };

int main(){ cout

cout

//Example of Test Cases

//Exercise #1 Car Car11 = Car(12000, 2.5, 25); Car11.display();

//Expected Result cout

//Exercise #2 Car Car21 = Car(27); Car21.display(); Car21.pumpGasInLiters(45); Car21.display();

//Expected Result cout

//Exercise #3 Car Car31 = Car(25); Car31.pumpGas(27); Car31.display(); Car31.addTripMiles(400); Car31.display();

//Expected Result cout

//Exercise #4 Car Car41 = Car(25); Car41.display(); Car41.updateMPG(35.5, 228); Car41.display();

//Expected Result cout

//Exercise #5 Car Car51 = Car(33); Car51.pumpGas(23); cout cout

//Expected Result cout

//Exercise #6 Car Car61 = Car(30); Car61.setMileage(1200); Car61.setGasInTank(15); Car61.setMilesPerGallon(25); Car61.display();

//Expected Result cout

return 0; }

/********************************************************************* * EXERCISE #1 * * Create a constructor that can be used to create a Car object with * a specified mileage, gas (in the tank) and miles-per-gallons (MPG). * Useful if you need to create a "used" Car object with 12,000 miles, * 2.5 gallons of gas in the tank and a rating of 25 MPG as follows: * Car *usedCar = new Car(12000, 2.5, 25) */ Car::Car(float mileage, float gasInTank, float milesPerGallon) { // TODO -- YOUR CODE HERE }

/********************************************************************* * EXERCISE #2 * * Add liters of gas to the target Car, adjust gallons of gas in tank. * HINT -- convert liters to gallons 1 gallon = 3.7854 liters. */ void Car::pumpGasInLiters(float liters) { // TODO -- YOUR CODE HERE }

/********************************************************************* * EXERCISE #3 * * Record additional mileage traveled by the target Car after a new trip * of given miles. Update total mileage and trip mileage accordingly. * Reduce the gas in the tank by the total gas consumed (needed) by the * target Car to travel the given distance in miles, use the car's mpg. */ void Car::addTripMiles(float miles) { // TODO -- YOUR CODE HERE }

/********************************************************************* * EXERCISE #4 * * Add a method to update the gas mileage (mpg) in the target Car to the * mileage (mpg) achieved in a given trip that consumed given litters of gas * to travel given miles. The result should be rounded to the nearest integer. * HINT -- use round function from C++ Library * HINT -- remember that 1 gallon = 3.7854 liters */ void Car::updateMPG(float litersOfGas, float milesTravelled) { // TODO -- YOUR CODE HERE }

/********************************************************************* * EXERCISE #5 * * Returns 1 (true) if and only if the target Car can travel for the * given (parameter) miles with the gas currently held in the tank. * Returns 0 (false) otherwise. */ bool Car::canReach(float miles) { // TODO -- YOUR CODE HERE return 0; // Dummy return }

/********************************************************************* * EXERCISE #6 BONUS!! * * Define the setters for mileage, gasInTank, and milesPerGallon */ // TODO -- YOUR CODE HERE void Car::setMileage(double mileage){} void Car::setGasInTank(double gasInTank){} void Car::setMilesPerGallon(double milesPerGallon){}

complete expected results in C++

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Define paraphrasing and reflecting.

Answered: 1 week ago