CodeTestValidate

No installations needed! Write Python directly in your browser and get instant feedback with our smart validation system.

Challenge # 7

Define a Rectangle Class with Area and Perimeter

Objective

Practice object-oriented programming by defining a class with attributes and methods.

Instructions

  • Define a class named Rectangle.

  • The class should have two attributes: width and height, passed during initialization.

  • Implement a method area() that returns the area of the rectangle.

  • Implement a method perimeter() that returns the perimeter of the rectangle.

  • Assume width and height are positive integers.

Constraints:
Do not use input(). • Assume all values are valid and positive integers.
Exemple IO

Input: rect = Rectangle(4, 5)print(rect.area())

Output: 20

Input: rect = Rectangle(4, 5)print(rect.perimeter())

Output: 18

Try It Online!

Output:

Validate Your Results

Get your code reviewed and validated to understand what you did right and where you can improve.

Previous challengeImplement Stack and Queue Using Lists
Loading...