Results 1 to 4 of 4
Thread: Recursive squares exercise java
 11242012, 03:26 PM #1Member
 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 n1 draw(n1, x  size/2, y  size/2, size/ratio); // lower left draw(n1, x  size/2, y + size/2, size/ratio); // upper left draw(n1, x + size/2, y  size/2, size/ratio); // lower right draw(n1, x + size/2, y + size/2, size/ratio); // upper right } // read in a commandline 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); } }
Thank you!!Last edited by JosAH; 11242012 at 03:29 PM. Reason: added [code] ... [/code] tags
 11242012, 03:33 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,312
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 11242012, 10:39 PM #3Member
 Join Date
 Nov 2012
 Posts
 2
 Rep Power
 0
Re: Recursive squares exercise java
thank you for the answer
how can I draw them in different order?
 11252012, 06:26 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 10
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.
Similar Threads

Java exercise
By Mbka in forum New To JavaReplies: 9Last Post: 06282012, 08:58 PM 
Help with recursive squares
By luke in forum New To JavaReplies: 1Last Post: 04182011, 11:47 PM 
Exercise for java 3d
By armiri in forum Java 2DReplies: 2Last Post: 05132010, 11:14 PM 
Exercise for java 3d
By armiri in forum Java SoftwareReplies: 3Last Post: 05132010, 11:13 PM 
exercise of recursive method
By amexudo in forum New To JavaReplies: 2Last Post: 03092008, 06:55 PM
Bookmarks