Results 1 to 20 of 24
Thread: Draw a pyramid
- 11-19-2011, 11:42 PM #1
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.
The result of this code is this:Java 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); } } } }

Could somebody please help me?
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.
Thank you in advance.
- 11-20-2011, 12:55 AM #2
Re: Draw a pyramid
Please explain what is wrong with the code in more detail.but it is wrong.
Not many people can help you with your third party classes.
- 11-20-2011, 01:52 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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, 09:53 AM #4
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, 10:40 AM #5
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, 01:53 PM #6
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, 02:08 PM #7
- 11-20-2011, 02:16 PM #8
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, 02:20 PM #9
Re: Draw a pyramid
I'll try to find out this printIn method and do it.
Thanks again, Norm.
- 11-20-2011, 02:36 PM #10
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, 02:43 PM #11
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, 02:45 PM #12
Re: Draw a pyramid
Yes, all bricks are placed at the same location.
x=30
y=168
i=1
j=14
- 11-20-2011, 02:46 PM #13
Re: Draw a pyramid
Time to look at your logic to see why the location values are not changing.
- 11-20-2011, 02:47 PM #14
- 11-20-2011, 02:54 PM #15
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);
add (brick);
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
...Last edited by MisterNikos; 11-20-2011 at 02:59 PM.
- 11-20-2011, 02:56 PM #16
- 11-20-2011, 02:57 PM #17
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, 02:58 PM #18
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, 03:01 PM #19
Re: Draw a pyramid
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, 03:05 PM #20
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.
Similar Threads
-
Need Help With Some Pyramid
By lord raza in forum New To JavaReplies: 1Last Post: 11-07-2011, 01:03 PM -
why cant i draw this box???
By stefandanielsen in forum New To JavaReplies: 2Last Post: 05-12-2011, 02:53 PM -
need help with Number Pyramid with Double Digits
By SmellyFoot in forum New To JavaReplies: 5Last Post: 03-29-2011, 12:04 PM -
pyramid
By Dashinator in forum New To JavaReplies: 8Last Post: 11-15-2010, 09:10 PM -
Will make a pyramid of some kind out of a number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:20 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks