Question

In: Computer Science

Do this in python with code that I can copy and run Design and implement class...

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.

  1. A private variable of type int named station to represent a station number. Set to
  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.
  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.

Solutions

Expert Solution

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


Related Solutions

hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
This needs to be in Python and each code needs to be separated. Design a class...
This needs to be in Python and each code needs to be separated. Design a class for a fast food restaurant meals that should have 3 attributes; Meal Type (e.g., Burger, Salad, Taco, Pizza); Meal size (e.g., small, medium, large); Meal Drink (e.g., Coke, Dr. Pepper, Fanta); and 2 methods to set and get the attributes. Write a program that ask the user to input the meal type, meal size, and meal drinks. This data should be stored as the...
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
Please do in java with code available for copy and with comments so I can follow...
Please do in java with code available for copy and with comments so I can follow along :)\ Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns()....
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
the purpose of the code is to implement a stack how can i convert this code...
the purpose of the code is to implement a stack how can i convert this code in a way that these changes apply to it? // simplify the logic of the main // getline(cin, line) needs to be changed to a normal cin >> line; //make a function for 'list' // not allowed temporary stack (stack s2) //merge the catches (errors) // introduce another function for main // avoid repetitive code here is the code: #include #include #include #include #include...
what is the solution of this code? Also how would I run it in python to...
what is the solution of this code? Also how would I run it in python to check? q = Queue() q.enqueue(10) q.enqueue(12) q.enqueue(15) for i in range(len(q)): → if len(q) % 2 == 0: → → q.enqueue(q.dequeue()) → else: → → q.dequeue() print(q)
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
I need to implement a code in python to guess a random number. The number must...
I need to implement a code in python to guess a random number. The number must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT