SHOW THAT YOUR CODE WORKS WITH THE GIVEN QUESTION C QUESTION

SHOW THAT YOUR CODE WORKS WITH THE GIVEN QUESTION!!!

C++ QUESTION!!

Problem 1 (10 points) Design and implement an ADT called Car. Support the following operations: a constructor, ability to set make and model, ability to shift gear up or down (assume a max of 6-speed), ability to shift lanes (left or right; assume a max of three lanes), and accelerate and decelerate (in units of 5 mph). Instantiate some Car objects in your main function and take it for a spin.

Solution

#include <iostream>

#define MAX_GEAR 6

#define MIN_GEAR 0

#define MAX_LANE 1

#define MIN_LANE -1

using namespace std;

class Car {

string carMake;

string carModel;

int currentGear = 0;

int currentLane = 0;

int currentSpeed = 0;

public:

Car(){

//default constructor

}

Car(string make, string model){

carMake = make;

carModel = model;

cout<<\"Here is our brand new \"+model + \" from \" + make + \"\ \";

}

void setMake(string );

void setModel(string );

int changeGear(bool gearUp);

int changeLane(bool toRight);

int changeSpeed(bool speedUp);

string showLaneText(int lane){

switch(lane){

case -1:

return \"Our \"+carModel+\" is in left lane now.\ \";

break;

case 0:

return \"Our \"+carModel+\" is in middle lane now.\ \";

break;

case 1:

return \"Our \"+carModel+\" is in right lane now.\ \";

break;

}

}

};

void Car::setMake (string make) {

carMake = make;

}

void Car::setModel (string model) {

carModel = model;

}

int Car::changeGear (bool gearUp) {

//This function would be used to change the gear

//gearUp would tell us whether we want gear up and gear down

cout<<\"Changing gear...\";

if (gearUp){

if (currentGear == MAX_GEAR){

cout<<\"Our \"+carModel+\" is in top gear.\ \";

}else{

currentGear += 1;

cout<<\"Our \"+carModel+\" is in gear \" << currentGear << \" now.\ \";

}

}else{

if (currentGear == MIN_GEAR){

cout<<\"Our \"+carModel+\" is in 0th gear. Let\'s get it up.\";

}else{

currentGear -= 1;

cout<<\"Our \"+carModel+\" is in gear \" << currentGear << \" now.\ \";

}

}

}

int Car::changeLane(bool toRight){

//This function would be used to change the lane

//toRight would tell us whether we want to change to right lane or left lane

cout<<\"Changing lane...\";

if (toRight){

if (currentLane == MAX_LANE){

cout<<\"Oh ho, it\'s footpath on the right.Keep left.\ \";

}else{

currentLane += 1;

cout<<showLaneText(currentLane);

}

}else{

if (currentLane == 0){

cout<<\"Oh ho, it\'s footpath on the left.Keep right.\ \";

}else{

currentLane -= 1;

cout<<showLaneText(currentLane);

}

}

}

int Car:: changeSpeed(bool speedUp){

//This function would be used to change the speed

//speedUp would tell us whether we want to accelerate or deccelarate

if (speedUp){

cout<<\"Speeding up ... hold tight.... \";

currentSpeed += 5;

cout<<carModel+\" is moving at \"<<currentSpeed<<\" now\ \";

}else{

cout<<\"Speeding down ... relax.... \";

currentSpeed += 5;

cout<<carModel+\" is moving at \"<<currentSpeed<<\" now\ \";

}

}

int main() {

// your code goes here

Car car1 (\"Ferrari\", \"F430 JDC\");

Car car2 (\"Bugatti\", \"Veyron\");

car1.changeGear(true);

car1.changeSpeed(true);

car2.changeGear(true);

car2.changeSpeed(true);

car1.changeGear(true);

car1.changeLane(true);

car1.changeSpeed(true);

car1.changeGear(true);

car1.changeSpeed(true);

car2.changeGear(true);

car1.changeSpeed(true);

car1.changeGear(true);

car1.changeSpeed(true);

car2.changeSpeed(true);

car1.changeLane(true);

car1.changeGear(false);

car2.changeLane(true);

car2.changeGear(true);

car2.changeSpeed(true);

car1.changeSpeed(true);

car1.changeLane(false);

car2.changeLane(true);

car2.changeSpeed(true);

car2.changeGear(true);

car1.changeSpeed(false);

car2.changeSpeed(true);

car1.changeSpeed(false);

car2.changeSpeed(true);

car2.changeGear(false);

car2.changeSpeed(false);

car2.changeSpeed(false);

return 0;

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site