# Thread: Recursive squares exercise java

1. Member Join Date
Nov 2012
Posts
2
Rep Power
0

## 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. Here is the code for the first part.
Java 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);
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!!
Last edited by JosAH; 11-24-2012 at 02:29 PM. Reason: added [code] ... [/code] tags  Reply With Quote

2. ## 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  Reply With Quote

3. Member Join Date
Nov 2012
Posts
2
Rep Power
0

## Re: Recursive squares exercise java

how can I draw them in different order?  Reply With Quote

4. ## Re: Recursive squares exercise java

How would you draw them in a different order by hand? Likely re ordering them will come down to just reorganizing the lines of code.  Reply With Quote

java, recursive squares 