Answer to Question #156827 in C++ for Dumbo

Question #156827

UML AND C++


Given the class diagram below, create a simple Television remote controlled

application. Classes and functions shown in the diagram must be complete and

properly declared.


Use the following program description:

 The program has 2 TV objects. One as Cable TV and the other is Regular TV

 Ask the user to enter the type of Television. This will automatically turn on the

Television.

 The change of channel and volume shall only be done when the tv is on (Status

=1).

 Changes in channels and volume must be handled by an object of RemoteC.

 The initial channel for Cable TV is 10 while Regular TV is 2. Initial volume regardless

of the type of Television is 10. Display these values once the TV is turned on.

 Ask the user for an action (change volume or change channel).

 The maximum channel for Cable TV is 50 while Regular TV is 10. Maximum

Volume is 100. Action is invalid if the changes exceeds to its maximum value.

 Continue to ask user for an action until the user enters ‘0’ (zero) to turn off the TV


1
Expert's answer
2021-01-22T18:26:51-0500
#include <iostream>


using namespace std;


//Base class for all televisions
class Television
{
    //All member variables and functions are public
    public:
        int volume;
        int channel;
        int status;
        int maxChannel;


        void turnOn()
        {
            //Set the status to 1
            cout<<"Turning on your TV"<<endl;
            status = 1;
        }
        void turnOff()
        {
            //Set the status to 1
            cout<<"Turning on your TV"<<endl;
            status = 1;
        }
        //Display settings status
        void displaySettingStatus()
        {
            //Display only if tv is on
            if(status == 1)
            {
                cout<<"The TV Volume is: "<<volume<<endl;
                cout<<"The TV Channel is: "<<channel<<endl;
            }


        }




};
//Cable TV inherits from Television base class
class CableTV : public Television
{






};


//Regular TV inherits from Television base class
class RegularTV : public Television
{




};


//Remote control class
class RemoteC
{
    //All member variables and functions are public
    public:
        void decreaseVolume(Television& tv)
        {
            tv.volume -=1;
        }
        //Pass tv object by reference
        void increaseVolume(Television& tv)
        {
            tv.volume +=1;
        }




        void setChannel(Television& tv, int cn)
        {
            tv.channel = cn;
        }
        void turnOn(Television& tv)
        {
            tv.status = 1;
        }
        void turnOff(Television& tv)
        {
            cout<<"Turning off tv.."<<endl;
            tv.status = 0;
        }
};


int main()
{
    //Television class objects
    //Cable Tv
    Television cableTv;
    //Init volume and  and maxchannel
    cableTv.channel = 10;
    cableTv.volume = 10;
    cableTv.maxChannel = 50;




    //Regular TV
    Television regularTV;
    //Init volume, channel and maxchannel
    regularTV.channel = 2;
    regularTV.volume = 10;
    regularTV.maxChannel = 10;


    //Remote
    RemoteC remote;


    //Prompt user to
    int tvType;
    cout<<"Please enter the type of television to watch.."<<endl;
    cout<<"1- Cable TV \n2-Regular TV"<<endl;
    cout<<"TV Choice Type: ";
    cin>>tvType;
    //If user choice is cable tv
    if(tvType == 1)
    {
        //Turn on tv
        cableTv.turnOn();
        cout<<"Welcome to Cable TV"<<endl;
        cableTv.displaySettingStatus();


        //Ask user what to change
        int watchingChoice = 1;
        while(watchingChoice != 0)
        {
            cout<<"Please choose what you would like to do. \n1. Change Volume \n2. Change Channel \n0. To turn off TV"<<endl;
            cout<<"Your Choice: ";
            cin>>watchingChoice;
            //Change volume
            if(watchingChoice == 1)
            {
                //Ask user if to increase or decrease
                int volumeChoice;
                cout<<"Do you want to \n1. Increase volume \n2. Decrease volume"<<endl;
                cin>>volumeChoice;
                //If increase volume if volume is in range and tv is on
                if(volumeChoice == 1 && (cableTv.volume >= 0 && cableTv.volume <= 100) && cableTv.status == 1)
                {
                    //Increase volume
                    remote.increaseVolume(cableTv);
                    cableTv.displaySettingStatus();
                }
                if(volumeChoice == 2 && (cableTv.volume >= 0 && cableTv.volume <= 100) && cableTv.status == 1)
                {
                    //Increase volume
                    remote.decreaseVolume(cableTv);
                    cableTv.displaySettingStatus();
                }
                else
                {
                    cout<<"Maximum or minimum volume reached."<<endl;
                }
            }


            //Change channel
            if(watchingChoice == 2)
            {
                //Ask user if to increase or decrease
                int channelChoice;
                cout<<"Please enter channel to watch: "<<endl;
                cin>>channelChoice;
                //As long as channel is not beyond the supported set it
                if(channelChoice > 0 && channelChoice <= cableTv.maxChannel)
                {
                    remote.setChannel(cableTv, channelChoice);
                    cableTv.displaySettingStatus();
                }


                else
                {
                    cout<<"Invalid choice. Supported channel is between 1 and "<<cableTv.maxChannel<<endl;
                }
            }
        }
        //Turn off tv
        cableTv.turnOff();
    }




    //If user choice is regular tv
    if(tvType == 2)
    {
        //Turn on tv
        regularTV.turnOn();
        cout<<"Welcome to Cable TV"<<endl;
        regularTV.displaySettingStatus();


        //Ask user what to change
        int watchingChoice = 1;
        while(watchingChoice != 0)
        {
            cout<<"Please choose what you would like to do. \n1. Change Volume \n2. Change Channel \n0. To turn off TV"<<endl;
            cout<<"Your Choice: ";
            cin>>watchingChoice;
            //Change volume
            if(watchingChoice == 1)
            {
                //Ask user if to increase or decrease
                int volumeChoice;
                cout<<"Do you want to \n1. Increase volume \n2. Decrease volume"<<endl;
                cin>>volumeChoice;
                //If increase volume if volume is in range and tv is on
                if(volumeChoice == 1 && (regularTV.volume >= 0 && regularTV.volume <= 100) && regularTV.status == 1)
                {
                    //Increase volume
                    remote.increaseVolume(regularTV);
                    regularTV.displaySettingStatus();
                }
                if(volumeChoice == 2 && (regularTV.volume >= 0 && regularTV.volume <= 100) && regularTV.status == 1)
                {
                    //Increase volume
                    remote.decreaseVolume(regularTV);
                    regularTV.displaySettingStatus();
                }
                else
                {
                    cout<<"Maximum or minimum volume reached."<<endl;
                }
            }


            //Change channel
            if(watchingChoice == 2)
            {
                //Ask user if to increase or decrease
                int channelChoice;
                cout<<"Please enter channel to watch: "<<endl;
                cin>>channelChoice;
                //As long as channel is not beyond the supported set it
                if(channelChoice > 0 && channelChoice <= regularTV.maxChannel)
                {
                    remote.setChannel(regularTV, channelChoice);
                    regularTV.displaySettingStatus();
                }


                else
                {
                    cout<<"Invalid choice. Supported channel is between 1 and "<<regularTV.maxChannel<<endl;
                }
            }
        }
        //Turn off tv
        regularTV.turnOff();
    }
    else
    {
        cout<<"Invalid television type. Try again"<<endl;


    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog