Home > OS >  How to use association in two classes in python programming
How to use association in two classes in python programming

Time:01-21

I have two classes: Rectangle() and Figures (). I need to connect these two classes with Association. But i have no idea how to call methods: get_perimeter() and get_area() from Rectangle() in Figures.

i need to use method get_perimeters() in class Figures with using Association. It means i must call a method get_perimeter() from class Rectangle(). First of all my class Figures() create an empty list, then it adds group of rectangles with using method add_rectangle() (for example it'll be three rectangles (width=15, height=13; width=12, height=25 etc.)) And then when i call a method get_perimeters(), program need to show me perimeters of three rectangles (with using get_perimeter() from Rectangle class)

Here my code. If you have some ideas or offers about improving my code i'll be thankful

Here my 2 classes:

class Rectangle:
    
    def __init__(self, width=None, height=None):
        
        self.width = width
        self.height = height
    
    def __str__(self):
        
        return "Rectangle with width {width} cm and height {height} cm".format(width=self.width, height=self.height)
       
    def get_area(self):
        
        return self.width * self.height
        
    def get_perimeter(self):
      
        return (self.width   self.height) * 2
       
    @staticmethod
    def get_info():
        
         return "This class calculates perimeter and area of the rectangles"
        

class Figures:
    
    def __init__(self, num_figures=None):
        self.figures = []
        self.num_figures = 0
        
    def __str__(self):
        
        return "Container containing figures"
    
    def __len__(self):
        
        return len(self.figures)

    def add_rectangle(self, width=None, height=None):
     
        if width and height != None:
            self.figures.append(Rectangle(width=width, height=height))
        else:
            print("You didn't provide width or height properly")
        
    def get_perimeters(self, width=None, height=None):
        pass   

    def get_areas(self, width=None, height=None):
       
        pass
    
    @staticmethod
    def get_info():
        
        return "This class creates the container containing the instances of class Rectangle"



   

CodePudding user response:

Your code does store instances of Rectangle in figures using the method add_rectangle but your methods get_perimeters, get_areas, and get_info in class Figures aren't doing any of those things. If we want to create three rectangles and then use the get_perimeter method, for example, we could enumerate through the instances stored as a list in your figures variable, call the get_perimeter instance method of class Rectangle, and print all three rectangles' perimeters in the order in which they were instantiated:

def get_perimeters(self):
    for fig_num, fig in enumerate(self.figures):
        print("perimeter for figure number "   str(fig_num)   ": "   str(fig.get_perimeter()))

Also, num_figures doesn't seem to be doing anything, and I'm guessing that you intend for it to keep track of the number of rectangles created. You could just put self.num_figures = 1 in method add_rectangle:

def add_rectangle(self, width=None, height=None):
    if width and height != None:
        self.num_figures  = 1
        self.figures.append(Rectangle(width=width, height=height))

Whole thing:

class Rectangle:
    def __init__(self, width=None, height=None):
        self.width = width
        self.height = height

    def __str__(self):
        return "Rectangle with width {width} cm and height {height} cm".format(width=self.width, height=self.height)

    def get_area(self):
        return self.width * self.height

    def get_perimeter(self):
        return (self.width   self.height) * 2


class Figures:
    def __init__(self):
        self.figures = []
        self.num_figures = 0

    def __str__(self):
        return "Container containing figures"

    def __len__(self):
        return len(self.figures)

    def add_rectangle(self, width=None, height=None):
        if width and height != None:
            self.num_figures  = 1
            self.figures.append(Rectangle(width=width, height=height))
        else:
            print("You didn't provide width or height properly")

    def get_perimeters(self):
        for fig_num, fig in enumerate(self.figures):
            print("perimeter for figure number "   str(fig_num)   ": "   str(fig.get_perimeter()))

    def get_areas(self):
        for fig_num, fig in enumerate(self.figures):
            print("area for figure number "   str(fig_num)   ": "   str(fig.get_area()))

    def get_info(self):
        """ This class creates the container containing the instances of class Rectangle
        """
        return self.figures

Let's create three rectangles:

m = Figures()
m.add_rectangle(width=15, height=13) # first rectangle;
m.add_rectangle(width=12, height=25) # second rectangle;
m.add_rectangle(width=10, height=20) # third rectangle;

Calling methods get_areas and get_perimeters from Figures, which uses methods get_area and get_perimeter from Rectangle, and seeing how many num_figures there are:

m.get_areas()
m.get_perimeters()
print('number of figures is '   str(m.num_figures))

outputs:

area for figure number 0: 195
area for figure number 1: 300
area for figure number 2: 200
perimeter for figure number 0: 56
perimeter for figure number 1: 74
perimeter for figure number 2: 60
number of figures is 3

We can use the method get_info to return instances of Rectangle, and then call methods (e.g. get_area) on one of its instances (e.g. first instance indexed at 0):

b = m.get_info() # b = all instances of class Rectangle;
b[0].get_area() # b[0] being the first instance of class Rectangle;

outputs:

195
  • Related