• Post Reply Bookmark Topic Watch Topic
  • New Topic

re: Java Nested For Loops to print concentrically nested squares one inside another  RSS feed

 
Greenhorn
Posts: 5
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I would like you all to try and suggest a solution for the following :

I need to create concentric nested squares(one inside another), until no more squares can be created;
Its just like drawing one square inside another ON PAPER,until no more squares are possible to be drawn,
by decrementing the length of the side by 4 after each square is drawn;
And N is the size of the square to start with(i.e; the size of the side of the outermost square).Only For-loops need to be used(along with if checks OfCourse..!!)
You have to draw the sides by using multiplication symbol('*'): The attached images will suggest exactly the pattern needed to be printed.
This code might help you partially,
image.jpg
[Thumbnail for image.jpg]
using * to print sides
image2.jpg
[Thumbnail for image2.jpg]
pattern to be printed
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might consider creating a Square class or similar. You can pass the x, y coordinates, and print a * if you are on the margin of a Square. Now all you have to do is work out how to nest Squares inside one another. You could have a Square which has a Square object as a field, but only if the “outer” Square has a certain size (or larger).
Now you loop through the possible x, y coordinates and look to see whether you have to print the *.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ketan Dikshit wrote:This code might help you partially,

Well, I have to admit, if it was me, and this was a GUI, I suspect I'd go about it recursively, and if it was for a console, I'd deal with the problem in 'halves' (top half, bottom half; the latter being a reflection of the first).

But most of all, I'd provide at least a method signature, eg:(and I'm sure there are better).

Winston
 
Ketan Dikshit
Greenhorn
Posts: 5
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think you might consider creating a Square class or similar. You can pass the x, y coordinates, and print a * if you are on the margin of a Square. Now all you have to do is work out how to nest Squares inside one another. You could have a Square which has a Square object as a field, but only if the “outer” Square has a certain size (or larger).
Now you loop through the possible x, y coordinates and look to see whether you have to print the *.


Okay, what you are suggesting is to use a typical rendering algorithm(something like growNshrink kind..) to check the validity for the pixel(if * needs to be printed),while traversing, one line at a time...That sounds good. I'll try that..!! Thanks
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome and good luck with it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!