3.2k views
3 votes
Write a Python class named Rectangle constructed by a length and width. methods it should have Area()- this should return the area as a value Perimeter()- this should return the total of all the sides as a value Print()- this should print the rectangle such that "Length X Width" Should ask the user to enter the width and length

User Campbelt
by
5.7k points

2 Answers

1 vote

Answer:

class Rectangle():

def __init__(self, l, w):

self.len = l

self.wi = w

def area(self):

return self.len*self.wi

def perimeter(self):

return self.len+self.len+self.wi+self.wi

def printing(self):

print('{} Lenght X Width {}'.format(self.len, self.wi))

length = int(input("enter the length "))

width = int(input("enter the width "))

newRectangle = Rectangle(length, width)

newRectangle.printing()

print("Area is: ")

print(newRectangle.area())

print("The perimeter is: ")

print(newRectangle.perimeter())

Step-by-step explanation:

The class rectangle is defined with a constructor that sets the length and width

As required the three methods Area, perimeter and printing are also defined

The input function is used to prompt and receive user input for length and width of the rectangle

An object of the Rectangle class is created and initalilized with the values entered by the user for length and width

The three methods are then called to calculate and return their values

User Alex Tau
by
5.9k points
4 votes

Answer:

class Rectangle:

def __init__(self, length=1, width=1):

self.length = length

self.width = width

def Area(self):

return self.length * self.width

def Perimeter(self):

return 2 * (self.length + self.width)

def Print(self):

print(str(self.length) + " X " + str(self.width))

l = float(input("Enter the length: "))

w = float(input("Enter the width: "))

a = Rectangle(l, w)

print(str(a.Area()) + " " + str(a.Perimeter()))

a.Print()

Step-by-step explanation:

Create a class called Rectangle

Create its constructor, that sets the length and width

Create a method Area to calculate its area

Create a method Perimeter to calculate its perimeter

Create a Print method that prints the rectangle in the required format

Ask the user for the length and width

Create a Rectangle object

Calculate and print its area and perimeter using the methods from the class

Print the rectangle

User Sequoya
by
6.3k points