Getting started with Java acm

389 views Asked by At

Just getting started here. I've had programming classes before, but am new to Java and have no substantial experience. This program is from Mehran Sahami's Stanford lecture posted to Youtube. https://www.youtube.com/watch?v=YpZCKVG4s5k&t=1996s The code is visible starting around 32 minutes. It is a simple graphics program that shows a bouncing ball. A good place for me to start experimenting with settings, replacing one object with another and generally getting used to how the syntax relates to what appears on the screen. But, I can't even get to the metaphorical starting gate! I tried cutting and pasting into the Sololearn emulator but get the same errors. I think it has to be something with the acm libraries, but . . . what?

Code is below and error messages below that.

import acm.program.*;
import acm.graphics.*;


public class BouncingBall extends GraphicsProgram {
private static final int DIAM_BALL = 30;
private static final double GRAVITY = 3;
private static final int DELAY = 50;
private static final double X_START = DIAM_BALL / 2;
private static final double Y_START = 100;

//x velocity
private static final double X_VEL = 5;
//Y velocity determined by gravity and bounce
private static final double BOUNCE_REDUCE = 0.9;
//Starting coords
private double xVel = X_VEL;
private double yVel = 0.0;
//private instance variable
private GOval ball;
}
public void run(){
    setup(){
       while (ball.getX() < getWidth()) {
            moveBall();
            checkForCollision();
            pause(DELAY);
        }
        }
        //create and place ball
    private void setup(){
        ball=new GOval(X_START,Y_START,DIAM_BALL,DIAM_BALL);
        ball.setFilled(true);
        add(ball);
    }
    //update and move ball
private void moveBall(){
        yVel+=GRAVITY;
        ball.move(xVel,yVel);
}
//Collision detection
private void checkForCollision(){
    if(ball.getY()>getHeight()-DIAM_BALL){
        yVel=-yVel*BOUNCE_REDUCE;
        double diff=ball.getY()-(getHeight()-DIAM_BALL);
        ball.move(0,-2*diff);
    }
}
}
}

"Error: Java: class, interface, or enum expected" There are about a dozen of these, specifying (22,12), (26,17), (27,17), (28,13), (33,13), (34,13) . . .

I have a feeling that when I understand why some of these are a problem, I'll be able to fix all of them.

Thanks in advance!

2

There are 2 answers

3
Berkley Lamb On

I removed setup(){ and a } at the end of the file, i believe that was the cause of the issue.

import acm.program.*;
import acm.graphics.*;


public class BouncingBall extends GraphicsProgram {
private static final int DIAM_BALL = 30;
private static final double GRAVITY = 3;
private static final int DELAY = 50;
private static final double X_START = DIAM_BALL / 2;
private static final double Y_START = 100;

//x velocity
private static final double X_VEL = 5;
//Y velocity determined by gravity and bounce
private static final double BOUNCE_REDUCE = 0.9;
//Starting coords
private double xVel = X_VEL;
private double yVel = 0.0;
//private instance variable
private GOval ball;


public void run(){
    setup();
    while (ball.getX() < getWidth()) {
            moveBall();
            checkForCollision();
            pause(DELAY);
    }
 }
 //create and place ball
private void setup(){
        ball=new GOval(X_START,Y_START,DIAM_BALL,DIAM_BALL);
        ball.setFilled(true);
        add(ball);
    }
    //update and move ball
private void moveBall(){
        yVel+=GRAVITY;
        ball.move(xVel,yVel);
}
//Collision detection
private void checkForCollision(){
    if(ball.getY()>getHeight()-DIAM_BALL){
        yVel=-yVel*BOUNCE_REDUCE;
        double diff=ball.getY()-(getHeight()-DIAM_BALL);
        ball.move(0,-2*diff);
    }
}
}

Check out the video on 35:03, it shows exactly what is above.

0
Erik R. On

@Saint Razz: Firstly, you use a non-public library 'acm.jar' and the sololearn emulator
don't know this library.
Secondly, try to avoid using 'static'. Except you write a class and it has methods, variables, constants that may exist only once.
e.g.

class Student() { 
     private String mName;
     private String mCollege;
     public Student(String name) { 
         mName = name;
         college = "ITS";
     }
}


In that case it would make sense to use for memory issues a static for String college.

Next there is a mistake that Berkley Lamb as well corrected. It makes no sense to declare a method in a method. I mean your setup() method in the run() method.
Take a careful look on your curly brackets. A compiler will always throw an error if you try to call a method outside of a class in java. (see close curly bracket after private GOval ball - declaration.

And one last thing I recommend you to use either a constant for window width and height or to initialize the window in a public void init() {} method before you call the run method, so long you use the acm.jar. Otherwise it can happen that you call getWidth() and get the value 0. That cause sometimes some unwanted errors.

I hope it helps you further.