Challenge 1: Implement the Rectangle Class Using the Concepts of Encapsulation
Can you implement the rectangle class using the concepts of encapsulation? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.
We'll cover the following
Problem Statement
You are given a partially completed code of a Rectangle
class in the editor. Add two fields i.e. length
and width
and modify the default and parametrized constructors which assign parameters to the length
and width
fields of the Rectangle
class.
In the end, Implement the getArea()
method which 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
4
Get hands-on with 1200+ tech skills courses.