# Draw a pyramid

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-20-2011, 12:42 AM
MisterNikos
Draw a pyramid
Hello, Java World!
This is my first post with my first problem in Java learning.
I want to solve the problem #1 (Draw a pyramid) of this webpage.
I have written this code, but it is wrong.
Code:

```/*  * File: Pyramid.java  * Name:  * Section Leader:  * ------------------  * This file is the starter file for the Pyramid problem.  * It includes definitions of the constants that match the  * sample run in the assignment, but you should make sure  * that changing these values causes the generated display  * to change accordingly.  */ import acm.graphics.*; import acm.program.*; import java.awt.*; public class Pyramid extends GraphicsProgram {         /* Width of each brick in pixels */         private static final int BRICK_WIDTH = 30;                 /* Width of each brick in pixels */         private static final int BRICK_HEIGHT = 12;                 /* Number of bricks in the base of the pyramid */         private static final int BRICKS_IN_BASE = 14;                         /* Runs the program */         public void run() {                 for (int i = 0; i < BRICKS_IN_BASE; i++) {                 for (int j = BRICKS_IN_BASE; i > 0; i--) {                         int x = i*BRICK_WIDTH;                         int y = j*BRICK_HEIGHT;                         GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);                         add (brick);                 }                 }         } }```
The result of this code is this:
Attachment 2042

I'd prefer you do not give me the answer to my problem by writing another code, but to tell me what is wrong with my code.
• 11-20-2011, 01:55 AM
Norm
Re: Draw a pyramid
Quote:

but it is wrong.
Please explain what is wrong with the code in more detail.

• 11-20-2011, 02:52 AM
pbrockway2
Re: Draw a pyramid
The for loops look sensible enough. But I'm one of those Norm referred to who find the acm classes a bit obscure. Just a thought though: maybe the bricks are white?
• 11-20-2011, 10:53 AM
MisterNikos
Re: Draw a pyramid
Thank you for the replies.
Norm, I don't know what is wrong with my code. I thought that the code was correct, but when I ran the program, I saw the one and only brick you see in the picture above.
Let me explain the code a little more further (just like I made it up):
int i = 0; i < BRICKS_IN_BASE; i++ //i=0,1,2,...,13 since BRICKS_IN_BASE=14
int j = BRICKS_IN_BASE; i > 0; i--) //j=14,13,12,...,1
int x = i*BRICK_WIDTH //x=0,30,60,...,390 since BRICK_WIDTH=30
int y = j*BRICK_HEIGHT //y=168,156,144,...,12 since BRICK_HEIGHT=12
GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT) //1st brick position(0,168), 2nd brick(30,156), 3rd brick(60,144),...

This is the way I made up this code.
But I don't know why it is wrong.
Hope you can help me now.
Thanks a lot.
• 11-20-2011, 11:40 AM
MisterNikos
Re: Draw a pyramid
Norm, maybe you didn't understand my question, because you didn't see the problem I want to solve.
Check out this website
http://see.stanford.edu/materials/ic...imple-java.pdf
and you will understand what is the program I want to run.
Thanks again.
• 11-20-2011, 02:53 PM
Norm
Re: Draw a pyramid
Add some printlns to your code to display the locations where you are placing the shapes to see if you are putting them in the places you want them to be.
Run the program and copy and paste here the print out that shows where the shapes are being placed.
• 11-20-2011, 03:08 PM
MisterNikos
Re: Draw a pyramid
Quote:

Originally Posted by Norm
Add some printlns to your code to display the locations where you are placing the shapes to see if you are putting them in the places you want them to be.
Run the program and copy and paste here the print out that shows where the shapes are being placed.

Sorry, didn't understand what to do.
This is my first code. I'm not familiar with printIn and other functions.
• 11-20-2011, 03:16 PM
Norm
Re: Draw a pyramid
You use the System.out.println(<String to print here>) method to display values on the console when the program executes.
The values you need to print out are the x and y locations for each shape and the i and j loop control values.
Also print out the BRICK_XXX variables that you use to control the loops.
• 11-20-2011, 03:20 PM
MisterNikos
Re: Draw a pyramid
I'll try to find out this printIn method and do it.
Thanks again, Norm.
• 11-20-2011, 03:36 PM
MisterNikos
Re: Draw a pyramid
I added the println and the results were these:
println (x);
println (y);
println (i);
println (j);

1
14
30
168
1
14
30
168
1
14
30
168
...
• 11-20-2011, 03:43 PM
Norm
Re: Draw a pyramid
You need to put labels on what is printed so you know what value is being printed: println ("x="+x);
You can print more than one value in a println: "x=" + x + ", y=" + y + ", i=" + i etc
It looks like all the values are the same. Are all the bricks being placed at the same location?
• 11-20-2011, 03:45 PM
MisterNikos
Re: Draw a pyramid
Yes, all bricks are placed at the same location.
x=30
y=168
i=1
j=14
• 11-20-2011, 03:46 PM
Norm
Re: Draw a pyramid
Time to look at your logic to see why the location values are not changing.
• 11-20-2011, 03:47 PM
MisterNikos
Re: Draw a pyramid
Quote:

Originally Posted by Norm
Time to look at your logic to see why the location values are not changing.

The mistake is somewhere at my logic. But WHERE? Pfffff, I can't find it.
• 11-20-2011, 03:54 PM
MisterNikos
Re: Draw a pyramid
Hmm, I found a mistake!
public void run() {
for (int i = 0; i < BRICKS_IN_BASE; i++) {
for (int j = BRICKS_IN_BASE; j > 0; j--) {
int x = i*BRICK_WIDTH;
int y = j*BRICK_HEIGHT;
GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);

Now, the new prints are:
i=0
j=14
x=0
y=168
i=0
j=13
x=0
y=156
i=0
j=12
x=0
y=144
i=0
...
• 11-20-2011, 03:56 PM
MisterNikos
Re: Draw a pyramid
And the result is this:
Attachment 2044
• 11-20-2011, 03:57 PM
Norm
Re: Draw a pyramid
You don't need to post the print outs. The prints are for you to use to solve your problem.
If you did want to ask a question about them, you could show a few lines, not all of them.
You can print more than one value on a line in a println: "x=" + x + ", y=" + y + ", i=" + i etc
• 11-20-2011, 03:58 PM
MisterNikos
Re: Draw a pyramid
Sorry, I just wanted to show them to you, in case you could help me.
I'm going to delete them and leave only one example.
• 11-20-2011, 04:01 PM
Norm
Re: Draw a pyramid
Quote:

Originally Posted by MisterNikos
And the result is this:
Attachment 2044

Now you need to think about how the shapes are positioned. How many shapes go on each line and where do they go on that line.
Take a piece of paper and draw the pyramid. Get the x,y location for the starting location of each shape on each row of shapes. Write those numbers down and look at the pattern for the numbers.
Where on first row, where on second row etc for all the rows.
• 11-20-2011, 04:05 PM
MisterNikos
Re: Draw a pyramid
I know the coordinates of each brick.
Now, I know that the ONLY problem is about the i.
x=0, y=168, i=0, j=14
x=0, y=156, i=0, j=13
x=0, y=144, i=0, j=12
x=0, y=132, i=0, j=11
x=0, y=120, i=0, j=10
x=0, y=108, i=0, j=9

If i was 1,2,3,... the x would be also correct and the pyramid would be fine.
There is something wrong with my i for loop, but I can't figure it out.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last