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; } } } |
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…
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
I don’t understand your use of the j=i? How do you build up rows, I don’t see j related to anything?
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
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.
Hi Zig,
If you goto the http://www.codingfriends.com/index.php/2010/05/10/problem-1-2/ then you will be able to setup the eclipse environment to work with this :).
HTH
Genux
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. 🙂
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;
}
}