Picture It Recursion:

 

The code to print one square inside another is below, but it only prints the one square inside the other. If you want the printInsideSquare method to print a big square with smaller and smaller squares until the size is 5, how would you code it? Consider the following questions:

 

What size parameter will make the program want to stop getting smaller?

When the method receives a size of 100, what does the method have available to it? Remember it has all parameter values and also the result of any other call it wants to make to printInsideSquare? How many squares do you want the method to add to the picture each time it is called?

 

Which is the base case?

What will it return?

 

Change the code below to call make the printInsideSquare method work to print squares inside squares until it reaches a square of size 5:

 

import javalib.worldimages.*;

import java.awt.Color;

public class Test7

{

    public static void main()

    {

        WorldImage myWorld = printInsideSquares (  600);

        myWorld.show();

    }

    public static WorldImage printInsideSquares(  int size)

    {

       __________________________________________

    __________________________________________

     __________________________________________

            WorldImage mainSquare = AImage.makeRectangle(size,size,Mode.OUTLINED);

            WorldImage littleSquare = AImage.makeRectangle(size/10*9,size/10*9,

                    Color.RED, Mode.OUTLINED);

            mainSquare = mainSquare.place( littleSquare, size/2, size/2); 

     __________________________________________

     __________________________________________

     __________________________________________

     __________________________________________

     __________________________________________

            return mainSquare;

        }

    }