Create a class Rectangle. The class has attributes __length and __width, each of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. It has set and get methods for both __length and __width. The set methods should verify that __length and __width are each floating-point numbers larger than 0.0 and less than 20.0. Write a driver program to test the class.

What will be an ideal response?


```
# Class Rectangle.

class Rectangle:
"""Represents Rectangle with length and width attributes"""

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

self.setLength( length ) # uses set functions
self.setWidth( width )

def setLength( self, length ):
"""Sets length between 0.0 and 20.0"""

if 0.0 < length < 20.0:
self._length = float( length )
else:
raise ValueError, "Length must be in range (0.0-20.0)"

def setWidth( self, width ):
"""Sets width between 0.0 and 20.0"""

if 0.0 < width < 20.0:
self._width = float( width )
else:
raise ValueError, "Width must be in range (0.0-20.0)"

def getLength( self ):
"""Returns length"""

return self._length

def getWidth( self ):
"""Returns width"""

return self._width

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

return 2 * ( self._length + self._width )

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

return self._length * self._width
# Exercise 7.6: ex07_06.py
# Driver for class Rectangle.

from Rectangle1 import Rectangle

aRectangle = Rectangle( 16, 5 )

print "Length =", aRectangle.getLength()
print "Width =", aRectangle.getWidth()
print "Perimeter =", aRectangle.perimeter()
print "Area =", aRectangle.area()
```
Length = 16.0
Width = 5.0
Perimeter = 42.0
Area = 80.0

Computer Science & Information Technology

You might also like to view...

In settings under Multitasking, a Windows 10 user can select which windows are shown on the taskbar and appear when pressing the ________ keys

A) Shift + Del B) Alt + Tab C) Ctrl + Enter D) Alt + Shift

Computer Science & Information Technology

The chat room concept originated with Internet Relay Chat (IRC).

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

If the following Java statements are executed, what will be displayed? System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator");

a. The top three winners are Jody, the Giant Buffy, the Barbarian Adelle, the Alligator b. The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, the Alligator c. The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, and the Albino d. The top three winners are Jody, the Giant Buffy, the BarbarianAdelle, the Alligator

Computer Science & Information Technology

Explain the purpose of freezing panes in a worksheet.

What will be an ideal response?

Computer Science & Information Technology