2

Recursion always has been something I have a hard time with. I have a test tomorrow and he said there will be some Recursion on the test so I want to be prepared.

The problem I am trying to do says this:

Given a class Rectangle with instance variables width and height, provide a recursive getArea() method. Construct a rectangle whose width is one less than the original and call its getArea method.

So I know that in recursion you end up calling the method inside itself with a simplified rendition. Like, I know somewhere in getArea(int n) I will have to call getArea(n - 1). I am just not sure what to do with width and height.

So I have this:

public int getArea()
{
   if (width == 1) {
      // Base case here. Not sure what to do for this.
      return 1;   // Maybe? I'm not sure.
   } else {
      Rectangle smallerRect = new Rectangle (width - 1);
      int smallerArea = smallerRect.getArea();
      return smallerArea + height + width;
   }
}

Can anyone help me better understand recursion or maybe how to go about thinking through a recursive function? Thanks.

2
  • So what should be the output? If you decrease the width by on and call the method again it will continue until width is 0? Commented May 5, 2015 at 0:02
  • Note that when you construct a new Rectangle the constructor will require two parameters to initialize the height and width instance variables, not just width. Commented May 5, 2015 at 0:06

3 Answers 3

3

You've got the recursion itself right, with a base case and a recursive case, and a correct reduction of the parameter in the recursive call (except that, as the commenters have noted, you also need to specify the height of the new rectangle). It's only the geometry that needs fixing: height doesn't change during the recursion; what is the area of the base case rectangle, which has got width 1 and height height? And if you are told the area of the rectangle with width width - 1 and height height, how much extra area do you get by adding a strip of width 1 and height height?

For later use: while mathematically correct, this is a terrible way to compute the area of a rectangle, so please don't do this outside of exam/homework situations :-)

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this perhaps? It's basically just multiplying width by height with recursion...

public int getArea() {
    return getArea(width);
}

private int getArea(int x) {
    return x == 0 ? 0 : height + getArea(x-1);
}

1 Comment

This is best answer but do not confuse the hell out of poster, he is new to programming.
0
public int getArea()
{
   if (width == 1) {
      // Base case
      return height;   // Area = width(1)*height
   } else {
      Rectangle smallerRect = new Rectangle (width - 1, height);
      int smallerArea = smallerRect.getArea();
      return smallerArea + height;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.