In c++, Your classes should provide the necessary methods to perform the followi
ID: 3829879 • Letter: I
Question
In c++,
Your classes should provide the necessary methods to perform the following operations at the minimum:
-Turn the TV on or off
- Configure channels, add more channels or remove unwanted channels
- Change channel by number, i.e. go to channel 7 directly -Change channel by 1, up and down
- Change sound volume by specific number, i.e. 26 -Change sound volume by 1, up and down
- Print out the current TV’s status: for example; o “You are currently watching Channel 7 - ABC news, sound volume @18”
Here is the begining of the code:
#include <iostream>
using namespace std;
class TV
{
private:
bool switchedOn;
int currentChannel;
string channelTitle[10];
int volume;
public:
TV()
{
switchedOn = false;
currentChannel = -1;
for(int i = 0; i < 10; i++)
channelTitle[i] = "Channel" + to_string(i);
volume = 0;
}
bool isSwitchedOn() { return switchedOn; }
int getCurrentChannel()
{
if(switchedOn)
return currentChannel;
return -1;
}
string getChannelTitle()
{
if(switchedOn)
return channelTitle[currentChannel];
return "No channel";
}
int getVolume()
{
if(switchedOn)
return volume;
return 0;
}
void switchOn()
{
switchedOn = true;
currentChannel = 1;
volume = 10;
}
void setCurrentChannel(int channel)
{
currentChannel = channel;
}
void setChannelTitle(string title, int channelNumber)
{
channelTitle[channelNumber] = title;
}
void setVolume(int vol)
{
volume = vol;
}
};
Explanation / Answer
Here is the code for you:
#include <iostream>
using namespace std;
class TV
{
private:
bool switchedOn;
int currentChannel;
string channelTitle[100];
int numOfChannels;
int volume;
public:
TV()
{
switchedOn = false;
currentChannel = -1;
for(int i = 0; i < 10; i++)
channelTitle[i] = "Channel" + to_string(i);
volume = 0;
numOfChannels = 10;
}
bool isSwitchedOn() { return switchedOn; }
int getCurrentChannel()
{
if(switchedOn)
return currentChannel;
return -1;
}
string getChannelTitle()
{
if(switchedOn)
return channelTitle[currentChannel];
return "No channel";
}
int getVolume()
{
if(switchedOn)
return volume;
return 0;
}
//-Turn the TV on or off
void switchOn()
{
switchedOn = true;
currentChannel = 1;
volume = 10;
}
void switchOff()
{
switchedOn = false;
currentChannel = -1;
volume = 0;
}
//- Configure channels, add more channels or remove unwanted channels
void addChannels(int additionalChannels)
{
numOfChannels += additionalChannels;
}
void removeChannels(string titleToRemove)
{
int i = 0;
for(i = 0; i < numOfChannels; i++)
if(channelTitle[i].compare(titleToRemove) == 0)
break;
for(; i < numOfChannels-1; i++)
channelTitle[i] = channelTitle[i+1];
numOfChannels--;
}
//- Change channel by number, i.e. go to channel 7 directly
void setCurrentChannel(int channel)
{
currentChannel = channel;
}
//-Change channel by 1, up and down
void moveChannelUp()
{
currentChannel++;
if(currentChannel == numOfChannels)
currentChannel = 0;
}
void moveChannelDown()
{
currentChannel--;
if(currentChannel == -1)
currentChannel = numOfChannels-1;
}
void setChannelTitle(string title, int channelNumber)
{
channelTitle[channelNumber] = title;
}
//- Change sound volume by specific number, i.e. 26
void setVolume(int vol)
{
volume = vol;
}
//-Change sound volume by 1, up and down
void increaseVolume()
{
volume++;
}
void decreaseVolume()
{
volume--;
if(volume == -1)
volume = 0;
}
//- Print out the current TV’s status: for example;
//o “You are currently watching Channel 7 - ABC news, sound volume @18”
void printStatus()
{
if(isSwitchOn())
{
cout << "You are currently watching Channel " << currentChannel;
cout << "- " << channelTitle[currentChannel] << ", sound volume @" << volume << endl;
}
else
cout << "TV is switched off." << endl;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.