Question

PYTHON Exercise #2: Design and implement class Radio to represent a radio object. The class defines...

PYTHON

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.
1. A private variable of type int named station to represent a station number. Set to 1.
2. A private variable of type int named volume to represent the volume setting. Set to 1.
3. A private variable of type boolean named on to represent the radio on or off. Set to false.
Page 1 of 2
4. A non-argument constructor method to create a default radio.
5. Method getStation() that returns the station.
6. Method getVolume() that returns the volume.
7. Method turnOn() that turns the radio on.
8. Method turnOff() that turns the radio off.
9. Method stationUp() that increments the station by 1 only when the radio is on.
10. Method stationDown() that decrements the station by 1 only when the radio is on.
11. Method volumeUp() that increment the volume by 1 only when the radio is on.
12. Method volumeDown() that decrements the volume by 1 only when the radio is on.
13. Method toString()to printout a meaningful description of the radio as follows(if the radio is on):
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.

PYTHON

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Please indent the code as per the provided screenshot of code. Otherwise, it raises indentation errors.

Program Screenshot:

#declare class Radio class Radio(): #Declare private variables by using underscore #before variable name #declare station var

#Implement the method stationDown #to return the value current station value def stationDown (self): if self. turned on: self#test program def main): #create an object for the radio class radio - Radio() #print the label for turn on the radio print (

#call stationUp methods to increase the station radio.stationUp () radio.stationUp () radio.stationUp () #print the label pri

Sample Output:

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 thevolume lev

Code to be Copied:

#declare class Radio

class Radio():

    #Declare private variables by using underscore

    #before variable name

    #declare station variable and set to 1

    _station=1

    #declare volume variable and set to 1

    _volume=1

    #declare turned_on variable and set to 1

    _turned_on = False

    #A non argument constructor

    #which initialize all the values

    def __init__(self):

        self.__station = 1

        self.__volume = 1

        self.__turned_on = False

    #implement method getStation()

    #to return the current station value

    def getStation(self):

        return self.__station

    #implement method getVolume()

    #to return the current Volume value

    def getVolume(self):

        return self.__volume

    #Implement the method turnOn()

    #that return true when it is on

    def turnOn(self):

        self.__turned_on = True

    #Implement the method turnOff()

    #that return true when it is off

    def turnOff(self):

        self.__turned_on = False

    #Implement the method stationUp

    #to return the value current station value

    def stationUp(self):

        if self.__turned_on:

            self.__station += 1

            if self.__station > 10:

                self.__station = 10

    #Implement the method stationDown

    #to return the value current station value

    def stationDown(self):

        if self.__turned_on:

            self.__station -= 1

            if self.__station < 1:

                self.__station = 1

    #Implement the method volumeUp

    #to return the value current volume value

    def volumeUp(self):

        if self.__turned_on:

            self.__volume += 1

            if self.__volume > 10:

                self.__volume = 10

    #Implement the method volumeDown

    #to return the value current volume value              

    def volumeDown(self):

        if self.__turned_on:

            self.__volume -= 1

            if self.__volume < 1:

                self.__volume = 1

    #implement toString() method to

    #print the station and volume values

    def toString(self):

        #if radio is turned on

        if self.__turned_on:

            #print the values

            print('The radio station is {0} and the volume level is {1}.'

                  .format(self.__station, self.__volume))

        #otherwise print the radio is off

        else:           

            print('The radio is off.')

#test program

def main():

    #create an object for the radio class

    radio = Radio()

    #print the label for turn on the radio

    print('Turn radio on:')

    radio.turnOn()

   

    #call the toString() method

    radio.toString()

    #call volumeUp methods to increase the volume

    radio.volumeUp()   

    radio.volumeUp()

    radio.volumeUp()

    #print the label

    print('Turn volume up by 3:')

    #call the toString() method

    radio.toString()

   

    #call stationUp methods to increase the station

    radio.stationUp()

    radio.stationUp()

    radio.stationUp()

    radio.stationUp()

    radio.stationUp()

    #print the label

    print('Move station up by 5:')

    #call the toString() method

    radio.toString()

    #call volumeDown methods to decrease the volume   

    radio.volumeDown()

    #print the label

    print('Turn volume down by 1:')

    #call the toString() method

    radio.toString()

    #call stationUp methods to increase the station

    radio.stationUp()

    radio.stationUp()

    radio.stationUp()

    #print the label

    print('Move station up by 3:')

  #call the toString() method

    radio.toString()

    #call method turnOff to turn off the radio

    radio.turnOff()

    #print the label for turn on the radio

    print('Turn radio off:')

    #call the toString() method

    radio.toString()

    #call volumeUp methods to increase the volume

    radio.volumeUp()

    radio.volumeUp()

    #print the label

    print('Turn volume up by 2:')

    #call the toString() method

    radio.toString()

   

    #call stationDown method to decrease the station

    radio.stationDown()

    radio.stationDown()

    #print the label

    print('Turn station down by 2:')

    #call the toString() method

    radio.toString()

   

#main method

main()

Add a comment
Know the answer?
Add Answer to:
PYTHON Exercise #2: Design and implement class Radio to represent a radio object. The class defines...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SOLVE IN PYTHON: Exercise #2: Design and implement class Radio to represent a radio object. The c...

    SOLVE IN PYTHON: 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. A private variable of type int named station to represent a station number. Set to 1. A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio...

  • ** IN JAVA **: Design and implement class Radio to represent a radio object. The class defines th...

    ** IN JAVA **: 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.A private variable of type int named station to represent a station number. Set toA private variable of type int named volume to represent the volume setting. Set to 1.A private variable of type boolean named on to represent the radio on or off. Set to false.A...

  • using visual studios C# implementing using system; be sure I can copy and paste. Exercise #2:...

    using visual studios C# implementing using system; be sure I can copy and paste. 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. 1. A private variable of type int named station to represent a station number. Set to 1. 2. A private variable of type int named volume to represent the volume setting. Set to...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors.​ 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 1. A private variable of type int...

  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • Help please 1) Define a Java class for defining the activity of a TV set. The...

    Help please 1) Define a Java class for defining the activity of a TV set. The class contains A private int data field named channel that specifies what channel is currently set for the tuner (channels range from 1 to 120, default is 1) a. b. A private int data field named volumeLevel that specifies the volume level of the TV (range is 1 to 7; default is 1) A private boolean data field named on that determines whether the...

  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...

  • C# Language The ColourControl Class The class should be public. The class defines a read-only or...

    C# Language The ColourControl Class The class should be public. The class defines a read-only or private set public property named Value of type int. The class defines a public event named ValueChanged whose delegate type is void with two parameters, object sender and EventArgs e. The public constructor for the class is a default constructor (i.e., takes no parameters). The constructor sets the Value property to 128. The class defines a public void method named IncreaseValue. The method increases...

  • 1. Please write the following program in Python 3. Also, please create a UML and write...

    1. Please write the following program in Python 3. Also, please create a UML and write the test program. Please show all outputs. (The Fan class) Design a class named Fan to represent a fan. The class contains: Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. A private int data field named speed that specifies the speed of the fan. A private bool data field named on that specifies...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT