Results 1 to 6 of 6
  1. #1
    DLorien is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Requesting feedback on my first Java program.

    Hey forum peeps...

    Like many others here, I'm following the self-study Stanford CS106a program. The only problem is, I don't have anyone to let me know if my programs are good, bad, or otherwise. The Karel stuff was pretty simple and I didn't need any help with that. But once I got into the first Java assignment (Pyramid.java), I was really lost. After a week and trying this and that, I ended up with a solution that seems fairly simple. In fact, I'm surprised how simple it was. So, I compared mine to some other solutions I found on the net and I seem to have taken a whole different path to create my program. So, I thought I'd post it here and ask what other people thought of it. Am I on the right track? Is there something that I'm doing that could be done better? Throw me a grade (A - F) if you'd like. That way I can at least know where I stand.

    TIA

    DLorien

    Java 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.*;
    
    public class Pyramid extends GraphicsProgram {
    
    /** Width of each brick in pixels */
        private static final int BRICK_WIDTH = 30;
    
    /** Height 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() {
            buildRow();
        }
        
    /* 
     * The buildRow() method builds a pyramid top-down, using the window width or 
     * 1/2 of the pyramid width (whichever is wider) for it's center. It calls the 
     * following methods:
     * 
     *     getX() - Determines the first x coord for each row
     *     getY() - Determines the first y coord for each row
     * 
     */
        private void buildRow() {
    
        /* 
         * For each row, find the x and y coordinates for the first brick in the row
         */
            for (int ROW = 0; ROW < BRICKS_IN_BASE; ROW++) {
    
                double x = getX(ROW);
                double y = getY(ROW);
                
                /* 
                 * Build each brick in the row
                 */
                
                for (int brickNum = 0; brickNum < (ROW + 1); brickNum++) {
                    
                    // Build a brick
                    GRect brick = new GRect(x , y , BRICK_WIDTH , BRICK_HEIGHT);
                    add(brick);
                    
                    // Add another brick
                    x = x + BRICK_WIDTH;
                }
            }
        }
    
    /* 
     * For each row, find the x coordinate for the first brick in the row 
     * currently being built. This method receives the row number (ROW) as 
     * an integer from buildRow() and returns the result as a double.
     */
        private double getX(int ROW) {
    
            // Find the center
            double CENTER = getCenter();
    
            // Calculate number of bricks in the row
            int numBricks = ROW + 1;
            
            // Calculate the width of the row
            double rowWidth = numBricks * BRICK_WIDTH;
            
            // Subtract 1/2 of the row width from the center
            return CENTER - (rowWidth / 2);
        }
    
    /* 
     * For each row, find the y coordinate for the row currently being 
     * built. This method receives the row number (ROW) as an integer from 
     * buildRow() and returns the result as a double.
     */
        private double getY(int ROW) {
    
            return ROW * BRICK_HEIGHT;
        }
    
    /* 
     * Find the width of the window and the width of the pyramid. Build the
     * pyramid in the center of the window if the window is wider than
     * the pyramid. If the pyramid is wider than the window, use the middle 
     * of the pyramid as it's center.
     */
        private double getCenter() {
    
        double WINDOW_WIDTH = getWidth();
        double PYRAMID_WIDTH = BRICKS_IN_BASE * BRICK_WIDTH;
        
            if (WINDOW_WIDTH < PYRAMID_WIDTH) {
    
                return PYRAMID_WIDTH / 2;
    
            } else {
    
                return WINDOW_WIDTH / 2;
            }
        }
    }
    Last edited by DLorien; 12-04-2013 at 09:18 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Requesting feedback on my first Java program.

    When posting code, don't forget the highlight tags to preserve formatting.

    Also, code you post should be in the form of an SSCCE. It's going to be pretty hard to critique code that we can't run.

    The most important question to ask yourself is: does your code work? Do you understand it? If so, you've done your job. Any code you write will look like rubbish in 6 months (that's what learning does to you), so it's a bit of a waste of time to worry too much about style.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DLorien is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Requesting feedback on my first Java program.

    Thanks for the formatting tips. This was my first forum post, so I didn't realize I could do that. I'll make sure I watch that for future posts.

    As far as the program is concerned...it works perfectly. I understand it too. The point that Merhan (the teacher) keeps repeating, is whether another programmer can come behind you and easily figure out what you were doing, then modify it for new requirements, if necessary. If not, the code should be improved. By posting here in this forum, I'm asking that question. Would you, as another programmer, be able to figure out what the program does and modify it easily? Does it make sense or did I fail at making human understandable code?

    Obviously, this is a simple project, so it shouldn't be too hard to figure out from a veteran's perspective. But any input from the community will make my future programs better.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: Requesting feedback on my first Java program.

    I would remove all the multiline comments or turn them into single line comments if I were you, as you can see it messes up the forum's syntax highlighter.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Requesting feedback on my first Java program.

    I must applaud you for the amount of comments. Keep that habit up! It's really useful for the next programmer that has to maintain your code. Just be careful that the comments actually add some value to the code. Prevent comments like these:
    Java Code:
    //initialize i to zero
    int i = 0;
    //adds 1 to i
    i++;
    //initialize blah to FooBar
    String blah = "Foobar";
    //find the first character 
    char fist = blah.charAt(0);
    I am here commenting the obvious. Also, when you decide to refactor i to j, the comment doesn't make any sense anymore.

    Second, on line 45, 71 and 91 you use a variable ROW. First thing I thought it was some sort of constant (static final int), but it's not. That's because of how it's named: all uppercase. Please respect the Java naming conventions.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    DLorien is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Requesting feedback on my first Java program.

    Awesome feedback...thanks everyone. I actually went back and re-read the requirements and found that while my solution above works, it's technically wrong because it's supposed to build the pyramid bottom-up, not top-down. I went back to the drawing board and my new solution takes all your feedback into consideration. I also managed to get the code much shorter.

    Java Code:
    import acm.graphics.*; 
    import acm.program.*;
    
    public class FirstJavaProgram extends GraphicsProgram {
    	
    	private static final int BRICK_WIDTH = 30;
    	private static final int BRICK_HEIGHT = 12;
    	private static final int BRICKS_IN_BASE = 14;
    
    	public void run() {
    
    		for (int row = BRICKS_IN_BASE; row > 0; row--) {
    			
    			int y = row * BRICK_HEIGHT;
    			int x = getX(row);
    			
    			for (int brickNum = 0; brickNum < row; brickNum++) {
    			
    				buildBrick(x, y);
    				x = x + BRICK_WIDTH;
    			}
    		}
    	}
    
    	//Find x value for first brick in the row. 
    	private int getX(int rowNum) {
    
    		if (getWidth() < (BRICKS_IN_BASE * BRICK_WIDTH)) {
    
    			//If window is smaller than pyramid
    			return ((BRICKS_IN_BASE * BRICK_WIDTH) - (rowNum * BRICK_WIDTH)) / 2;
    
    		} else {
    				
    			//If window is larger than pyramid 
    			return (getWidth() - (rowNum * BRICK_WIDTH)) / 2;
    		}
    	}
    	
    	private void buildBrick(int x, int y) {
    		
    		GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(brick);
    	}
    }

Similar Threads

  1. Feedback on aspect-oriented programming in java
    By dismal-it in forum Forum Lobby
    Replies: 5
    Last Post: 08-08-2013, 11:48 AM
  2. Tic-Tac-Toe Version.1.0. Requesting feedback and help.
    By EscSequenceAlpha in forum New To Java
    Replies: 16
    Last Post: 05-29-2012, 04:36 PM
  3. Need feedback for my program.
    By Pojahn_M in forum New To Java
    Replies: 3
    Last Post: 08-12-2011, 08:12 PM
  4. Java (SCJP) Certification - Koenig solutions feedback
    By alishamary in forum Java Certification
    Replies: 2
    Last Post: 11-05-2009, 05:59 PM
  5. Help getting my program to continue requesting info.
    By antdilla22 in forum New To Java
    Replies: 4
    Last Post: 09-13-2009, 10:44 PM

Posting Permissions

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