Python An Introduction to Programming by Parker James R

Python An Introduction to Programming by Parker James R

Author:Parker, James R.
Language: eng
Format: epub
Publisher: Mercury Learning and Information
Published: 2016-11-14T00:00:00+00:00


Exercises

1. Define a class named square in which the construct takes the length of the side as a parameter. This class should have a method area() that computes and returns the area of the square.

2. Define a subclass of square named button that also has a location, passed as X and Y parameters to the constructor. A button always has a width of 10. The button class has the following methods:

center() Return the coordinates of the center of the button

label(s) Set the value of a text label to be drawn to s

3. Create a class client. A client is a data-only class that has no methods other than __init__(), but that holds data. In this case the client class holds a name, a category (retail or commercial), a time value (integer), and a service value (integer). All values are established when the instance is created by passing parameters to __init__(). Now create two subclasses of client, one for each category, retail and commercial.

4. Define a class named fraction that implements fractional numbers. The constructor takes the numerator and denominator as parameters, and the class provides methods to add, multiply, negate (make negative), print, and find the reciprocal of a fraction. Test this class by calculating:

14/16 * 3/4

1/2 – 1/4

Bonus: results are reduced to smallest possible denominator.

5. Given the following class:

class value:

def __init__ (self)

self.val = randrange(0,100)

and the initialization:

t = ()

for i in range(0,100):

v = value()

t = t + (v,)

write the code that scans the tuple t and locates the smallest integer saved in any of the class instances.

6. Create a class that simulates a NAND logic gate with three inputs. The output will be 1 unless all three inputs are 1, in which case the output is 0. Every time an input is changed, the output is changed to reflect the new state; thus, methods to set each input and to calculate the result will be needed, in addition to a method that returns the output.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.