Created
March 14, 2015 13:18
-
-
Save kenechiokolo/af10ca9de5c9ef287d88 to your computer and use it in GitHub Desktop.
Programming Methodology CS106A: Assignment 2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
// Height of pyramid | |
double pyramidHeight = BRICKS_IN_BASE; | |
// Number of bricks in current row | |
double rowBricks = BRICKS_IN_BASE; | |
public void run() { | |
double i = 0; | |
while (i < pyramidHeight) { | |
buildRow(); | |
rowBricks -= 1; | |
i++; | |
j++; | |
} | |
} | |
// j is declared outside of a method because it used in both run and buildRow | |
double j = 0; | |
// builds a pyramid row | |
private void buildRow() { | |
for (double i=0; i < rowBricks; i++) { | |
double x = getWidth() / 2 - (BRICK_WIDTH/2); | |
double y = getHeight() - (BRICK_HEIGHT + j*BRICK_HEIGHT); | |
GRect brick = new GRect (x+(i*BRICK_WIDTH) - BRICK_WIDTH*(rowBricks/2), y, BRICK_WIDTH, BRICK_HEIGHT); | |
add (brick); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment