# Recursive squares exercise java

• 11-24-2012, 03:26 PM
JavaGirlie
Recursive squares exercise java
Hi guys!
I am currently taking my first programming course and we have been given an exercise to produce recursive squares.

Attachment 4251

Here is the code for the first part.
Code:

```public class RecursiveSquares {     // plot a square, centered on (x, y) of the given side length     // with a light gray background and black border     public static void drawSquare(double x, double y, double size) {         StdDraw.setPenColor(StdDraw.LIGHT_GRAY);         StdDraw.filledSquare(x, y, size/2);         StdDraw.setPenColor(StdDraw.BLACK);         StdDraw.square(x, y, size/2);     }     // plot an order n recursive squares pattern     // centered on (x, y) of the given side length     public static void draw(int n, double x, double y, double size) {         if (n == 0) return;         drawSquare(x, y, size);         // 2.2 ratio looks good         double ratio = 2.2;         // recursively draw 4 smaller trees of order n-1         draw(n-1, x - size/2, y - size/2, size/ratio);    // lower left          draw(n-1, x - size/2, y + size/2, size/ratio);    // upper left          draw(n-1, x + size/2, y - size/2, size/ratio);    // lower right         draw(n-1, x + size/2, y + size/2, size/ratio);    // upper right     }     // read in a command-line argument N and plot an order N recursive     // squares pattern     public static void main(String[] args) {         int N = Integer.parseInt(args[0]);         double x = 0.5, y = 0.5;  // center of square         double size = 0.5;        // side length of square         draw(N, x, y, size);     } }```
Could you guys think of an algorithm on how I can do the rest of them?

Thank you!!
• 11-24-2012, 03:33 PM
JosAH
Re: Recursive squares exercise java
For variation a) you first draw the big square and then you draw the four smaller squares on top of it; for variation d) you first draw the four small squares and the big square on top of them; variations b) and c) are similar variations with a different order of drawing ...

kind regards,

Jos
• 11-24-2012, 10:39 PM
JavaGirlie
Re: Recursive squares exercise java