Challenge 1: Implement Rectangle Class Using the Encapsulation

In this challenge, you will implement a rectangle class using the concepts of encapsulation.

Problem statement

You are given a partially completed code of a Rectangle class in the editor. Implement the class by completing the tasks below.

Task 1

Implement a constructor to initialize the values of two private properties: length and width.

Task 2

Implement a method, area(), in the Rectangle class that returns the product of length and width. See the formula below:

Area=length×widthArea = length \times width

Sample properties

length = 4
width = 5

Sample method output

20

Task 3

Implement a method, perimeter(), in the Rectangle class that returns two times the sum of length and width. See the formula below:

Perimeter=2×(length+width)Perimeter = 2\times (length + width)

Sample properties

length = 4
width = 5

Sample method output

18

Get hands-on with 1200+ tech skills courses.