Problem 1

On the assignment 2, we are dealing more with graphics, e.g. rectangles and lines and some more loops like for .. loop. It is using the acm library which helps from hiding some of the java implementations.

In the assignment 1 above link, I outline how to compile and run the problems within a standard eclipse and in this assignment 2 we are using the same idea of using the run configuration to run the correct java file that we are working on.

Here is the Problem 1 : Brick Pyramids, “Write a GraphicsProgram subclass that draws a pyramid consisting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid.

The pyramid should be centered at the bottom of the window and should use constants for the following parameters:

BRICK_WIDTH The width of each brick (30 pixels)
BRICK_HEIGHT The height of each brick (12 pixels)
BRICKS_IN_BASE The number of bricks in the base (12)

The numbers in parentheses show the values for this diagram, but you must be able to change those values in your program and still produce a reasonable picture.

My answer, was to start from the bottom of the pyramid and build up, with building up we are wanting to go from the BRICKS_IN_BASE to 1 (or >0) and then find the middle part of the screen (getWidth / 2) (this finds the middle of the screen – the width divided by 2), but since we are wanting to start the pyramid bricks from the left then minus from this middle of the screen (I called this startX) the number of bricks width that we want to build on that level divided by 2 (this is because we figure out the bricks size (number of bricks for this level) * the number that we are putting on this level (e.g. 10) then divide this by 2, which gives a offset to start from)

The startY means where we want to start the building of bricks, which is at the bottom of the screen (-1 so we can see the bottom of the brick height (BRICK_HEIGHT)).

Here is the source code for this assignment but I have included the full assignment code in the above zip file and also the PDF which is the assignment questions.

/*
 * File: Pyramid.java
 * Name: Genux (Ian Porter)
 * 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.*;
 
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 = 12;
 
	public void run() {
		/* You fill this in. */
		int startY = getHeight()-BRICK_HEIGHT;
		int startX;
		for (int i = BRICKS_IN_BASE; i >0; i--)
		{
			startX = getWidth() / 2;
			startX -= i * (BRICK_WIDTH/2);
			for (int j = i; j > 0; j--)
			{
				GRect rect = new GRect(startX,startY,BRICK_WIDTH,BRICK_HEIGHT);
				add(rect);
				startX += BRICK_WIDTH;
			}
			startY -= BRICK_HEIGHT;
		}
	}
}

11 thoughts on “Problem 1”

  1. Could you please look at this, (I’ve been on it for the last 6 hours) it’s my version of the same problem and slightly different, however I cant find my mistake… and I don’t understand why my version doesn’t work…

    * File: Pyramid.java
     * ------------------
     * This program is a stub for the Pyramid problem, which draws
     * a brick pyramid.
     */
     
    import acm.graphics.*;
    import acm.program.*;
     
    public class Pyramid extends GraphicsProgram {
     
    	private static final int BRICK_WIDTH = 30;
    	private static final int BRICK_HEIGHT = 12;
    	private static final int BRICKS_IN_BASE = 12;
     
    	public void run() {
     
    		int centreX = getWidth()/2;
    		int bottomY = getHeight();
    		for(int i=BRICKS_IN_BASE; i >0; i--){
    				for(int j = BRICKS_IN_BASE; j >0; j--){
    					GRect gBrick = new GRect(centreX - j*BRICK_WIDTH/2, bottomY - BRICK_HEIGHT*(i-(i-8)), BRICK_WIDTH, BRICK_HEIGHT);
    					add(gBrick);
    				}
    		}
    	}
    }
  2. Just on my mobile at present, but looking at your source code it is a guess but the two loops need to referr eg
    For (int i=0-9 ….
    for ( int j = i – 9

    So that the first one i loops over 0-9 and the second one j loops
    0-9
    Then
    1-9
    Then
    2-9
    Etc
    Hth

    Shall have check later

  3. Hi Sanjay,

    J is the local looping variable that is using the value of i as a starting point to then build up the bricks from that height, since “i” holds the value of the height of the bricks at that point.

    HTH

  4. Thanks for the quick earlier response Genux.

    I’m not sure what is wrong but I receive the following error message when I try to compile the code you have posted above.

    “Selection does not contain a main type (do you have a public void run() method?)”

    I see warnings like: “GraphicsProgram cannot be resolved to a type”, “The import acm cannot be resolved” & “The method getHeight() is unresolved for the type Pyramid” next to the lines of code as well.

    I’ve downloaded acm.jar from jtf.acm.org but I am not sure where to place that file. It is in the plugin section of the eclipse folder for now.

    Hope you can help me out with compiling this program without errors. Thanks in advance.

  5. Thanks for the tip Genux. However, I think the mistake was mine. I forgot to import the starter code for these assignments. Its working now. 🙂

  6. public class Pyramid extends GraphicsProgram {
     
    	/* define your constants here. */
    	private static final int BRICK_WIDTH = 30;
    	private static final int BRICK_HEIGHT = 12;
    	private static final int BRICKS_IN_BASE = 12;
    	private static double canvasWidth,canvasHeight,startX,originalX;
    	private static int newXloc,numBricks,row;
     
     
     
        public void run() {
        	// find out the canvas size
        	canvasSize();
        	buildPyramid();
        }
     
        private void canvasSize() {
        	canvasWidth = getWidth();
        	canvasHeight = getHeight();
        }
     
        private void buildPyramid() {
     
        	// find out the x location of the 1st brick to lay at 1st row
        	startX = (canvasWidth-(BRICKS_IN_BASE*BRICK_WIDTH))/2;
     
        	numBricks = BRICKS_IN_BASE;
        	row = 1;
     
     
        	/* canvasHeight-BRICK_HEIGHT*row >= BRICK_HEIGHT is check to ensure that 
        	 * bricks are not laid over the top of the canvas screen because the block height
        	 * can vary
        	 * 
        	 * The x location of the brick is calculated by adding half the brick width to the
        	 * original X location of the 1st brick at each row. Within the for loop the x
        	 * location of each brick is calculated by adding the brick width to startX.
        	 * 
        	 * The y location of the brick is calculated by canvasHeight-BRICK_HEIGHT*row
        	 * 
        	 * After laying a row the numBricks is decrease as the row is increased.
        	 * The new x location of the brick has to be added to the brick width via the formula 
        	 * newXloc = newXloc + BRICK_WIDTH/2.
        	 */
     
        	while (numBricks >= 1 && canvasHeight-BRICK_HEIGHT*row >= BRICK_HEIGHT) {
     
        		originalX = startX;
     
        		for (int i=1; i= 2) {
        			startX = originalX + BRICK_WIDTH/2;
        		}
     
        	}
        }
     
    }
  7. The original code for the last few lines did not appear in my previous post. Reposting the last portion of the buildPyramid private class…hope it appears fine this time round..

    while (numBricks >= 1 && canvasHeight-BRICK_HEIGHT*row >= BRICK_HEIGHT) {

    originalX = startX;

    for (int i=1; i= 2) {
    startX = originalX + BRICK_WIDTH/2;
    }

    }

  8. /*
     * File: Pyramid.java
     * ------------------
     * This program draws a pyramid of bricks arranged in horizontal rows.
     * The number of bricks in a row decreases by one as one moves up the pyramid
     */
     
    import acm.program.*;
    import acm.graphics.*;
     
    public class Pyramid extends GraphicsProgram{
     
     
    	private static final int BRICK_WIDTH = 30;			
    	private static final int BRICK_HEIGHT = 12;
    	private static final int BRICKS_IN_BASE = 20;
     
    	/*
    	 * The pyramid is created by laying down rows or bricks from left to right and decrementing 
    	 * the number of bricks by one as for each subsequent row.
    	 */
    	public void run(){
    		for(int i=BRICKS_IN_BASE; i>0; i--){			//i represents the number of bricks in a row
    			updateStartCoordinatesForNextRow(i);
    			laydownRowOfIBricks(i);
    		}
    	}
     
    	/*
    	 * This method lays down a row of bricks. A row is laid down by updating the x and y coordinates 
    	 * for the next brick and then placing the brick
    	 */
    	private void laydownRowOfIBricks(int nBricksInRow){
    		for(int i=0; i<nBricksInRow; i++){				//i represents the brick number in the row: i=0 is the fist brick, i=1 is the second brick...
    			updateCoordinatesForNextBrick(i);
    			placeBrick();
    			pause(100);
    		}
    	}
     
     
    	/*
    	 * This method determines the x and y coordinates for the first brick of each row. For the bottom row,
    	 * these coordinates are determined using the width and height of the graphics window as well as the number 
    	 * of bricks in the bottom row and the brick dimension
    	 * For subsequent rows, the x coordinate of the first brick will be midway through the first brick of the previous row
    	 * and the y coordinate will be one BRICK_HIEGHT above the y coordinate of the previous row
    	 */
    	private void updateStartCoordinatesForNextRow(int NBricksInRow){
    		if(NBricksInRow == BRICKS_IN_BASE){
    			startXCoordForNextRow = getWidth()/2 - (BRICKS_IN_BASE/2)*BRICK_WIDTH;
    			startYCoordForNextRow = getHeight() - BRICK_HEIGHT;
    		} else {
    			startXCoordForNextRow += BRICK_WIDTH/2;
    			startYCoordForNextRow -= BRICK_HEIGHT;
    		}
    	}
     
    	/*
    	 * This method determines the x and y coordinates of the next brick to be placed
    	 * The y coordinate of the next brick is always the same as the y coordinate of the row
    	 * For the first brick of the row i.e (brickNumber == 0), its x coordinate is equal to the starting x coordinate for the row.
    	 * For each subsequent brick, its x coordinate is the previous x coordinate plus the brick width.
    	 */
    	private void updateCoordinatesForNextBrick(int brickNumber){
    		YCoordForNextBrick = startYCoordForNextRow;
    		if(brickNumber == 0){
    			XCoordForNextBrick = startXCoordForNextRow;
    		} else {
    			XCoordForNextBrick += BRICK_WIDTH;
    		}
     
    	}
     
    	/*
    	 * This method places down a brick which is just a GRect
    	 */
    	private void placeBrick(){
    		add(new GRect(XCoordForNextBrick, YCoordForNextBrick, BRICK_WIDTH, BRICK_HEIGHT));
    	}
     
     
     
    	//instance variables
    	private int startXCoordForNextRow;			//x coordinate for the first brick in a row
    	private int startYCoordForNextRow;			//y coordinate for the first brick in a row	
    	private int XCoordForNextBrick;				//x coordinate for the next brick to be placed	
    	private int YCoordForNextBrick;				//y coordinate for the next brick to be placed
  9. /*
     * 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.*;
     
    public class Pyramid extends GraphicsProgram {
     
    	/** Width of each brick in pixels */
    	private static final int BRICK_WIDTH = 30;
     
    	/** Height 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 static int ROW = 0, LAID = 0;
     
    	public void run() {
    		layBricks();
    	}
     
    	/**
    	 * Lay bricks:
    	 * Lays bricks with the width and height based on static variables
    	 */
    	public void layBricks() {
    		while (LAID < getBricksToLay()) {
    			GRect brick = new GRect(getBrickPlaceX(), getBrickPlaceY(), BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    			LAID++;
    		}
    		ROW++;
    		if (LAID != 1) {
    			LAID = 0;
    			layBricks();
    		}
    	}
     
    	/**
    	 * Returns brick amount to lay for the row
    	 */
    	public int getBricksToLay() {
    		return BRICKS_IN_BASE - ROW;
    	}
     
    	/**
    	 * Returns X coordinate for current brick to be lain
    	 */
    	public int getBrickPlaceX() {
    		return ((getWidth() - (getBricksToLay() * BRICK_WIDTH)) / 2) + BRICK_WIDTH * LAID;
    	}
     
    	/**
    	 * Returns Y coordinate for current brick to be lain
    	 */
    	public int getBrickPlaceY() {
    		return (getHeight() - BRICK_HEIGHT) - (BRICK_HEIGHT * ROW);
    	}
    }

Leave a Reply

Your email address will not be published. Required fields are marked *