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;
		}
	}
}