In: Computer Science
IN C++!!
Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods:
Assume that the station and volume settings range from 1 to 10.
The radio station is X and the volume level is Y. Where X and Y are the values of variables station and volume. If the radio is off, the message is: The radio is off.
Now design and implement a test program to create a default radio object and test all class methods on the object in random order. Print the object after each method call and use meaningful label for each method call as shown in the following sample run.
Sample run:
Turn radio on:
The radio station is 1 and the volume level is 1.
Turn volume up by 3:
The radio station is 1 and the volume level is 4.
Move station up by 5:
The radio station is 6 and the volume level is 4.
Turn volume down by 1:
The radio station is 6 and the volume level is 3.
Move station up by 3:
The radio station is 9 and the volume level is 3.
Turn radio off.
The radio is off.
Turn volume up by 2: The radio is off.
Turn station down by 2: The radio is off.
****This requires some effort so please drop a like if you are satisfied with the solution****
I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference....
Code:
#include<iostream>
using namespace std;
class Radio{
private:
int station;
int volume;
bool status;
public:
Radio(){
this->station=1;
this->volume=1;
this->status=false;
}
int getStation(){
return this->station;
}
int getVolume(){
return this->volume;
}
void turnOn(){
this->status=true;
}
void turnOff(){
this->status=false;
}
void stationUp(){
if(this->status)
this->station+=1;
}
void stationDown(){
if(this->status)
this->station-=1;
}
void volumeUp(){
if(this->status)
this->volume+=1;
}
void volumeDown(){
if(this->status)
this->volume-=1;
}
void toString(){
if(this->status)
cout<<"The
radio station is "<<this->station<<" and the volume
level is "<<this->volume<<endl;
else
cout<<"The
radio is off"<<endl;
}
};
int main(){
Radio R;
cout<<"Turn Radio On:"<<endl;
R.turnOn();
R.toString();
cout<<"Turn volume up by 3:"<<endl;
for(int i=0;i<3;i++)
R.volumeUp();
R.toString();
cout<<"Move station up by 5:"<<endl;
for(int i=0;i<5;i++)
R.stationUp();
R.toString();
cout<<"Turn volume down by
1:"<<endl;
R.volumeDown();
R.toString();
cout<<"Move station up by 3:"<<endl;
for(int i=0;i<3;i++)
R.stationUp();
R.toString();
cout<<"Turn radio off:"<<endl;
R.turnOff();
R.toString();
cout<<"Turn volume up by 2: ";
for(int i=0;i<2;i++)
R.volumeUp();
R.toString();
cout<<"Turn station down by 2: ";
for(int i=0;i<2;i++)
R.stationDown();
R.toString();
return 0;
}
Output Screenshot:

Code Screenshots:


