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!