Part II Programming 1 Read the code skeleton at the bootom a

Part II: Programming

1. Read the code skeleton at the bootom and add your own code according to the comments.

2. For this lab, if your program runs correctly, it should generate the following output on screen:

I\'m in my 2013 Mazda hot rod.

I\'m accelerating ...

Current speed: 5 mph.

Current speed: 10 mph.

Current speed: 15 mph.

Current speed: 20 mph.

Current speed: 25 mph.

Now I\'m braking ...

Current speed: 20 mph.

Current speed: 15 mph.

Current speed: 10 mph.

Current speed: 5 mph.

Current speed: 0 mph. 3.

For this lab, you just need to submit Lab10.cpp file on the following submission website (Be careful about your file\'s name, no empty space is allowed!)

Code Skeleton

// SPECIFICATION: This file contains the declaration and definitions of the

// Car class (Note: all its member functions are in-line).

// The main() program creates a Car object and uses it to

// simulate an accelerating and braking of the car

// INSTRUCTIONS: Read the following code skeleton and add your own code

// according to the comments. Ask your TA or your class-

// mates for help and/or clarification. When you see

// //---- that is where you need to add code.

//-------------------------------------------------------------------------*/

#include <iostream>

#include <string>

using namespace std;

//Declare a class Car and it has the following member variables:

// year : An int that holds the car\'s model year

// make: A string object that holds the make of the car

// speed: An int that holds the car\'s current speed

class //----

{

private:

//---- // Model year of the Car

//---- // Make of the Car

//---- // Current speed of the Car (mph)

public:

//Write the constructor

//The constructor should accept the car\'s year and make as arguments and

//assign these values to the its member varialbes. Default value for year

//is 2016 and default make is \'Unknown\', speed is 0

//----header of the constructor

{

//----

}

// Accessors for member variable year (i.e. \"get\" functions)

int getYear()

{

return year;

}

// Write an accessor called \'getMake\' for member variable make

//----

{

return //----

}

// Write an accessor called \'getSpeed\' for member variable speed

//----

// Mutators: Write a function called \'accelerate\', it should add

// 5 to the speed member variable each time it is called

void accelerate()

{

//----

}

// Mutators: Write a function called \'brake\', it should subtract

// 5 from the speed member variable each time it is called

// Note: speed can\'t be negative!

//----

}; //end of class Car declaration and definition

// ************************* main ***************************

int main()

{

//Create a Car object called \'hotRod\', which is 2013, make is \'Mazda\'

//----

// Describe the car in the following format

// \'I\'m in my 2013 Mazda hot rod\'.

cout << //----

// Write a for loop to accelerate the car object 5 times

// Each time print it speed on screen in the following format

// \'Current speed: 10 mph\'

cout << \"I\'m accelerating ... \ \ \";

for (//----)

{

//----

cout << \"Current speed: \" << //----

}

// Write a for loop to brake the car object 5 times

// Each time print it speed on screen in the following format

// \'Current speed: 5 mph\'

cout << \"\ Now I\'m braking ... \ \ \";

for //----

{

//----

cout << \"Current speed: \" << //----

}

return 0;

}

Solution

#include <iostream>
#include <string>
using namespace std;

class Car //Declare a class Car and it has the following member variables:
{
private:
int year ; // year : An int that holds the car\'s model year
string make; // make: A string object that holds the make of the car
int speed; // speed: An int that holds the car\'s current speed

public:
//Write the constructor
Car(int year,string make)
{
   this-> year = year;
   this->make = make;
   this->speed = 0;
//The constructor should accept the car\'s year and make as arguments and
//assign these values to the its member varialbes.
}
Car()
{
year = 2016;
make = \"Unknown\";
speed = 0;

//Default value for year
//is 2016 and default make is \'Unknown\', speed is 0
}
// Accessors for member variable year (i.e. \"get\" functions)
int getYear()
{
return year;
}
// Write an accessor called \'getMake\' for member variable make
//----
string getMake()
{
return make;   
}
// Write an accessor called \'getSpeed\' for member variable speed

int getSpeed()
{
   return speed;

   }
// Mutators: Write a function called \'accelerate\', it should add
// 5 to the speed member variable each time it is called
void accelerate(int speed)
{
this->speed = this->speed + 5;
}
// Mutators: Write a function called \'brake\', it should subtract
// 5 from the speed member variable each time it is called
// Note: speed can\'t be negative!
void brake(int speed)
{
   this->speed = this->speed -5;
   }
  
   }; //end of class Car declaration and definition
   // ************************* main ***************************
int main()
{
//Create a Car object called \'hotRod\', which is 2013, make is \'Mazda\'

Car hotRod(2013,\"Mazda\");
// Describe the car in the following format
// \'I\'m in my 2013 Mazda hot rod\'.
cout << \"\'I\'m in my 2013 Mazda hot rod\'.\";
// Write a for loop to accelerate the car object 5 times
// Each time print it speed on screen in the following format
// \'Current speed: 10 mph\'
cout << \"\ I\'m accelerating ... \";
for(int i=0;i<5;i++)
{
   hotRod.accelerate(5);
cout<<\"\ Current speed: \"<<hotRod.getSpeed()<<\" mph.\";
}
// Write a for loop to brake the car object 5 times/
// Each time print it speed on screen in the following format
// \'Current speed: 5 mph\'
cout << \"\ \ Now I\'m braking ... \";
for(int i=0;i<5;i++) //----
{
hotRod.brake(5);
cout << \"\ Current speed: \" << hotRod.getSpeed()<<\" mph\";
}
return 0;
}

output:

Part II: Programming 1. Read the code skeleton at the bootom and add your own code according to the comments. 2. For this lab, if your program runs correctly, i
Part II: Programming 1. Read the code skeleton at the bootom and add your own code according to the comments. 2. For this lab, if your program runs correctly, i
Part II: Programming 1. Read the code skeleton at the bootom and add your own code according to the comments. 2. For this lab, if your program runs correctly, i
Part II: Programming 1. Read the code skeleton at the bootom and add your own code according to the comments. 2. For this lab, if your program runs correctly, i
Part II: Programming 1. Read the code skeleton at the bootom and add your own code according to the comments. 2. For this lab, if your program runs correctly, i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site