In: Computer Science
Do this in python with code that I can copy and run
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.
Screenshot
-----------------------------------------------------------------------------------
Program
#Create a class Radio
class Radio:
#Private attributes
station=1
volume=1
on=False
#Default constructor
def __init__(self):
self.station = 1
self.volume = 1
self.on=False
#Return station
def getStation(self):
return
self.station
#Return volume
def getVolume(self):
return self.volume
#Turn on radio
def turnOn(self):
self.on=True
#Turn off radio
def turnOff(self):
self.on=False
#Station incerement1
def stationUp(self):
if(self.station<10
and self.on==True):
self.station+=1
#Station decrement1
def stationDown(self):
if(self.station>1 and
self.on==True):
self.station-=1
#Volume incerement1
def volumeUp(self):
if(self.volume<10 and self.on==True):
self.volume+=1
#Volume decrement 1
def volumeDown(self):
if(self.volume>1 and
self.on==True):
self.volume-=1
#to string method
def __str__(self):
if(self.on==True):
return '%s %i %s %i %s'%('The radio station is ',self.station,' and
the volume level is ',self.volume,'.')
else:
return 'The radio is off'
#Main method for test
def main():
#Radio object
radio=Radio()
#Turn on
print('Turn radio on:')
radio.turnOn()
print(' ',radio)
#Volume up
print('Turn volume up by 3:')
radio.volumeUp()
radio.volumeUp()
radio.volumeUp()
print(' ',radio)
#Station up
print('Move station up by 5:')
radio.stationUp()
radio.stationUp()
radio.stationUp()
radio.stationUp()
radio.stationUp()
print(' ',radio)
#volume down
print('Turn volume down by 1:')
radio.volumeDown()
print(' ',radio)
#Station up
print('Move station up by 3:')
radio.stationUp()
radio.stationUp()
radio.stationUp()
print(' ',radio)
#turn off radio
print('Turn radio off.')
radio.turnOff()
print(' ',radio)
#Volume up
radio.volumeUp()
radio.volumeUp()
print(' Turn volume up
by 2:',radio)
#Station down
radio.stationDown()
radio.stationDown()
print(' Turn station
down by 2:',radio)
#Program starts here
if __name__ == "__main__":
main()
------------------------------------------------------------------------------
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 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