Create a method display() that display the length, width, perimeter and area of an object created using an instantiation on rectangle class.
Question
Create a method display() that display the length, width, perimeter and area of an object created using an instantiation on rectangle class.
Solution
Sure, here is a Python method that fits your description:
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def display(self):
perimeter = 2 * (self.length + self.width)
area = self.length * self.width
print(f"Length: {self.length}")
print(f"Width: {self.width}")
print(f"Perimeter: {perimeter}")
print(f"Area: {area}")
# Create an object of Rectangle
rect = Rectangle(5, 10)
# Call the display method
rect.display()
In this code:
-
We define a class
Rectanglewith an__init__method that takeslengthandwidthas parameters. These parameters are stored as instance variables. -
We define a
displaymethod within theRectangleclass. This method calculates theperimeterandareaof the rectangle using the formulae2 * (length + width)andlength * widthrespectively. It then prints thelength,width,perimeter, andarea. -
We create an instance of the
Rectangleclass withlength5 andwidth10, and store it in the variablerect. -
We call the
displaymethod on therectobject, which prints thelength,width,perimeter, andareaof the rectangle.
Similar Questions
4. Create a class called Rectangle that represents a rectangle with width andheight attributes. Include methods to calculate the area and perimeter of therectangle. Create an instance of the class and demonstrate its functionality
Implement a Class for a RectangleCreate a class Rectangle with attributes for length and width.Implement methods to calculate the area and perimeter of the rectangle.Ensure that the length and width are always positive.Constraints:NAExample:Sample Input:5.06.0Sample Output:Area: 30Perimeter: 22 Explanation:output is the area and perimeter of rectangle through given inputs respectively.
Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
Write a VB Program that calculates the area of a rectangle give that the area = lengthmultiplied by width.
Select the correct answerWhat will be the output of the following Java code?class rectangle { int width; int length; int area; void area(int width, int length) { this.width = width; this.length = length; } } class Output { public static void main(String args[]) { area obj = new area(); obj.area(5 , 6); System.out.println(obj.length + " " + obj.width); } }Options6 55 630 00 30
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.