any attempt to set a negative value will instead set it to 0. · This applies to the following methods below: __init__, set, up, down   Write the following methods: · __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.) · __repr__ - converts Volume to a st

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please use Python for this question:

1. Develop 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.

 

The class must guarantee that:

· 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.

· This applies to the following methods below: __init__, set, up, down

 

Write the following methods:

· __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.)

· __repr__ - converts Volume to a str for display, see runs below.

· set - sets the volume to the specified argument (Subject to 0 <= vol <=11 constraint above.)

· get - returns the numeric value of the Volume

· up - given a numeric amount, increases the Volume by that amount. (Subject to 0 <= vol <=11 constraint above.)

· down - given a numeric amount, decreases the Volume by that amount. (Subject to 0 <= vol <=11 constraint above.)

· __eq__ - implements the operator ==. Returns True if the two Volumes have the same value and False otherwise.

 

Output for question 1 will be in the attached images.

 

 

2. Write standalone function partyVolume() that takes accepts one argument, a string containing the name of a file. The objective of the function is to determine the a Volume object that is the result of many people at a party turning the Volume up and down. More specifically: the first line of the file is a number that indicates the initial value of a Volume object. The remaining lines consist of a single character followed by a space followed by a number. The character will be one of 'U" or 'D' which stand for "up" and "down" respectively. The function creates a new Volume object and then process each line of the file by calling the appropriate method of the Volume object which changes the value of the Volume. The function then returns the final Volume object.

 

· This is a standalone function, it should NOT be inside (indented within) the class. It should be listed in the module after the Volume class and without indentation.

 

 

· Note that the first line of the file will be treated differently than all the other lines. Probably the easiest way is to 1) open the file, 2) call the .readline() method (no s!) which reads a single line, the initial value of the Volume, then 3) call the .readlines() method which reads the rest of the lines. Item 2) will be used to set the initial Volume and 3) will be iterated over to represent turning the volume up and down some number of time.

·Return the final Volume object.

Here is what is included in the party1.txt, party2.txt, and party3.txt files:

party1.txt:
 
2.35
U 5
D 3
U 2
 
 
party2.txt:
 
18.3
D 8.5
U 0.0
D 2.0
D 6.0
U 8.75
D 5.5
D 2.5
D 0.75
U 9.75
D 9.0
U 7.0
D 5.5
D 0.0
U 3.75
U 6.0
U 1.25
D 4.5
D 2.75
D 9.5
U 6.5
D 6.25
D 2.0
U 3.75
D 1.75
U 9.0
U 1.0
D 2.75
D 3.75
U 2.75
U 5.75
D 8.25
D 2.75
U 3.75
D 0.25
D 8.0
D 2.0
D 3.5
D 0.0
U 8.5
U 1.0
U 8.5
U 1.0
D 6.25
U 5.0
U 5.5
D 3.25
U 6.5
U 7.25
D 7.25
D 0.0
 
party3.txt:
 
-3.5
U 2.5
U 9.75
D 9.5
D 5.0
U 2.75
U 4.5
D 1.75
D 8.25
D 1.25
D 2.25
U 8.25
U 2.5
U 2.0
U 0.25
D 4.25
U 2.5
U 4.0
U 3.0
D 6.25
U 9.75
U 4.0
U 3.75
D 9.0
U 8.75
U 2.75
D 8.5
D 6.25
D 1.0
U 4.75
D 5.5
D 1.25
D 9.5
D 6.0
D 5.5
U 6.5
D 7.0
U 7.75
D 1.25
U 5.5
D 2.75
D 0.5
U 5.0
U 4.25
D 5.25
U 7.0
U 4.5
D 5.75
D 0.0
U 0.0
D 4.5
 
Output for question 2 (partyVolume):
 

##### partyVolume #####


>>> partyVolume('party1.txt')

Volume(6.35)

>>> partyVolume('party2.txt')

Volume(3.75)

>>> partyVolume('party3.txt')

Volume(0.75)


# make sure return not print


>>> partyVolume('party1.txt')==Volume(6.35) # return not print

True

>>> partyVolume('party2.txt')==Volume(3.75)

True

>>> partyVolume('party3.txt')==Volume(0.75)

True

# can compare Volumes using ==
>>> # comparisons
>>> v = Volume (5)
>>> v.up(1.1)
>>> v == Volume (6.1)
True
>>> Volume(3.1) == Volume(3.2)
False
# constructor cannot set the Volume greater
# than 11 or less than e
>>> v = Volume (20)
>>> v
Volume (11)
>>> v - Volume(-1)
>>> v
Volume (e)
>>>
# more thorough checks of clamping
>>> v = Volume()
>>> [ (val*.5,v.set (val*.5), str(v)) for val in range(-3,30,4)] # set
[(-1.5, None, 'Volume (0)'), (0.5, None, 'Volume (0.5)'), (2.5, None, 'Volume (2.5)'), (4.5, None, 'Volume (4.5)'), (6.5, None, 'Volume (6.5)'), (8.5, None, 'Volume (8.5)'),
(10.5, None, 'Volume (10.5)'), (12.5, None, 'Volume (11)'), (14.5, None, 'Volume (11)')]
>>> [ (val*.5, Volume (val*.5)) for val in range(-3,30,4)] # constructor
[(-1.5, Volume (0)), (0.5, Volume (0.5)), (2.5, Volume (2.5)), (4.5, Volume (4.5)), (6.5, Volume(6.5)), (8.5, Volume (8.5)), (10.5, Volume (10.5)), (12.5, Volume (11)),
(14.5, Volume(11))]
>>> v = Volume (5)
>>> [ (v.up(.75), str(v)) for i in range(10) ] # up
[(None, 'Volume (5.75)'), (None, 'Volume (6.5)'), (None, 'Volume (7.25)'), (None, 'Volume (8.0)'), (None, 'Volume (8.75)'), (None, 'Volume (9.5)'), (None, 'Volume (10.25)'),
(None, 'Volume (11.0)'), (None, 'Volume(11)'), (None, 'Volume (11)')]
>>> v = Volume (5)
>>> [ (v.down(.75), str(v)) for i in range (10) ] # down
[(None, 'Volume (4.25)'), (None, 'Volume (3.5)'), (None, 'Volume (2.75)'), (None, 'Volume(2.0)'), (None, 'Volume (1.25)'), (None, 'Volume (e.5)'), (None, 'Volume (0)'),
(None, 'Volume (0)'), (None, 'Volume(0)'), (None, 'Volume (0)')]
>>>
Transcribed Image Text:# can compare Volumes using == >>> # comparisons >>> v = Volume (5) >>> v.up(1.1) >>> v == Volume (6.1) True >>> Volume(3.1) == Volume(3.2) False # constructor cannot set the Volume greater # than 11 or less than e >>> v = Volume (20) >>> v Volume (11) >>> v - Volume(-1) >>> v Volume (e) >>> # more thorough checks of clamping >>> v = Volume() >>> [ (val*.5,v.set (val*.5), str(v)) for val in range(-3,30,4)] # set [(-1.5, None, 'Volume (0)'), (0.5, None, 'Volume (0.5)'), (2.5, None, 'Volume (2.5)'), (4.5, None, 'Volume (4.5)'), (6.5, None, 'Volume (6.5)'), (8.5, None, 'Volume (8.5)'), (10.5, None, 'Volume (10.5)'), (12.5, None, 'Volume (11)'), (14.5, None, 'Volume (11)')] >>> [ (val*.5, Volume (val*.5)) for val in range(-3,30,4)] # constructor [(-1.5, Volume (0)), (0.5, Volume (0.5)), (2.5, Volume (2.5)), (4.5, Volume (4.5)), (6.5, Volume(6.5)), (8.5, Volume (8.5)), (10.5, Volume (10.5)), (12.5, Volume (11)), (14.5, Volume(11))] >>> v = Volume (5) >>> [ (v.up(.75), str(v)) for i in range(10) ] # up [(None, 'Volume (5.75)'), (None, 'Volume (6.5)'), (None, 'Volume (7.25)'), (None, 'Volume (8.0)'), (None, 'Volume (8.75)'), (None, 'Volume (9.5)'), (None, 'Volume (10.25)'), (None, 'Volume (11.0)'), (None, 'Volume(11)'), (None, 'Volume (11)')] >>> v = Volume (5) >>> [ (v.down(.75), str(v)) for i in range (10) ] # down [(None, 'Volume (4.25)'), (None, 'Volume (3.5)'), (None, 'Volume (2.75)'), (None, 'Volume(2.0)'), (None, 'Volume (1.25)'), (None, 'Volume (e.5)'), (None, 'Volume (0)'), (None, 'Volume (0)'), (None, 'Volume(0)'), (None, 'Volume (0)')] >>>
# set and get
>>> v = Volume ()
>>> v.set(5.3)
>>> v
Volume (5.3)
>>> v.get ()
5.3
>>> v.get()--5.3 # return not print
True
>>> v.set(13.2)
>>> v
Volume (11)
>>> v.set(-1.2)
>>> v
Volume (e)
# _init_,_repr_, up, down
>>> v = Volume (4.5) # set Volume with value
>>> v
Volume (4.5)
>>> v.up(1.4)
>>> v
Volume (5.9)
>>> v.up(6) # should max out at 11
>>> v
Volume (11)
>>> v.down (3.5)
>>> v
Volume (7.5)
>>> v.down (10) # minimum must be e
>>> v
Volume (0)
# default arguments for init_
>>> v = Volume() # Volume defaults to e
>>> v
Volume (0)
Transcribed Image Text:# set and get >>> v = Volume () >>> v.set(5.3) >>> v Volume (5.3) >>> v.get () 5.3 >>> v.get()--5.3 # return not print True >>> v.set(13.2) >>> v Volume (11) >>> v.set(-1.2) >>> v Volume (e) # _init_,_repr_, up, down >>> v = Volume (4.5) # set Volume with value >>> v Volume (4.5) >>> v.up(1.4) >>> v Volume (5.9) >>> v.up(6) # should max out at 11 >>> v Volume (11) >>> v.down (3.5) >>> v Volume (7.5) >>> v.down (10) # minimum must be e >>> v Volume (0) # default arguments for init_ >>> v = Volume() # Volume defaults to e >>> v Volume (0)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education