Sunday, December 30, 2012

Programming: Java I

I started this class on iTunes U recently called CS106A (Programming Methodology) from Sanford University. It is taught by Mehran Sahami. He is really great! He keeps the information really interesting in his lectures and the book, "The Art and Science of Java" was written by a professor at Stanford and is perfectly tailored to the class.

Today, in Chapter 4 of this book, I spent a few hours doing the programming exercises, and I got pretty excited when I made my first animation with Java so I took a short video of it in action with the source code behind it. I know, it's easy and cheap, but ehhh. It's pretty fun anyhow. The source code is at the bottom if you're interested.

The class can be found here:
http://www.stanford.edu/class/cs106a/
...or on iTunesU.


/*
 * File: BouncingBallWalls.java
 * ---------------------
 * This is exercise 15 in chapter 4 of "The Art and Science of Java."
 * It requires me to write a program that makes an animated bouncing
 * ball on the screen, bouncing off the window edges.
 */

import java.awt.*;
import acm.program.*;
import acm.graphics.*;

public class BouncingBallWalls extends GraphicsProgram {
    public void run() {
        GOval ball = new GOval (getWidth()/2 - BALL_SIZE/2, getHeight()/2 - BALL_SIZE, BALL_SIZE, BALL_SIZE);               /* Centers the ball */
        ball.setFilled(true);
        ball.setFillColor(Color.BLUE);
        ball.setColor(Color.BLUE);
        add(ball);
        double dx = (getWidth()/N_STEPS);
        double dy = (getWidth()/N_STEPS);
        while(true) {
            ball.move(dx, dy);                                   /* Movement for the ball */
            pause(PAUSE_TIME);
            if (ball.getY() > getHeight() - BALL_SIZE) {         /* Each "if" statement reverses the direction of the ball on one */
                dy = dy * -1;                                                         /* axis if it hits a boundry */
            }
            if(ball.getX() > getWidth()- BALL_SIZE) {
                dx = dx * -1;
            }
            if(ball.getY() < 0) {
                dy = dy * -1;
            }
            if(ball.getX() < 0) {
                dx = dx * -1;
            }
        }
    }
    private static final double N_STEPS = 1000;
    private static final double PAUSE_TIME = 2;
    private static final double BALL_SIZE = 50.0;
}

1 comment:

  1. thanks for your contribution to the programming community,
    this helped me a lot in what i'm doing

    ReplyDelete