Page 1 of 6 123 ... LastLast
Results 1 to 20 of 117
  1. #1
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default CS106A Stanford University

    I'm currently learning (or trying to learn) Java through a series of lectures from Stanford University; I'm sure some of you are familiar with this course as it's available on YouTube.

    I found the first assignment a little difficult, however I did get there in the end. I'm now on the second assignment and I'm still having difficulty. Mehran Sahami is an amazing teacher, so I'm starting to wonder whether I just simply don't have the brain for programming.

    I find it difficult to visualize what's going on exactly, for example, on the second assignment you have to create a pyramid http://see.stanford.edu/materials/ic...imple-java.pdf and upon using two loops to calculate the x and y axis for each row it all gets a little confusing. In the end I did manage to get the correct rectangles per row however all the rows started from 0 (x axis) so of course it wasn't a pyramid. I can't figure out how exactly I'm supposed to get the next row to start a further plus 15 pixels (half a rectangle) further than the previous row. And then of course, even if I did manage to sort that out I'd also have to figure out how to center the whole thing and push it to the bottom of the window.

    Anyway, I found someone else's code on this forum but I'm still finding it difficult to visualize what's happening in the code. I can't follow what's happening in my head, especially when other methods are being called.

    My Math is very, very lacking and I wonder whether this could be part of the problem. I'm 20 years old however my Math ability is probably the same as that of a 13 year old! (not something that I'm proud of, but that's another story).

    Would getting my Math up to a good standard help I wonder? Or do some people just not have what it takes to program?

    There's also a book titled "The Art and Science of Java" which has some reading material to be read before each lecture, I'm also having difficulty with the programming assignments on that.

    Any advice would be much appreciated!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Learning Java View Post
    Would getting my Math up to a good standard help I wonder?
    Yes; although it sounds blunt if you are wondering about two dimensional pyramids (just triangles actually) you really have to work a bit on your (discrete) math. You don't have to become a rocket scientist but reaching a good standard is really a prerequisite when it comes to programming.

    kind regards,

    Jos

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    This assignment is a pyramid in more ways than one, in that it should be layers built on layers. Approach it this way:

    1. Write a drawBrick(double x, double y) method that just draws a single GRect. Test it by calling drawBrick(10, 5) in your run() method.
    2. Write a drawRowOfBricks(double x, double y, int numBricks) method that calls your drawBrick() method to draw a row of bricks. Change your run() method to call drawRowOfBricks(10, 5, 6) to test it.
    3. Write a drawCenteredRowOfBricks(double y, int numBricks) that calculates the starting x position and calls drawRowOfBricks(). Change run() to test it.
    4. Now you're ready to write a drawPyramid(int baseBricks) method, with a simple for() loop that calls drawCenteredRowOfBricks(). Hint: it will be easier if you start by drawing the bottom row first. (Think i-- instead of i++)


    If you're like most beginners, you probably got it working, but have a ton of code just in your run() method. This exercise is all about decomposing your code into simple methods. It's a good habit to learn. Give it a try, and post what you come up with here. Remember to use CODE tags when you do (the # icon in the forum editor toolbar).

    -Gary-

  4. #4
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Previously what I was doing is just having all the code in the run method without using other methods, which I now know is wrong.

    Now that I'm trying to solve the problem using methods I'm still having some trouble as I guess I need practice.

    Right, so I'm going to do it in the steps as you have suggested.

    This is my attempt at step 1; I've done something wrong though as the program crashes, not sure what.

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		drawBrick (10.0,  5.0);
    		
    	}
    	
    	public double drawBrick (double x, double y) {
    		
    		GRect Brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(Brick);
    		return (drawBrick(x, y));
    	
    		
    	}
    
    	
    }
    Last edited by Learning Java; 05-09-2010 at 08:54 PM.

  5. #5
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I think I may know what my mistake was.

    The drawBrick method should be void rather than double, as it shouldn't return anything. Instead it should add the Brick to the program itself. Is step 1 now correct?

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		drawBrick (10,  5);
    		
    	}
    	
    	public void drawBrick (double x, double y) {
    		
    		GRect Brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(Brick);
    		
    	
    		
    	}
    
    	
    }
    Last edited by Learning Java; 05-09-2010 at 08:56 PM.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    All your methods for this exercise will be void -- they do not need to return a value.

    -Gary-

  7. #7
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Write a drawRowOfBricks(double x, double y, int numBricks) method that calls your drawBrick() method to draw a row of bricks. Change your run() method to call drawRowOfBricks(10, 5, 6) to test it.
    This is going to need a for loop right? And in order to draw one row I'm going to have to have a counter for the x axis right? And leave the y axis alone for now?

  8. #8
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    This is where I'm at so far... not sure if it's correct, though.

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		//drawBrick (10, 5);
    		drawRowOfBricks(10, 5, 6);
    		
    	}
    	
    	public void drawBrick (double x, double y) {
    		
    		GRect Brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(Brick);
    		
    	}
    
    	public void drawRowOfBricks (double x, double y, int numBricks) {
    		
    		for (int i = numBricks; i > 0; i--) {
    		
    		x = BRICK_WIDTH * i;
    	//	y =
    		
    		drawBrick(x, y);
    		
    		}
    			
    			
    		}
    		
    	}

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Keep it simpler than that. You don't need to do any math with your loop index variable i. Just increment your x variable by the proper amount each time through the loop.

    -Gary-

  10. #10
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    This is where I get a little lost. Like this?

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		//drawBrick (10, 5);
    		drawRowOfBricks(10, 5, 6);
    		
    	}
    	
    	public void drawBrick (double x, double y) {
    		
    		GRect Brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		add(Brick);
    		
    	}
    
    	public void drawRowOfBricks (double x, double y, int numBricks) {
    		
    		for (int i = numBricks; i > 0; i--) {
    		
    		x += BRICK_WIDTH;
    	//	y =
    		
    		drawBrick(x, y);
    		
    		}
    			
    			
    		}
    		
    	}

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    That's basically right, but you want to draw the brick, then increment x.

    Also, what you have is fine, but I just want to point out that it would also be OK to do a "normal" for() loop here -- starting from zero and incrementing up to numBricks. Counting down will be more important when you get to your drawPyramid() method. In this method, we are only concerned with looping a certain number of times, so it doesn't matter whether we count up or count down.

    -Gary-

  12. #12
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I'm good to go now for the 3rd step right?

    I'll continue tomorrow morning. Thanks for all your help!!!

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    I'm good to go now for the 3rd step right?

    I'll continue tomorrow morning. Thanks for all your help!!!
    Assuming you fixed the sequencing, yes you're good to go. The next method has the most complicated math, but it's really not that bad -- you're just trying to find x for the first brick, based on the numBricks parameter you get, and the width of the screen.

    -Gary-

  14. #14
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Good morning!

    How does this look?

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		//drawBrick (10, 5);
    		//drawRowOfBricks(10, 5, 6);
    		drawCenteredRowOfBricks(5, 6);
    		
    	}
    	
    	public void drawBrick (double x, double y) {
    		
    		GRect Brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		
    		add(Brick);
    		
    	}
    
    	public void drawRowOfBricks (double x, double y, int numBricks) {
    		
    		for (int i = 0; i < numBricks; i++) {
    		
    		drawBrick(x, y);
    		
    		x += BRICK_WIDTH;
    		
    		}
    			
    		}
    	
    	public void drawCenteredRowOfBricks(double y, int numBricks) {
    		
    		int x = (getWidth() / 2) - (numBricks * BRICK_WIDTH) / 2;
    		
    		
    		drawRowOfBricks (x,y,numBricks);
    		
    		
    	}
    	
    
    		
    	}
    Last edited by Learning Java; 05-10-2010 at 11:16 AM.

  15. #15
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I'm not sure how exactly but I seem to have done it. Does it look ok to you?

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		drawPyramid (BRICKS_IN_BASE);
    
    	}
    	
    	public void drawBrick (double x, double y) {
    		
    		GRect Brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		
    		add(Brick);
    		
    	}
    
    	public void drawRowOfBricks (double x, double y, int numBricks) {
    		
    		for (int i = 0; i < numBricks; i++) {
    		
    		drawBrick(x, y);
    		
    		x += BRICK_WIDTH;
    		
    		}
    			
    		}
    	
    	public void drawCenteredRowOfBricks(double y, int numBricks) {
    		
    		int x = (getWidth() / 2) - (numBricks * BRICK_WIDTH) / 2;
    		
    		drawRowOfBricks (x, y, numBricks);
    		
    	}
    	
    	public void drawPyramid (int baseBricks) {
    		
    		int y = (getHeight() - BRICK_HEIGHT);
    			
    		for (int i = BRICKS_IN_BASE; i > 0; i--) {
    			
    		drawCenteredRowOfBricks(y, baseBricks);
    			
    		y -= BRICK_HEIGHT;
    			
    		baseBricks -= 1;
    			
    		}
    			
    	}
    		
    }

  16. #16
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    Good morning!

    How does this look?
    Looks pretty good to me, but more importantly, how does it look to you? Are your tests running as expected?

    A couple of style issues -- better to learn good habits while you're starting out. Call your variable brick instead of Brick. Brick is perfect for a class name, but brick or myBrick for a variable. For methods, keep your parentheses snug up against the method name, in order to differentiate it from a statement such as for or while.

    Java Code:
            public void myMethod(String someParameter, int anotherParameter) {
                    for (int i = 0; i < someParameter.length(); i++) {
                            int x = someParameter.someMethod(anotherParameter, "Hello");
                    }
            }
    Good style makes your code more readable, which is very important, especially when you're learning. You've got some indentation issues here, but I'll assume that's partly because of the forum software. Still, you should make the effort to keep it looking clean, as it's basically the on-line equivalent of good grooming. Be consistent in your use of spaces, blank lines, placement of {}s, etc. Mostly your style is very good. Aim for perfect. :)

    Now you just have your drawPyramid() method. Think it through first, and you'll find it's very easy. Where do you want to start? What do you need to do between rows? Should you draw the row first or change your y variable first?

    -Gary-

  17. #17
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Not bad, but take another look at drawPyramid(). It does what you need it to do, but I think you can make the code just a bit cleaner and simpler. Think about BRICKS_IN_BASE and baseBricks, where they are coming from, and whether you should be using both in your method. (Think about your i variable too.) Think about whether you could start out with the simpler
    Java Code:
                    int y = getHeight();
    and what you would have to change to make that work. (It's pretty simple, and I hinted at it in my last post.)

    -Gary-

  18. #18
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    And then just as a summary, did this exercise of splitting things up into methods make the whole process more understandable? Is this version of your code more readable than what you had before you made your first post?

    -Gary-

  19. #19
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    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.program.*;
    import acm.graphics.*;
    
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
    	private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
    	private static final int BRICKS_IN_BASE = 14;  //The number of bricks in the base (14)
    	 
    	
    	public void run() {
    		
    		drawPyramid(BRICKS_IN_BASE);
    		
    	}
    	
    	public void drawBrick(double x, double y) {
    		
    		GRect brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
    		
    		add(brick);
    		
    	}
    
    	public void drawRowOfBricks(double x, double y, int numBricks) {
    		
    		for (int i = 0; i < numBricks; i++) {
    		
    		drawBrick(x, y);
    		
    		x += BRICK_WIDTH;
    		
    		}
    			
    	}
    	
    	public void drawCenteredRowOfBricks(double y, int numBricks) {
    		
    		int x = (getWidth() / 2) - (numBricks * BRICK_WIDTH) / 2;
    		
    		drawRowOfBricks(x, y, numBricks);
    		
    	}
    	
    	public void drawPyramid(int baseBricks) {
    		
    		int y = getHeight();
    			
    		for (int i = baseBricks; i > 0; i--) {
    		
    		y -= BRICK_HEIGHT;
    		
    		baseBricks -= 1;
    		
    		drawCenteredRowOfBricks(y, i);
    			
    		}
    			
    	}
    		
    }
    Last edited by Learning Java; 05-10-2010 at 12:46 PM.

  20. #20
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    And then just as a summary, did this exercise of splitting things up into methods make the whole process more understandable? Is this version of your code more readable than what you had before you made your first post?
    Although I do feel good about having finally complete the task, I do doubt whether I would have been able to do so without your guidance. Even now looking at the code I find it hard to follow what's going on. You broke it up into 4 different methods in advance; I wouldn't have been able to think that far ahead and work that out.

    I just don't feel as if my mind is thinking about solving tasks the way it should be - does that make sense? I think my approach is wrong.

Page 1 of 6 123 ... LastLast

Similar Threads

  1. Help for University Project
    By ja107 in forum Networking
    Replies: 3
    Last Post: 03-18-2010, 10:22 AM
  2. Help for University Project
    By ja107 in forum Networking
    Replies: 1
    Last Post: 03-04-2010, 12:22 AM
  3. University Of Nottingham Website Survey
    By MuslimCoder in forum Reviews / Advertising
    Replies: 1
    Last Post: 03-02-2009, 10:42 AM

Posting Permissions

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