Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise Bike (C++) Following the class diagram shown below, create the class Ex

ID: 3785482 • Letter: E

Question

Exercise Bike (C++)

Following the class diagram shown below, create the class ExerciseBike. An ExerciseBike represents a stationary bicycle. Please review the class and the sample driver code. As explained in class, there is a specific order to the way the class methods should be called. In other words, you can't start pedaling unless you actually got onto the bicycle first. You also can't set a desired level or time to ride unless you have started pedaling first. You can't get off the bike until you stop first. Your class should enforce these rules and write errors to cout as they occur. A sample driver for this class is shown below. You should probably make a more thorough driver to test your class better.

ExerciseBike

ExerciseBike( );

void getOn( );
void startPedaling( );
bool isBeingPedaled( );
void setLevel( int level );
void setTime( int minutesToRide );
void ride( int minutes );
void stop( );
void getOff( );

bool my_isOn;
bool my_isBeingPedaled
int my_Level;
int my_Time;
int my_TimeRiddenSoFar;

Sample Driver Code

cout << "--Test 1--" << endl;

ExerciseBike b;
b.getOn( );
b.startPedaling( );
b.setLevel( 10 );
b.setTime( 10 );
b.ride( 10 );
b.stop( );
b.getOff( );

cout << "--Test 2--" << endl;

ExerciseBike bad1;
bad1.startPedaling( );

cout << "--Test 3--" << endl;

ExerciseBike bad2;
bad2.getOn( );
bad2.startPedaling( );
bad2.getOff( );

cout << "--Test 4--" << endl;

ExerciseBike bad3;
bad3.getOn( );
bad3.setLevel( 10 );

cout << "--Test 5--" << endl;

ExerciseBike bad4;
bad4.stop( );

Sample Driver Output

--Test 1--
someone got on the bike...
someone started pedaling...
level set to 10
time to ride set to 10
someone has ridden 10 minutes...
someone has stopped pedaling...
someone has gotten off the bike...
--Test 2--
you can't start pedaling until you get on first!
--Test 3--
someone got on the bike...
someone started pedaling...
you can't get off the bike until you stop pedaling!
--Test 4--
someone got on the bike...
you can't set a level until you start pedaling!
--Test 5--
you can't stop pedaling until you start pedaling first!

ExerciseBike

ExerciseBike( );

void getOn( );
void startPedaling( );
bool isBeingPedaled( );
void setLevel( int level );
void setTime( int minutesToRide );
void ride( int minutes );
void stop( );
void getOff( );

bool my_isOn;
bool my_isBeingPedaled
int my_Level;
int my_Time;
int my_TimeRiddenSoFar;

Sample Driver Code

cout << "--Test 1--" << endl;

ExerciseBike b;
b.getOn( );
b.startPedaling( );
b.setLevel( 10 );
b.setTime( 10 );
b.ride( 10 );
b.stop( );
b.getOff( );

cout << "--Test 2--" << endl;

ExerciseBike bad1;
bad1.startPedaling( );

cout << "--Test 3--" << endl;

ExerciseBike bad2;
bad2.getOn( );
bad2.startPedaling( );
bad2.getOff( );

cout << "--Test 4--" << endl;

ExerciseBike bad3;
bad3.getOn( );
bad3.setLevel( 10 );

cout << "--Test 5--" << endl;

ExerciseBike bad4;
bad4.stop( );

Sample Driver Output

--Test 1--
someone got on the bike...
someone started pedaling...
level set to 10
time to ride set to 10
someone has ridden 10 minutes...
someone has stopped pedaling...
someone has gotten off the bike...
--Test 2--
you can't start pedaling until you get on first!
--Test 3--
someone got on the bike...
someone started pedaling...
you can't get off the bike until you stop pedaling!
--Test 4--
someone got on the bike...
you can't set a level until you start pedaling!
--Test 5--
you can't stop pedaling until you start pedaling first!

Explanation / Answer

// Exercise.cpp


#include<iostream>
using namespace std;

class ExerciseBike
{
private:
bool my_isOn;
bool my_isBeingPedaled;
int my_Level;
int my_Time;
int my_TimeRiddenSoFar;
public:
ExerciseBike()
{
my_isOn = 0;
my_isBeingPedaled = 0;
my_Level = 0;
my_Time = 0;
my_TimeRiddenSoFar = 0;
}
  
void getOn( )
{
my_isOn = 1;
cout <<"someone got on the bike... ";
}
void startPedaling( )
{
if (! my_isOn)
{
cout << "you can't start pedaling until you get on first! ";
return;
}
my_isBeingPedaled = 1;
cout << "someone started pedaling... ";
}
bool isBeingPedaled( )
{
return my_isBeingPedaled && my_isOn;
}
void setLevel( int level )
{
if (isBeingPedaled())
{
my_Level = level;
cout << "level set to "<< my_Level << endl;
}
else
{
cout << "you can't set a level until you start pedaling! ";
return;
}
}
void setTime( int minutesToRide )
{
if (isBeingPedaled())
{
my_Time = minutesToRide;
cout << "time to ride set to " << my_Time << endl;
}
else
{
cout << "you can't set a time until you start pedaling! ";
return;
}
}
void ride( int minutes )
{
cout << "someone has ridden " << minutes << " minutes... ";
my_TimeRiddenSoFar += minutes;
}
void stop( )
{
if (my_isOn)
{
my_isOn = 0;
cout << "someone has stopped pedaling... ";
}
else
{
cout << "you can't stop pedaling until you start pedaling first! ";
}
}
void getOff( )
{
if (my_isOn)
{
cout << "you can't get off the bike until you stop pedaling! ";
}
else
{
cout << "someone has gotten off the bike... ";
}
}
};

// main.cpp

#include <iostream>
#include "ExerciseBike.cpp"

using namespace std;

int main()
{
cout << "--Test 1--" << endl;
ExerciseBike b;
b.getOn( );
b.startPedaling( );
b.setLevel( 10 );
b.setTime( 10 );
b.ride( 10 );
b.stop( );
b.getOff( );

cout << "--Test 2--" << endl;
ExerciseBike bad1;
bad1.startPedaling( );

cout << "--Test 3--" << endl;
ExerciseBike bad2;
bad2.getOn( );
bad2.startPedaling( );
bad2.getOff( );
cout << "--Test 4--" << endl;
ExerciseBike bad3;
bad3.getOn( );
bad3.setLevel( 10 );

cout << "--Test 5--" << endl;
ExerciseBike bad4;
bad4.stop( );
  
return 0;
}

/*

Sample run

$ g++ main.cpp ExerciseBike.cpp
$ ./a.out
--Test 1--
someone got on the bike...
someone started pedaling...
level set to 10
time to ride set to 10
someone has ridden 10 minutes...
someone has stopped pedaling...
someone has gotten off the bike...
--Test 2--
you can't start pedaling until you get on first!
--Test 3--
someone got on the bike...
someone started pedaling...
you can't get off the bike until you stop pedaling!
--Test 4--
someone got on the bike...
you can't set a level until you start pedaling!
--Test 5--
you can't stop pedaling until you start pedaling first!

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote