Answer:
The answer to this question can be given as:
Code:
class Square //define class Square
{
Private int sideLength; //define variable
Square(int length) //define parameterized constructor.
{
sideLength = length; //hold value of the parameter
}
int getArea() //define function getArea.
{
Private int area; //define variable.
area = sideLength * sideLength; //calculate area.
return area; //return value.
}
}
Explanation:
In this question it is not a good idea to introduce an instance variable for the area because It may be a different method that defines the same variable with the same name but different variables because they are related to different functions, so it is better to make this variable local in this case.