Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: 2 sets of code, should work same but dont

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2 sets of code, should work same but dont

    Hi, I am learning Java by doing the Stanford CS106 class, and in one of the assignments, I need to make a pyramid. All fine with that, but when I tried to clean up my code, it seemed to break it, and I can not figure out why. I dont understand how they do not work the same. If someone can tell me why its not working the same, it would be greatly appreciated. The first code listed is the working one. I should probably say, the first one prints the pyramid correctly, the second code, from what I can see, just displays bricks (rectangles) and an even spacing straight across the screen,

    /*
     *Draws a pyramid with a base of 14, and 14 bricks high.
     */
     
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
     
    public class Pyramid extends GraphicsProgram {
     
    	private static final long serialVersionUID = 1L;
     
    	/** 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() 
     
    	{			
    		for (int i = 0; i < BRICKS_IN_BASE; i++)
    		{ 
    			int startingPointX = (getWidth() / 2) - (BRICKS_IN_BASE * (BRICK_WIDTH / 2));
    			int startingPointY = getHeight() - BRICK_HEIGHT;
    			startingPointX += ( i * BRICK_WIDTH / 2);
    			startingPointY -= (i * BRICK_HEIGHT);
     
    			for (int j = 0; j < BRICKS_IN_BASE - i; j++)
    			{
    				drawBrick(startingPointX, startingPointY);
    				startingPointX += BRICK_WIDTH;
    			}
     
    		}
    	}
     
    	private void drawBrick(int x, int y)
    	{
    		GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(brick);
    	}
     
    }

    /*
     *Draws a pyramid with a base of 14, and 14 bricks high.
     */
     
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
     
    public class Pyramid extends GraphicsProgram {
     
    	private static final long serialVersionUID = 1L;
     
    	/** 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 startingPointX = (getWidth() / 2) - (BRICKS_IN_BASE * (BRICK_WIDTH / 2));
    		int startingPointY = getHeight() - BRICK_HEIGHT;
     
    		int posX = startingPointX;
    		int posY = startingPointY;
     
    		for (int i = 0; i < BRICKS_IN_BASE; i++)
    		{ 
    			posX = startingPointX;
    			posY = startingPointY;
     
    			posX += ( i * BRICK_WIDTH / 2);
    			posY -= (i * BRICK_HEIGHT);
     
    			for (int j = 0; j < BRICKS_IN_BASE - i; j++)
    			{
    				drawBrick(posX, posY);
    				startingPointX += BRICK_WIDTH;
    			}
     
    		}
    	}
     
    	private void drawBrick(int x, int y)
    	{
    		GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(brick);
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 2 sets of code, should work same but dont

    For testing I have copied the logic for each set of code and replaced the drawBrick method with a print statement so I can see what values are being used. The code was put into a main method without any GUI.
    I have also reduced the sizes so only 6 rows of bricks are created.

    Doing the above will show you what the code is doing.

    The output for the first one:
    (0,188) (10,188) (20,188) (30,188) (40,188) (50,188)
    (5,176) (15,176) (25,176) (35,176) (45,176)
    (10,164) (20,164) (30,164) (40,164)
    (15,152) (25,152) (35,152)
    (20,140) (30,140)
    (25,128)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 sets of code, should work same but dont

    Quote Originally Posted by Norm View Post
    Can you copy and paste here the output from the two programs?
    Be sure to wrap it code tags to preserve the indentations.

    How can the code be compiled and executed for testing?
    I see imports of 3rd party packages.

    Can you post the minimum values for width and height that will work?
    The only output I get is a graphics display. I am sorry I am not sure about the 3rd party packages, ive just recently started this online class, I got all the information from the stanford website on how to import the acm package. I have my eclipse linked to stanford system, or something like that, as you can probably tell, im not quite sure what I am talking about regarding this.

    Any positive values would would work for the width and height, i believe.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 2 sets of code, should work same but dont

    We crossed while posting. See my last post for a way to easily see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 sets of code, should work same but dont

    Quote Originally Posted by Norm View Post
    We crossed while posting. See my last post for a way to easily see what the code is doing.
    Managed to solve it, done as you suggested, printed out the values and discovered there was something not right with the x/posX, turns out I was using the line startingPointX += BRICK_WIDTH; instead of posX += BRICK_WIDTH; So the value was not increasing like it should have been.

    Thank you for the help. Greatly appreciated.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: 2 sets of code, should work same but dont

    Glad you found it. Looks like a common problem from when code is copied and pasted and then modified. There is often a whoops I forgot to change ...
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I dont know what to do with this code
    By rbp1973rbp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2019, 06:17 AM
  2. I dont understand this code ?
    By ammehmeti in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 22nd, 2014, 07:57 AM
  3. Help i dont know how to make it work!
    By Freshtilldeath0 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2012, 07:40 PM
  4. Replies: 4
    Last Post: July 25th, 2011, 06:12 PM
  5. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM