Write an inheritance hierarchy for class Quadrilateral, Trapezoid, Parallelo- gram, Rectangle and Square. Use Quadrilateral as the base class of the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. The data of Quadrilateral should be the (x, y) coordinate pairs for the four endpoints of the Quadrilateral. Write a driver program that creates and displays objects of each
of these classes.
What will be an ideal response?
```
# Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square
# classes.
import math
def distance( pointA, pointB ):
"""Returns the distance between two points"""
# uses math.hypot to compute distance
x = pointA[ 0 ] - pointB[ 0 ]
y = pointA[ 1 ] - pointB[ 1 ]
return math.hypot( x, y )
def slope( pointA, pointB ):
"""Returns the slope of a line"""
# slope = change in y-coordinates / change in x-coordinates
y = pointA[ 1 ] - pointB[ 1 ]
x = pointA[ 0 ] - pointB[ 0 ]
if x != 0:
return y / x
else: # a horizontal line has no slope
return None
class Quadrilateral:
"""Represents a quadrilateral"""
def __init__( self, x1, y1, x2, y2, x3, y3, x4, y4 ):
"""Class Quadrilateral constructor"""
# points are represented as tuples
self.point1 = ( x1, y1 )
self.point2 = ( x2, y2 )
self.point3 = ( x3, y3 )
self.point4 = ( x4, y4 )
def getPoints( self ):
"""Returns a list of points (tuples)"""
return [ self.point1, self.point2, self.point3,
self.point4 ]
def area( self ):
"""Abstract method: override in derived classes"""
raise NotImplementedError, \
"Derived class must define abstract method"
def perimeter( self ):
"""Returns the perimeter of the quadrilateral"""
points = self.getPoints()
perimeter = 0
# sums distances between adjacent points
for index in range( 4 ):
perimeter += distance( points[ index ],
points[ ( index + 1 ) % 4 ] )
return perimeter
class Trapezoid( Quadrilateral ):
"""Represents a trapezoid derived from class Quadrilateral"""
# do not need to define Trapezoid constructor--implicitly
# called Quadrilateral constructor is sufficient
def getBases( self ):
"""Returns the pairs of points that define the bases"""
points = self.getPoints()
bases = [] # holds base points
# a side is a base if its slope = 0 or None
for index in range( 4 ):
# append a tuple: ( basePoint1, basePoint2 )
m = slope( points[ index ], points[ ( index + 1 ) % 4 ] )
if m == 0 or m == None:
bases.append( ( points[ index ],
points[ ( index + 1 ) % 4 ] ) )
return bases
def getHeight( self ):
"""Returns the height of the trapezoid"""
basePoints = self.getBases()
# unpack base points
b1, b2, b3, b4 = ( basePoints[ 0 ][ 0 ],
basePoints[ 0 ][ 1 ], basePoints[ 1 ][ 0 ],
basePoints[ 1 ][ 1 ] )
if b1[ 1 ] == b3[ 1 ]:
return math.fabs( b1[ 0 ] - b3[ 0 ] )
else:
return math.fabs( b1[ 1 ] - b3[ 1 ] )
def getBaseLengths( self ):
"""Returns the lengths of the bases as a tuple"""
basePoints = self.getBases()
return ( distance( basePoints[ 0 ][ 0 ],
basePoints[ 0 ][ 1 ] ),
distance( basePoints[ 1 ][ 0 ],
basePoints[ 1 ][ 1 ] ) )
def area( self ):
"""Returns area of trapezoid"""
b1, b2 = self.getBaseLengths()
return .5 * ( b1 + b2 ) * self.getHeight()
class Parallelogram( Trapezoid ):
"""Represents a parallelogram"""
# do not need to define Trapezoid constructor--implicitly
# called Quadrilateral constructor is sufficient
def area( self ):
"""Returns the area of a parallelogram"""
# area = base * height
return self.getBaseLength() * self.getHeight()
def perimeter( self ):
"""Returns the perimeter of a parallelogram"""
# perimeter = 2 * ( height + base )
return 2 * ( distance( self.point1, self.point2 ) +
distance( self.point2, self.point3 ) )
def getBaseLength( self ):
"""Returns the base length of a parallelogram"""
# call Trapezoid method to get lengths
baseLengths = self.getBaseLengths()
# bases are equal length, so return length of one base
return baseLengths[ 0 ]
class Rectangle( Parallelogram ):
"""Represents a rectangle"""
def __init__( self, x1, y1, x2, y2, x3, y3, x4, y4 ):
"""Class Rectangle constructor"""
# rectangle is a special case of a parallelogram
# where all lines have slope = 0 or None
Parallelogram.__init__( self, x1, y1, x2, y2, x3, y3,
x4, y4 )
self.setWidth()
self.setLength()
def setWidth( self ):
"""Sets the width of the rectangle as the largest side"""
sides = self.getBaseLengths()
maximum = sides[ 0 ]
for side in sides:
if side > maximum:
maximum = side
self.width = maximum
def getWidth( self ):
"""Returns Rectangle width"""
return self.width
def setLength( self ):
"""Sets the length of the rectangle"""
sides = self.getBaseLengths()
minimum = sides[ 0 ]
for side in sides:
if side < minimum:
minimum = side
self.length = minimum
def getLength( self ):
"""Returns Rectangle length"""
return self.length
def area( self ):
"""Returns the area of a rectangle"""
# area = length * width
return self.length * self.width
class Square( Rectangle ):
"""Represents a square"""
def __init__( self, x1, y1, x2, y2, x3, y3, x4, y4 ):
"""Class Square constructor"""
# square is a rectangle with four equal sides
Rectangle.__init__( self, x1, y1, x2, y2, x3, y3, x4, y4 )
self.setSide()
def setSide( self ):
"""Sets side length of a square"""
sides = self.getBaseLengths()
self.side = sides[ 0 ]
def getSide( self ):
"""Returns Square's side"""
return self.side
def area( self ):
"""Returns area of a square"""
return self.side ** 2
def perimeter( self ):
"""Returns perimeter of a square"""
return self.side * 4
# Exercise 9.6: ex09_06.py
# Driver for classes from Shapes.py.
import Shapes
# create a quadrilateral
quad = Shapes.Quadrilateral( 0, 0, 0, 7, 5, 10, 7, 0 )
print """The quadrilateral is represented by the following points:
%s
This is its perimeter: %s """ % ( quad.getPoints(),
quad.perimeter() )
# create a trapezoid
trapezoid = Shapes.Trapezoid( 0, 0, 1, 7, 5, 7, 10, 0 )
print """The trapezoid is represented by the following points:
%s
These are the lengths of its bases: %s
This is its height: %s
This is its perimeter: %s
This is its area: %s """ % ( trapezoid.getPoints(),
trapezoid.getBaseLengths(), trapezoid.getHeight(),
trapezoid.perimeter(), trapezoid.area() )
# create a parallelogram
parallelogram = Shapes.Parallelogram( 2, 3, 2, 7, 4, 9, 4, 5 )
print """The parallelogram is represented by the following points:
%s
This is the length of its base: %s
This is its height: %s
This is its perimeter: %s
This is its area: %s """ % ( parallelogram.getPoints(),
parallelogram.getBaseLength(), parallelogram.getHeight(),
parallelogram.perimeter(), parallelogram.area() )
# create a rectangle
rectangle = Shapes.Rectangle( 15, 13, 8, 13, 8, 9, 15, 9 )
print """The rectangle is represented by the following points:
%s
This is its length: %s
This is its width: %s
This is its perimeter: %s
This is its area: %s """ % ( rectangle.getPoints(),
rectangle.getLength(), rectangle.getWidth(),
rectangle.perimeter(), rectangle.area() )
# create a square
square = Shapes.Square( 8, 8, 8, 3, 3, 3, 3, 8 )
print """The square is represented by the following points:
%s
This is the length of one of its sides: %s
This is its perimeter: %s
This is its area: %s """ % ( square.getPoints(),
square.getSide(), square.perimeter(), square.area() )
```
The quadrilateral is represented by the following points:
[(0, 0), (0, 7), (5, 10), (7, 0)]
This is its perimeter: 30.028990922
The trapezoid is represented by the following points:
[(0, 0), (1, 7), (5, 7), (10, 0)]
These are the lengths of its bases: (4.0, 10.0)
This is its height: 7.0
This is its perimeter: 29.6733930789
This is its area: 49.0
The parallelogram is represented by the following points:
[(2, 3), (2, 7), (4, 9), (4, 5)]
This is the length of its base: 4.0
This is its height: 6.0
This is its perimeter: 13.6568542495
This is its area: 24.0
The rectangle is represented by the following points:
[(15, 13), (8, 13), (8, 9), (15, 9)]
This is its length: 4.0
This is its width: 7.0
This is its perimeter: 22.0
This is its area: 28.0
The square is represented by the following points:
[(8, 8), (8, 3), (3, 3), (3, 8)]
This is the length of one of its sides: 5.0
This is its perimeter: 20.0
This is its area: 25.0
You might also like to view...
Being computer literate includes being able to
a. avoid spam, adware, and spyware. b. use the web effectively. c. diagnose and fix hardware and software problems. d. all of the above.
Choose the appropriate slide ________, to display text and graphics on a slide in the best manner
Fill in the blank(s) with correct word