Question

In: Computer Science

Write a class Volume that stores the volume for a stereo that has a value between...

  1. Write a class Volume that stores the volume for a stereo that has a value between 0 and 11. Usage of the class is listed below the problem descriptions. Throughout the class you must guarantee that:
    1. The numeric value of the Volume is set to a number between 0 and 11. Any attempt to set the value to greater than 11 will set it to 11 instead, any attempt to set a negative value will instead set it to 0.
    2. This applies to the following methods below: __init__, set, up, down

You must write the following methods:

  1. __init__ - constructor.   Construct a Volume that is set to a given numeric value, or, if no number given, defaults the value to 0.   (Subject to 0 <= vol <=11 constraint above.)
  2. __repr__ - converts Volume to a str for display, see runs below.
  3. set – sets the volume to the specified argument (Subject to 0 <= vol <=11 constraint above.)
  4. get – returns the numeric value of the Volume
  5. up – given a numeric amount, increases the Volume by that amount. (Subject to 0 <= vol <=11 constraint above.)
  6. down – given a numeric amount, decreases the Volume by that amount. (Subject to 0 <= vol <=11 constraint above.)
  7. __eq__ - implements the operator ==.   Returns True if the two Volumes have the same value and False otherwise.

use python 3.7

Solutions

Expert Solution

The question is missing the sample outputs, nevertheless the class has been fully implemented.

==============================================================================

class Volume():

    def __init__(self, vol=0):
        self.volume = vol
        if self.volume > 11:
            self.volume == 11
        if self.volume < 0:
            self.volume = 0

    def __repr__(self):
        return 'Stereo Volume: {}'.format(self.volume)

    def set(self, vol):
        self.volume = vol
        if self.volume > 11:
            self.volume == 11
        if self.volume < 0:
            self.volume = 0

    def get(self):
        return self.volume

    def up(self):
        self.volume += 1
        if self.volume > 11:
            self.volume == 11

    def down(self):
        self.volume -= 1
        if self.volume < 0:
            self.volume = 0

    def __eq__(self, other):
        if isinstance(other, Volume):
            return self.volume == other.volume
        else:
            return False


Related Solutions

Write a class DataSet that stores a number of values of type double in an array...
Write a class DataSet that stores a number of values of type double in an array list. Provide methods to insert a value to the array list and to compute the sum, average, and maximum value. Please put a note on what you did is for what. So I can understand this question better. Code provided: import java.util.*; public class DataSet {    private ArrayList<Double> data; /** Constructs an empty data set. @param maximumNumberOfValues the maximum this data set can...
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
The following information is available for Redwood Corporation for a sales volume of 500 stereo speakers...
The following information is available for Redwood Corporation for a sales volume of 500 stereo speakers for the past month: Total Per Unit Sales $112,500 $225 Less: Variable expenses 40,000 80 Contribution margin $ 72,500 $145 Less: Fixed expenses $ 17,500 Net operating income $ 55,000 If sales increase by $51,750, net income will increase by what amount? Select one: A. $33,350 B. $30,000 C. $20,000 D. $22,000
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
Write a paragraph about the determinants of airway resistance and the relationship between lung volume and...
Write a paragraph about the determinants of airway resistance and the relationship between lung volume and airway resistance.
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT