Challenge 1: Implement the Rectangle Class

Can you implement a rectangle class using the concepts of encapsulation? A solution is placed in the "solution" section to help you, but we suggest you try to solve it on your own first.

Problem Statement

You are given a partially completed code of a Rectangle class in the editor. Declare two fields, length and width and implement a parameterized constructor which assigns parameters to the length and width fields of the Rectangle class. In the end, Implement the GetArea() method which calculates and returns the area of the rectangle.

Input

Calls the constructor by passing length and width
Calls the GetArea() method to return the area

Output

Returns the area of a rectangle

Sample Input

Rectangle obj = new Rectangle(2, 2);

Sample Output

GetArea(); //Returns 4

Get hands-on with 1200+ tech skills courses.