• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie stuck on Pyramid Program  RSS feed

 
Jb Anderson
Greenhorn
Posts: 13
Eclipse IDE Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. This is my first attempt at learning a programming language. I am studying the CS106A online course through iTunes and I am reading the Art and Science of Java by Eric Roberts. I am about 2 weeks into learning and have started "Assignment #2: Simple Java Programs" and am attempting to build a Pyramid made from bricks. I have read other posts on this site, and it seems that it is encouraged to try and figure things out on your own, and to ask the forum for help when you get stuck. Which is where I currently am. I have spent some time thinking about the program, and realize I need two loops-- 1 that keeps track of bricks in a row and 1 that keeps track of the number of rows as the Pyramid rises in height. The Pyramid is supposed to be centered on the bottom of the screen. Three CONSTANTS have been required: BRICK_WIDTH=30, BRICK_HEIGHT=12, BRICKS_IN_BASE=14.

I tried breaking the scope of things down by trying step-wise refinement. But, I got bogged down. When I tried a method where both loops were included, I got limited success. For some reason, my equation to center along the x axis seems to be doubling the number of bricks. But, the correct number of total rows of bricks was a success. Also, the Pyramid was centered on the bottom of the screen. Another frustrating failure was that the number of bricks would not decrease as the new layers were created. My thought process was instead of using a loop that counts up (i++) I would use a loop that counts down (j--) which I thought would reduce the number of brick in each row as the "y" coordinate was reduced by each count of the BRICK_HEIGHT.

I am hoping someone could nudge me in the right direction or tweak my thinking a bit to help me solve it on my own.

for now, this is the code that "works". Whenever I add the (j--) the results are NOT what I expect.

I appreciate your taking the time to read my inquiry and provide some assistance in helping my thought process and my programming ability!!


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java jba wrote:Hello. This is my first attempt at learning a programming language...

And, I notice, your first post on this site, so: Welcome to the Ranch.

For future reference, please DontWriteLongLines. It makes your thread very hard to read and is actually not good practise. I've broken yours up this time, but please read the link for details.

Thanks.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java jba wrote:I am hoping someone could nudge me in the right direction or tweak my thinking a bit to help me solve it on my own.

OK.

First: StopCoding (←click).

Second: It would appear that you're building your rows of bricks from the top (1 brick) down (to BRICKS_IN_BASE bricks).
The x,y coordinate for the brick will be (as I recall) the top-left corner of that brick and, since you're building a line, you only have to worry about where the leftmost brick goes in order to centre them all; all the rest will simply be BRICK_WIDTH pixels further right. So, assuming the first brick of your bottom line starts at x,0, where will the line above that start? And the one above that? And the one above that?... and so on.

If you run into problems, try it first with a fixed number of lines, then try another... and another...and see if you can work out a pattern.

Alternatively, try centering ONE line of n bricks within a specific width, and then build up from there.

Programming is all about thinking, not coding; and until you understand the problem, you will never be able to code it.

HIH

Winston
 
Jb Anderson
Greenhorn
Posts: 13
Eclipse IDE Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston,

Thank you for your speedy reply and your welcoming me to the site.

I will take your suggestions back to the woodshed (which is on the far corner of the "Ranch") and see if I can come back with some improvements.

Thank you for taking the time to help me and offering suggestions.

-J
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jb Anderson wrote:Thank you for your speedy reply and your welcoming me to the site.

You're welcome.

I have two other tips for you:

1. Start by writing a program to display your pyramid upside-down (ie, "base" line first). I think you may find it easier. [*]

2. Number your lines from 0, not 1. So, for example, if you have 10 lines, number them 0 through 9.

Java indices all start from 0, which beginners often find a bit odd to start with; but it makes many calculations based on an index much simpler, since they are then simply an offset from the start. If you number from 1, you'll generally find that you have to get rid of that 1 at some point.

Winston

[*] Indeed, since this is a GUI, it shouldn't really matter what order you display your lines of bricks. Just make sure you start low enough down to display them all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!