Create a more sophisticated Rectangle class than the one you created in Exercise 7.6. This class stores only the x-y coordinates of the upper left-hand and lower right-hand corners of the rect- angle. The constructor calls a set function that accepts two tuples of coordinates and verifies that each of these is in the first quadrant, with no single x or y coordinate larger than 20.0. Methods

calculate the length, width, perimeter and area. The length is the larger of the two dimensions. In- clude a predicate method isSquare that determines whether the rectangle is a square. Write a driver program to test the class.

What will be an ideal response?


```
# Class Rectangle, defined by 2 points.

import math

class Rectangle:
"""Represents rectangle with upper-left and lower-right points"""

def __init__( self, upperLeft = ( 1.0, 1.0 ),
lowerRight = ( 1.0, 1.0 ) ):
"""Initializes length and width"""

# use access methods to initialize attributes
self.setUpperLeft( upperLeft )
self.setLowerRight( lowerRight )

def setUpperLeft( self, point ):
"""Sets position of upper-left point in square"""

# use utility method
self.__upperLeft = self.__setPoint( point )

def setLowerRight( self, point ):
"""Sets position of lower-right point in square"""

# use utility method
self.__lowerRight = self.__setPoint( point )

def __setPoint( self, point ):
"""Sets point with coordinates in range 0.0 and 20.0"""

x = float( point[ 0 ] )
y = float( point[ 1 ] )

if ( 0.0 < x < 20.0 ) and ( 0.0 < y < 20.0 ):
return ( x, y )
else:
raise ValueError, \
"Each coordinate must be in ( (0.0,0.0), (20.0, 20.0) )"

def width( self ):
"""Calculates and returns width"""

return min( self.__sides() ) # width is smaller value

def length( self ):
"""Calculates and returns length"""

return max( self.__sides() ) # length is larger value

def __sides( self ):
"""Utility method to calculate the length of the sides"""

return ( math.fabs( self.__upperLeft[ 0 ] -
self.__lowerRight[ 0 ] ),
math.fabs( self.__upperLeft[ 1 ] -
self.__lowerRight[ 1 ] ) )

def perimeter( self ):
"""Calculates and returns perimeter"""

sides = self.__sides()

return 2 * ( sides[ 0 ] + sides[ 1 ] )

def area( self ):
"""Calculates and returns area"""

sides = self.__sides()

return sides[ 0 ] * sides[ 1 ]

def isSquare( self ):
"""Returns 1 if Rectangle is a square, 0 otherwise"""

# a rectangle is a square if length is the same as width
sides = self.__sides()

if sides[ 0 ] == sides[ 1 ]:
return 1
else:
return 0
# Exercise 7.7: ex07_07.py
# Driver for class NewRectangle.

from Rectangle2 import Rectangle

# create two Rectangle objects
aRectangle = Rectangle( ( 8.0, 4.0 ), ( 2.0, 8.0 ) )
aSquare = Rectangle( ( 3.0, 5.0 ), ( 5.0, 3.0 ) )

# print length
print "Length of aRectangle =", aRectangle.length()
print "Length of aSquare =", aSquare.length()

# print width
print "\nWidth of aRectangle =", aRectangle.width()
print "Width of aSquare =", aSquare.width()

# print perimeter
print "\nPerimeter of aRectangle =", aRectangle.perimeter()
print "Perimeter of aSquare =", aSquare.perimeter()

# print area
print "\nArea of aRectangle =", aRectangle.area()
print "Area of aSquare =", aSquare.area()

# prints a message if the Rectangle is a square
if aRectangle.isSquare():
print "\naRectangle is a square."

if aSquare.isSquare():
print "\naSquare is a square."
```
Length of aRectangle = 6.0
Length of aSquare = 2.0
Width of aRectangle = 4.0
Width of aSquare = 2.0
Perimeter of aRectangle = 20.0
Perimeter of aSquare = 8.0
Area of aRectangle = 24.0
Area of aSquare = 4.0
aSquare is a square.
Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from Rectangle2 import Rectangle
>>> aRectangle = Rectangle()
>>> aRectangle.setUpperLeft( ( 0, 0 ) )
Traceback (most recent call last):
File "", line 1, in ?
File "Rectangle2.py", line 21, in setUpperLeft
self.__upperLeft = self.__setPoint( point )
File "Rectangle2.py", line 38, in __setPoint
raise ValueError, \
ValueError: Each coordinate must be in ( (0.0,0.0), (20.0, 20.0) )

Computer Science & Information Technology

You might also like to view...

____ are used to differentiate capability levels.

A. Utility attributes B. Control attributes C. Audit attributes D. Management attributes

Computer Science & Information Technology

What is a FLWOR expression?

What will be an ideal response?

Computer Science & Information Technology

The Total row is automatically added in both the Crosstab Query and the Nested Query

Indicate whether the statement is true or false

Computer Science & Information Technology

The ____ command moves all redundant messages in the selected folder to the Deleted Items folder.

A. Clean Up Conversation B. Clean Up Folder C. Ignore Conversation D. Create Rule

Computer Science & Information Technology