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 16 of 16

Thread: Stanford CS106a Assignment 2: Pyramid Blocks

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Stanford CS106a Assignment 2: Pyramid Blocks

    I solved the pyramid block question from assignment 2. The original question was:

    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.
    I sort of solved the problem but in a very chaotic way and I didn't figure out every specification. I'm looking for someone to possible look at my code and help me clear my thinking for how to solve the problem properly.

    My code is DEFINITELY scary... Sorry just trying to figure things out.

    /*
     * File: PyramidBoxes.java
     * ------------------------
     * This program should make a pyramid of boxes with the following requirements
     * 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)
     */
     
    import acm.graphics.*;
    import acm.program.*;
     
    public class PyramidBoxes extends GraphicsProgram {
    	//Width of each brick
    	private static final int BRICK_WIDTH = 30;
    	//Height of each brick
    	private static final int BRICK_HEIGHT = 12;
    	//Number of bricks in the base
    	private static final int BRICKS_IN_BASE = 14;
     
     
    	public void run() {
    		//Set an integer for the height of the window to be manipulated.
    		int windowHeight = getHeight() - 13;
     
    //		GRect rect = new GRect(BRICK_WIDTH, windowHeight, BRICK_WIDTH,BRICK_HEIGHT);
    //		add (rect);
    		for (int j = 0; j < 14; j++){
    			buildRow((8*BRICK_WIDTH - j*(BRICK_WIDTH / 2)),(j * BRICK_HEIGHT), j);
    		}
     
    	}
    	/*
    	 * This method will build a row but will need to know the starting value for x,y
    	 */
    	private void buildRow(int startingX_Value, int startingY_Value, int bricks_in_base){
     
    		for (int i = bricks_in_base; i > 0; i--){
    			//This line creates a new instance of GRect with the cascading values
    			int changingX_Value = (startingX_Value + i * BRICK_WIDTH);
    			GRect rect = new GRect((changingX_Value),startingY_Value, BRICK_WIDTH, BRICK_HEIGHT );
    			add(rect);
    		}
    	}
    }
    Last edited by zammtech; November 16th, 2013 at 04:26 AM. Reason: bad formatting.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    I'm sure the educators who put the ACM program together know a lot more about educating new programmers and probably Java than I ever will, so my comments are not meant to diminish their expertise or the ACM teaching approach in any way. However, those of us who learned Java in a more traditional way - using the native language and API without a "helper" interface - the ACM package is foreign and obstructive to accomplishing what we already know how to do. Going back to learn how to use the ACM package to help those using it is ignoring the "right way" we've already learned, the requests to do so are rare, so the effort and pain doesn't seem justified.

    I don't want to discourage you or suggest that you won't find help with ACM here. I think the pool of those able and willing to help is very small but not completely dry. Note the topic you found on the program was 3+ years old and didn't get much of a response. Heck, I might even take the time to figure it out if it'll help someone, but I think the demand for it is very small and my commitment is weak.

    The code you posted is not as scary as you make it out to be. There are false starts and incomplete ideas, but that's typical. A few variables are not used, but those may be lines you didn't comment out. I don't know how they teach variable naming, but the use of underscores in variable and method names outside constants is not typical.

    Having seen these topics for a few years, I've always wondered what the transition is like from the ACM way to a more traditional Java program structure and design, so if you ever make that leap, please report back.

    And even though I mean to defer to the goals and expertise of the ACM educators, I have to make one editorial, opinion only comment: It seems the ACM approach is built on a complete lack of faith in either their abilities to teach or the abilities of their students to learn the real structure of a Java program from the ground up. Okay, I'm done.

    Good luck and be well!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    I agree with Greg's comments. Using the ACM classes may make it easy to write simple GUI code but it appears to me to be a dead-end. The transition to standard java coding will leave most of the ACM stuff behind and will require the student to start over to learn the real way things are done in java.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Thank you for your replies. However, I am a bit ignorant as I am fairly new to programming and even newer to Java. Can you direct me to some tutorial or work that would be able to provide more detail regarding the difference between using the ACM classes and the 'real way things are done in Java'. This statement to me means nothing without any substance behind it.

    I'm not trying to be rude, but I chose this course as an introduction because of lack of information regarding the topic. Therefore instead of a lecture regarding a material I've used I would appreciate more helpful information as to point me in the direction you are speaking of.

    Thank you for your time as I do want to continue learning and pushing forward.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    That was my personal opinion. I'm not familiar with ACM but I have seen several students post questions about it. As Greg said, not many of us have used ACM, so the quality of help you'll get here could be very shallow.
    Again, my impression of ACM is that after a student has mastered it, he will have to forget most of what was learned and start over to learn the classes in Java SE. I'd think that using ACM is to give a student the "flavor" of programming without having to learn the nuts and bolts of java programming.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    I appreciate your response. After doing a small amount of searching I see how the ACM makes Java a bit different. I'm not to fussed about learning one thing or another, I just want to learn programming and problem solving skills which one day I can potentially use to make something interesting or work on a project with someone.

    When I was younger I played around with Visual Basic 3.0 and made a ton of programs but never made my own code. I used other peoples .bas files and used their code to make my own programs. This process is now me trying to develop my own code and problem solving skills. I'm open to a different book / set of tutorials or any insight that may be helpful.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    This link to a master index for java programming tutorials:
    The Really Big Index
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Excellent point, and I'm sorry you took my comments as a "lecture" with the negative connotations that term usually brings. Mostly, I didn't want you to stop what you were doing if you were excited about it, but I appreciate that you're willing to consider alternatives.

    The Java Tutorials are excellent supplementary material, but they may not be structured and complete enough to delve as deeply into each topic as you might desire. Refer to them as needed.

    I started with the free online book by David Eck, also available for download in a number of forms. Most any book will do (avoid the ones that promise results in a short time), but whichever you choose, I recommend you start on page one, read every word, type every example, run it, understand it, complete the chapter material, then do every exercise at the end of each chapter before moving to the next. If you have questions, come here and ask.

    I'm not personally fond of video tutorials, but I've heard medium to good reviews for the Java series done by The New Boston. Search for that and you should find them easily.

    While Eck's book was an excellent resource for me, when the material got confusing, I found it very helpful to refer to a number of other resources to get the same material presented in different ways. I probably did at least 10 different basic Swing tutorials before the subject finally "clicked," and I felt like I was beginning to understand it. Unfortunately, I know now that many (most?) of the Swing tutorials on the Internet are crap. But you don't need to worry about those for a while.

    Good luck!

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Quote Originally Posted by zammtech View Post
    I sort of solved the problem but in a very chaotic way and I didn't figure out every specification.
    What do you mean by "sort of" and "every specification"?
    Does the code produce the pyramid?

    Quote Originally Posted by zammtech View Post
    I am fairly new to programming and even newer to Java.
    A statement worth a thousand words. Understand that the ACM, as presented in CS106a, is an introduction to programming, and not a replacement for Java. It is meant to get one to understand basic programming constructs - variables, loops, methods... - while hiding some of the more complicated syntax. If you treat the ACM as the tool it is, and complete the course, by the end you will have learned something about "how to program" and can then start to learn any language of your choice, Java or not.

    The reason ACM is implemented through Java is because of Java's Object Oriented design, implementation hiding fits in naturally. Being implemented in Java makes Java a popular "next language" (I call it that because as said before, one should not treat ACM as a replacement for Java), but you can apply the basic constructs to just about any language you want to learn.

  10. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Quote Originally Posted by jps View Post
    What do you mean by "sort of" and "every specification"?
    Does the code produce the pyramid?

    A statement worth a thousand words. Understand that the ACM, as presented in CS106a, is an introduction to programming, and not a replacement for Java. It is meant to get one to understand basic programming constructs - variables, loops, methods... - while hiding some of the more complicated syntax. If you treat the ACM as the tool it is, and complete the course, by the end you will have learned something about "how to program" and can then start to learn any language of your choice, Java or not.

    The reason ACM is implemented through Java is because of Java's Object Oriented design, implementation hiding fits in naturally. Being implemented in Java makes Java a popular "next language" (I call it that because as said before, one should not treat ACM as a replacement for Java), but you can apply the basic constructs to just about any language you want to learn.
    Thank you for this comment. After hearing the previous comments I immediately began to look for alternatives and feel awful because I'm starting from someone else 'new introduction' and I don't know where to start or begin. I think I will continue with the course because I find it engaging mentally and the instructor and the book following each other closely and the instructor describes things throughly. However, now I know that what I'm doing isn't 100% Java! (Before I didn't have a clue!)

    Quote Originally Posted by GregBrannon View Post
    Excellent point, and I'm sorry you took my comments as a "lecture" with the negative connotations that term usually brings. Mostly, I didn't want you to stop what you were doing if you were excited about it, but I appreciate that you're willing to consider alternatives.

    Good luck!
    Thank you for your reply. If you read my comment before, I didn't realise anything particular about the ACM.jar prior to your comment. Now after seeing a different tutorial I notice the difference in programming for some input using java.util.Scanner then say ACM. I do think I will finish the course with the understanding NOW, that I'm not programming 100% Java, but that I am learning the Methodology of programming. The real syntax of Java will be revealed later.

    Quote Originally Posted by Norm View Post
    This link to a master index for java programming tutorials:
    The Really Big Index
    Thanks I'll have a look at these I think after I finish my course.

    --- Update ---

    Quote Originally Posted by jps View Post
    What do you mean by "sort of" and "every specification"?
    Does the code produce the pyramid?
    Well it does make a Pyramid to the specifications but it is not centered in the screen. The way I approached the problem was to first create a row, then figure out how to make columns of blocks. After which I figured out how to make each row of blocks move in so it looked like a pyramid rather then a set of stairs. However, I haven't centered it. Guessing from the about comments the ACM lines of code for getHeight() and getWidth() are different then non-acm code.

    I'm was wondering if there are some ways to clean up my code to make it more easily readable. Also is this a decent way to be thinking about decomposition?

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    I just had a thought that would be appealing for learners as well as me possibly receiving additional help. I'm wondering what if I did all of the exercises and homeworks from the course I'm taking using ACM first to build theory and then figure out the JAVA methods to solving these problems. I believe it would take me much longer to complete the course, but at the same time I would be developing my Java skills more quickly.

    Additionally, if I were to start a blog or have a central place to post my findings, it could be beneficial to other students who are taking the CS106a course by Stanford.

    What do you guys think about this?

  12. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Quote Originally Posted by zammtech View Post
    now I know that what I'm doing isn't 100% Java! (Before I didn't have a clue!)
    Quote Originally Posted by zammtech View Post
    with the understanding NOW, that I'm not programming 100% Java, but that I am learning the Methodology of programming. The real syntax of Java will be revealed later.
    On these two points, just to clarify, you are working in Java and with Java. This is also quietly introducing some very important parts of Object Oriented Programming. You are using existing code to implement your own functionality (extending graphics program). Using methods from ACM classes like readInt.. OOP concepts are quietly hidden in there, and you will see more of that as you continue. So this is more "real Java" than it may seem. Java was designed as an Object Oriented language and I think the use of the ACM is taking an Object Oriented approach to learning how to program with an Object Oriented language, as long as you understand that this is what is going on. (...and that the ACM is a third party package and not in the SE)


    Quote Originally Posted by zammtech View Post
    Well it does make a Pyramid to the specifications but it is not centered in the screen.
    See what you can do to work on getting them centered then. Find out how large the display is, the size of each brick, and how many bricks on the row, throw in a little math and you should have them centered.

    Quote Originally Posted by zammtech View Post
    The way I approached the problem was to first create a row, then figure out how to make columns of blocks. After which I figured out how to make each row of blocks move in so it looked like a pyramid rather then a set of stairs. However, I haven't centered it. Guessing from the about comments the ACM lines of code for getHeight() and getWidth() are different then non-acm code.

    I'm was wondering if there are some ways to clean up my code to make it more easily readable. Also is this a decent way to be thinking about decomposition?
    getWidth and getHeight can be easily found in "regular Java".
    For the decomposition, start with the most general idea, build a pyramid of bricks.
    Break that problem down into smaller problems, like build a row of bricks.
    That can be broken down into building just a single brick.
    To look at the code, you have somewhat done that. I would go with a method that builds a pyramid. That method would take as parameters, all information needed to build a pyramid. That method would call a method to build one row, and pass that method what was needed to correctly produce each row one at a time. Finally the row method would use a method to construct each individual brick one at a time. (just my opinion.. not necessarily the only or best solution for any given context)
    Looks good so far except I do not like to see hard-coded values like 13, 14, 8.. really any numerical value other than 0, 1, and 2 should be represented by a constant for readability in the code, (in the same way BRICK_WIDTH is done)

  13. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Thank you for the clarification. I think I'm learning a bit more as I research and attempt learning more. I have recoded the code and would maybe even do it again (as a learning opportunity) to build methods according to the decomposition that you have described. Here is my updated code which looks much cleaner and doesn't integrate the hard-coded values. I put those in at first to be able to see the pyramid without thinking about centering properly.
    /*
     * 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 baseWidth = BRICKS_IN_BASE * BRICK_WIDTH;
    		int bricksInRow = BRICKS_IN_BASE;
    		int startX = (getWidth() / 2) - (bricksInRow / 2) * BRICK_WIDTH;
    		int startY = getHeight() - BRICK_HEIGHT;
    		int x = startX;
    		int y = startY;
    		for (int i = 0; i < BRICKS_IN_BASE; i++){
    			x = startX + i * BRICK_WIDTH / 2;
    			for (int j = 0; j < bricksInRow; j++ ){
    				GRect rect = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    				add (rect);
    				x += BRICK_WIDTH;
    			}
    			y -= BRICK_HEIGHT;
    			bricksInRow--;
     
    		}
    	}
    }

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Quote Originally Posted by zammtech View Post
    maybe even do it again (as a learning opportunity) to build methods according to the decomposition that you have described.
    I think that is a good idea too. When you break it down, try to design it in a very general way. For example, when I thought about building a pyramid, I thought about that pyramid being a number of rows of a number of bricks each. With that in mind the more general idea is that you could also build a staircase. Or numerous other shapes.. So as you break it down, break it down in general terms and try not to include specifics for "building a pyramid centered in a space". Those specifics are unique to the program that "builds a pyramid centered in a space" an should be kept separate from a method that builds a brick or a row of bricks.

  15. #15
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    Ahh, so during the decomposition I should be focusing on generalising the problem and only use specifics when I have solved the generalised problem. I'm currently working on a similar assignment regarding making a checker board from squares and circles and I will try to use this idea. Thanks.

  16. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Stanford CS106a Assignment 2: Pyramid Blocks

    If you think about a brick being a rectangle with equal sides, and you think about the rows of the pyramid being of the same number of bricks, then the generalized version of the program would build the pyramid or the checker board with the same methods given different values.

Similar Threads

  1. CS106A
    By genux in forum The Cafe
    Replies: 2
    Last Post: November 16th, 2013, 03:40 AM
  2. Representing tones visually in blocks
    By Nindustries in forum Java Theory & Questions
    Replies: 9
    Last Post: January 20th, 2013, 01:49 PM
  3. Boucing ball from stanford uni
    By NoviceJAVA in forum AWT / Java Swing
    Replies: 0
    Last Post: October 8th, 2011, 05:38 PM
  4. java ceaser cipher stanford lectures
    By forms in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 25th, 2011, 05:23 PM
  5. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 08:12 AM