Results 1 to 7 of 7
  1. #1
    fisch3r is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Post Learning Java, wondering about pyramid assignment.

    Hi there!

    First of all, I tried searching, but I couldn't find somebody who had the same problem as me.

    Here's my problem:

    I'm currently working my way through the free Stanford assignments, trying to learn Java.
    The only thing I can't solve is the pyramid part of assignment 2.

    In the assignment I have to draw a pyramid made up of bricks.
    I'm supposed to import some ACM files to draw the bricks.

    Here's my source so far:

    Java Code:
    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 = 40;
    
    /** 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;
    	
    	public void run() {
    		int brickNum = 0;									//Sets which number brick the program's drawing.
    		int yCoords = 0;									//*Had to be initiated here, otherwise the compiler complained.
    		int xCoords = 0;
    		int rowNum = 1 ;									//Sets which row we're in.
    		
    		for (int i = 0; i<BRICKS_IN_BASE; i++) { 			//Runs the loop until there's only one brick at the top.
    			for (int j=0; j<BRICKS_IN_BASE; j++) {			//Runs the loop until the program's reached BRICKS_IN_BASE
    				xCoords = (getWidth()/2)-(BRICKS_IN_BASE*BRICK_WIDTH/2)+(BRICK_WIDTH*brickNum); //Finds the center of the canvas, then finds the start of the bricks, then adds brickNum*BRICK_WIDTH.
    				yCoords = getHeight()-BRICK_HEIGHT;			//Finds the coords for the bottom of the window, then subtracts one BRICK_HEIGHT.
    				
    				GRect brick = new GRect(xCoords, yCoords, BRICK_WIDTH, BRICK_HEIGHT);
    				add(brick);									//Adds the brick at the coordinates above.
    				
    				brickNum++;									//Adds one to the brickNum, so that the bricks will be added after one another.
    			}
    		}
    		rowNum++;											//*Adds one to the rowNum, so we'll be in the row above the previous one.
    		yCoords = yCoords-BRICK_HEIGHT*rowNum;				//*Should set new yCoords, so we should be one row above.
    		xCoords = xCoords+(BRICK_WIDTH/2);					//*Should move the next row half a block to the right.
    	}
    }


    I've figured out how to draw the bottom line of the pyramid.

    The code I can't get to work is marked with an asterisk in the comments.

    I'm pretty sure the problem is that I'm unable to change the ints inside my second for loop.
    Is this correct? Is there any way to do so anyway? Because then my problem would be solved, I think.

    Thanks in advance for any help!
    Last edited by fisch3r; 03-01-2012 at 08:36 PM.

  2. #2
    fisch3r is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Learning Java, wondering about pyramid assignment.

    Why does the CODE-tag do that? That's pretty annoying.

    Any way to change the width?

  3. #3
    fisch3r is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Learning Java, wondering about pyramid assignment.

    My idea here is that after the second for-loop has run (that's the one that draws the boxes after each other), yCoords should be set a row above, which is why I'm subtracting one BRICK_HEIGHT.
    Then xCoords should be set half a box to the right.

    It just seems as if these changes aren't, well, changed... The program just draws fourteen (that's the NUMBERS_IN_BASE) rows at the bottom line.
    It just keeps the original x- and y- Coords.

    Is it impossible to change values inside a previous for-loop?

  4. #4
    Jez
    Jez is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: Learning Java, wondering about pyramid assignment.

    Hello I just found this forum and looked at your code, although i'm not a programmer but why do both the for loops use BRICKS_IN_BASE? Im thinking as this is static and you probably want to reduce it by one each time round the outer loop you will need to use another variable that is set to that value initally.

    Also why have brickNum and rowNum can you not use i and j?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Learning Java, wondering about pyramid assignment.

    Quote Originally Posted by fisch3r View Post
    Hi there!

    First of all, I tried searching, but I couldn't find somebody who had the same problem as me.

    Here's my problem:

    I'm currently working my way through the free Stanford assignments, trying to learn Java.
    The only thing I can't solve is the pyramid part of assignment 2.

    In the assignment I have to draw a pyramid made up of bricks.
    I'm supposed to import some ACM files to draw the bricks.

    Here's my source so far:

    Java Code:
    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 = 40;
    
    /** 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;
    	
    	public void run() {
    		int brickNum = 0;									//Sets which number brick the program's drawing.
    		int yCoords = 0;									//*Had to be initiated here, otherwise the compiler complained.
    		int xCoords = 0;
    		int rowNum = 1 ;									//Sets which row we're in.
    		
    		for (int i = 0; i<BRICKS_IN_BASE; i++) { 			//Runs the loop until there's only one brick at the top.
    			for (int j=0; j<BRICKS_IN_BASE; j++) {			//Runs the loop until the program's reached BRICKS_IN_BASE
    				xCoords = (getWidth()/2)-(BRICKS_IN_BASE*BRICK_WIDTH/2)+(BRICK_WIDTH*brickNum); //Finds the center of the canvas, then finds the start of the bricks, then adds brickNum*BRICK_WIDTH.
    				yCoords = getHeight()-BRICK_HEIGHT;			//Finds the coords for the bottom of the window, then subtracts one BRICK_HEIGHT.
    				
    				GRect brick = new GRect(xCoords, yCoords, BRICK_WIDTH, BRICK_HEIGHT);
    				add(brick);									//Adds the brick at the coordinates above.
    				
    				brickNum++;									//Adds one to the brickNum, so that the bricks will be added after one another.
    			}
    		}
    		rowNum++;											//*Adds one to the rowNum, so we'll be in the row above the previous one.
    		yCoords = yCoords-BRICK_HEIGHT*rowNum;				//*Should set new yCoords, so we should be one row above.
    		xCoords = xCoords+(BRICK_WIDTH/2);					//*Should move the next row half a block to the right.
    	}
    }
    I've figured out how to draw the bottom line of the pyramid.

    The code I can't get to work is marked with an asterisk in the comments.

    I'm pretty sure the problem is that I'm unable to change the ints inside my second for loop.
    Is this correct? Is there any way to do so anyway? Because then my problem would be solved, I think.

    Thanks in advance for any help!
    I've quoted the code so it's a sensible size (got rid of the SIZE tag).
    Try sticking some println()s in there to see what values you're working with and whether they match what you would expect.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    fisch3r is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Thumbs up Re: Learning Java, wondering about pyramid assignment.

    I printed some variables to the console, and saw the error. I simply forgot to reset a variable before the loop was run again.
    Thank you for the help.

    For anyone wondering, here's my final code:

    Java Code:
    /**
     * This program draws a pyramid
     */
    
    import acm.graphics.*;
    import acm.program.*;
    
    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 = 20;
    	
    	public void run() {
    		int brickNum = 0;											//Sets which number brick the program's drawing.
    		int rowNum = 1;												//Sets which row we're in.
    		
    		for (int i = BRICKS_IN_BASE+1; i>1; i--) { 					//Runs the loop until there's one brick at the top.
    			for (int j=i; j>1; j--) {								//Runs the loop until the loop has reached j.
    				int xCoords = (getWidth()/2)-(i*BRICK_WIDTH/2)+(BRICK_WIDTH*brickNum); //Finds the center of the canvas, then finds the start of the row, then adds one brick.
    				int yCoords = getHeight()-BRICK_HEIGHT*rowNum;		//Finds the coordinates for the bottom of the window, then subtracts one BRICK_HEIGHT.
    				
    				GRect brick = new GRect(xCoords, yCoords, BRICK_WIDTH, BRICK_HEIGHT);
    				add(brick);											//Adds the brick at the coordinates above.
    				
    				System.out.print("bricknum= " + brickNum);
    				brickNum++;											//Adds one to the brickNum, so the bricks will be added after one another.
    			}
    			brickNum=0;												//Resets brickNum.
    			rowNum++;												//Adds one to rowNum, so the next row will be drawn above.
    		}
    	}
    }

  7. #7
    Leandros is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Learning Java, wondering about pyramid assignment.

    Hey guys this is my solution for the pyramid assignment. Hope it helps someone
    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30;  /** Width of each brick in pixels */
    	private static final int BRICK_HEIGHT = 12;	/** Height of each brick in pixels */
    	private static final int BRICKS_IN_BASE = 14; /** Number of bricks in the base of the pyramid */
    	
    	public void run() {
    		int height=getHeight();
    		int width=getWidth();
    			
    		/** I want to manipulate the number of BRICKS_IN_NUMBER so I assign it to a variable double bricks*/
    		int bricks = BRICKS_IN_BASE;	
    		/** the first loop adds the same number of rows we have set for the number of bricks
    		 * the number can be changed by changing the BRICK_HEIGHT*/
    		for (int j=0; j<BRICKS_IN_BASE; j++){
    			height=height-BRICK_HEIGHT;
    			int initPos=(width-(BRICK_WIDTH*bricks))/2;//calculates the initial position of each row
    			bricks--;
    
    			for (int i=bricks; i>0; i--){
    				/** set the size of the brick*/
    				GRect brick = new GRect(BRICK_WIDTH,BRICK_HEIGHT);
    				/** set the location of the brick*/
    				brick.setLocation(initPos, height);
    				add(brick);
    				
    				initPos=initPos+BRICK_WIDTH;/** this sets the position of the next brick */
    			}
    		}
    	}
    }
    Last edited by Leandros; 04-02-2012 at 01:11 AM.

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. Replies: 12
    Last Post: 06-14-2011, 04:55 PM
  3. Wondering..~
    By ryozkidz in forum Java Applets
    Replies: 0
    Last Post: 01-20-2011, 10:09 PM
  4. pyramid
    By Dashinator in forum New To Java
    Replies: 8
    Last Post: 11-15-2010, 09:10 PM
  5. Replies: 2
    Last Post: 10-13-2008, 05:01 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
  •