Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Draw a pyramid

  1. #1
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.
    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);
    		}
    		}
    	}
    }
    The result of this code is this:
    Draw a pyramid-screen-shot-2011-11-20-12.36.46-.-..png

    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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: Draw a pyramid

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

    Not many people can help you with your third party classes.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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?

  4. #4
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  5. #5
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default 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.

  7. #7
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    Quote Originally Posted by Norm View Post
    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.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default 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.

  9. #9
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    I'll try to find out this printIn method and do it.
    Thanks again, Norm.

  10. #10
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default 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?

  12. #12
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    Yes, all bricks are placed at the same location.
    x=30
    y=168
    i=1
    j=14

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: Draw a pyramid

    Time to look at your logic to see why the location values are not changing.

  14. #14
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    Quote Originally Posted by Norm View Post
    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.

  15. #15
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  16. #16
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    And the result is this:
    Draw a pyramid-screen-shot-2011-11-20-3.55.21-.-..png

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default 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

  18. #18
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: Draw a pyramid

    Quote Originally Posted by MisterNikos View Post
    And the result is this:
    Click image for larger version. 

Name:	Screen Shot 2011-11-20 at 3.55.21 μ.μ..png 
Views:	6 
Size:	34.4 KB 
ID:	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.

  20. #20
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need Help With Some Pyramid
    By lord raza in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 01:03 PM
  2. why cant i draw this box???
    By stefandanielsen in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:53 PM
  3. need help with Number Pyramid with Double Digits
    By SmellyFoot in forum New To Java
    Replies: 5
    Last Post: 03-29-2011, 12:04 PM
  4. pyramid
    By Dashinator in forum New To Java
    Replies: 8
    Last Post: 11-15-2010, 09:10 PM
  5. Will make a pyramid of some kind out of a number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •