SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pyramid GraphicsProgram, how to do it?

    Yo, I'm programming something here, I don't understand how I'll do it, I tried different codes but it all didn't work...

    So here's the problem:

    1. 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, as shown in the following sample run:



    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 (14)

    The numbers in parentheses show the values for this diagram, but you must be able
    to change those values in your program.

    Code:
    /*
     * 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;
    	
    	public void run() {	
    		
    	}
    }
    Can anyone help me solve this thing?? It's been like 7 hours and I still cannot solve it properly, it just fill my screen everytime I run the for loop...

    Come'on I am basically giving up with this program

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly have you tried so far? Without actually seeing your code attempts I can't really give you any suggestions.

  3. #3
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [Internet 2.0 Lingo]
    Word up G,

    Here's the dealy-O. Us hardcore code type'n balla's (double fist chest pump) are going to require more then an empty run method to help you out.

    We'z need'z allz your code and possibly the posting of your parent clazz. Ya dig?
    [/Internet 2.0 Lingo]

    Seriously, we won't be able to help you out until you post your parent class (which your professor should provide you) and some code showing where you're running into your problem.

    Best of Luck.
    baby steps... baby steps...

  4. #4
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    love the internet 2.0 lingo
    I needed a good laugh today

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yo yo,
    so here's the outcome that i should achieve



    I tried to write some code, many times actually.. but all epic failed...

    here's some sample code that i recently wrote

    Code:
    /*
     * 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;
    	
    	public void run() {	
    		int columnWidth = 30;
    		for (int i = 0; i < columnWidth; i = 30 + i)
    		{
    			int x = i;
    			for (int j = 0; j < BRICKS_IN_BASE; j++)
    			{
    				int y = BRICK_HEIGHT * j;
    				GRect brick2 = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    				add(brick2);
    				columnWidth = columnWidth * j;
    			}
    		}
    			
    	}
    }

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yo, so here's more reasonable code, still doesn't work though, i was thinking of deducting the base everytime i completed the outer for loop, but alas it didn't work cause i dont know where to put the base -= 1 thing.

    i was also thinking about sticking another for loop inside the inside for loop. but i thought that my mind would burst when i do that, so i didn't.

    the problem is im thinking linearly, i need to help myself to think parallel.. so any tip on doing that?? like thinking like dual core

    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 = 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;
    	
    	public void run() {	
    		int base = BRICKS_IN_BASE;
    		for (int i = 0; i < base; i++)
    		{
    			for (int j = 0; j < BRICK_HEIGHT; j++)
    			{
    					int x = BRICK_WIDTH * i;
    					int y = BRICK_HEIGHT * j;
    						
    					GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    					add(brick);
    			}
    		}
    	}
    }

  7. #7
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first loop is correct, the second one... well I'm not sure why using BRICK_HEIGHT there, not necessary. The first loop should keep track of the rows and the second one should be bricks per row. You want something like this

    Code:
    for (int i=0; i<rows; i++) {
     for (int j=0; j<bricksPerRow; j++) {
      //draw bricks here
     }
    }
    rows is going to be BRICKS_IN_BASE
    Bricks per row is very easy to calculate, you need to use i.

    The x and y coordinates are almost correct but you've got i and j mixed up there.

    You'll also have to deal with centering the bricks. What you have there will draw this (if you switch i and j)

    *
    **
    ***
    etc...

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rozner View Post
    The first loop is correct, the second one... well I'm not sure why using BRICK_HEIGHT there, not necessary. The first loop should keep track of the rows and the second one should be bricks per row. You want something like this

    Code:
    for (int i=0; i<rows; i++) {
     for (int j=0; j<bricksPerRow; j++) {
      //draw bricks here
     }
    }
    rows is going to be BRICKS_IN_BASE
    Bricks per row is very easy to calculate, you need to use i.

    The x and y coordinates are almost correct but you've got i and j mixed up there.

    You'll also have to deal with centering the bricks. What you have there will draw this (if you switch i and j)

    *
    **
    ***
    etc...
    hey dude, i actually solved the problem!! pretty cool man, thanks to the guys that didn't bother to help me, and to you cause you give me some tips. actually cause i was like squeezing my brain to actually solve this cheap problem, but anyway thanks man!!

    here's the million dollar code that haunt me for a day:
    Code:
    /*
     * 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;
    	
    	public void run() {
    		int totalPyramidWidth = BRICK_WIDTH * BRICKS_IN_BASE;
    		int totalPyramidHeight = BRICK_HEIGHT * BRICKS_IN_BASE;
    		int startingHeight = getHeight() - 0;
    		int startingWidth = (getWidth() - totalPyramidWidth) / 2;
    		int size = BRICKS_IN_BASE + 1;
    		int x = startingWidth;
    		for (int j = 0; j < BRICKS_IN_BASE; j++)
    		{
    			size--;
    			int pyramidWidth = size * BRICK_WIDTH;
    			x = (getWidth() - pyramidWidth) / 2;
    			startingHeight -= BRICK_HEIGHT;
    			for (int i = 0; i < size; i++)
    			{
    				GRect brick = new GRect(x, startingHeight, BRICK_WIDTH, BRICK_HEIGHT);
    				add(brick);
    				x += BRICK_WIDTH;
    			}
    		}
    	}		
    }
    Don't really know how I made this work, I need to solve problems like this hundred times i guess i grasp the concept.

    It took me from here to there:
    Code:
    /*
     * 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;
    	
    	public void run() {
    		int initBrick = 30;
    		int initPlacement = (getWidth() - BRICK_WIDTH) / 2;
    	
    		for (int i = 0; i < initBrick; i += 30)
    		{
    			int initX = i;
    			int x = initX + initPlacement;
    			GRect brick = new GRect(x, 0, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 60; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 15;
    			int y = 12;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 90; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 30;
    			int y = 24;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 120; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 45;
    			int y = 36;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 150; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 60;
    			int y = 48;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 180; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 75;
    			int y = 60;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 210; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 90;
    			int y = 72;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 240; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 105;
    			int y = 84;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 270; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 120;
    			int y = 96;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 300; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 135;
    			int y = 108;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 330; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 150;
    			int y = 120;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 360; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 165;
    			int y = 132;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 390; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 180;
    			int y = 144;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		for (int i = 0; i < 420; i += 30)
    		{
    			int initX = i;
    			int x = (initX + initPlacement) - 195;
    			int y = 156;
    			GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			add(brick);
    		}
    		
    	}
    }
    pretty freaking cool, cause look at that code, man its like the ugliest code i ever seen, but i managed to reduce it 85% by squeezing my homo sapien brain


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •