Classes

Seymore4Head Seymore4Head at Hotmail.invalid
Fri Oct 31 19:42:32 CET 2014


On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
<Seymore4Head at Hotmail.invalid> wrote:

>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
><rgaddi at technologyhighland.invalid> wrote:
>
>
>>Define a Square class, subclassed from Rectangle.  Use getters/setters
>>to enforce that the length and width must be equal.  Confirm that
>>length and width remain locked, and that perimeter() and area() work
>>correctly.
>
>class Rectangle:
>    def __init__(self,length,width):
>        self.length=length
>        self.width=width
>    def area(self):
>        return self.length*self.width
>    def perimeter(self):
>        return 2*self.length+2*self.width
>class Square(Rectangle):
>    def set_side (self):
>        if self.length!=self.width:
>            
>    
>a=Rectangle(3,5)
>print (a.area())
>print (a.perimeter())
>b=Rectangle(5,7)
>print (b.area())
>print (b.perimeter())
>c=Rectangle(4,4)
>print (c.area())
>print (c.perimeter())
>
>I bombed on the rest.
class Rectangle:
    def __init__(self,length=0,width=0):
        self.length=length
        self.width=width
    def area(self):
        return self.length*self.width
    def perimeter(self):
        return 2*self.length+2*self.width
class Square(Rectangle):
    def set_side (self,side):
        self.side=side
        return side*side
            
    
a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Rectangle(4,4)
print (c.area())
print (c.perimeter())

A little closer.



More information about the Python-list mailing list