0

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!

Saint Razz
  • 53
  • 1
  • 7
  • Possible duplicate of [Java ACM package](https://stackoverflow.com/questions/3796879/java-acm-package) – Berkley Lamb Aug 03 '17 at 17:18
  • I got the acm from the above link. I have tried moving the acm.jar to various places in my directories, but none seem to have any effect. Same errors no matter where I place it. Also, the type of the file reads "executable Jar File", which is different from how it has been described by some commenters. – Saint Razz Aug 03 '17 at 18:33
  • Should the line in run have setup(){ ? or setup(); – Berkley Lamb Aug 03 '17 at 18:41
  • I don't think so. I transferred the code as it is displayed here. setup() is on the next line of code and nested within the run function's brackets. – Saint Razz Aug 03 '17 at 18:49

2 Answers2

0

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.

Berkley Lamb
  • 283
  • 2
  • 12
  • Hmm, didn't compile, but only one error . . . Hell of an improvement. Let's see if I can fix that remaining issue. Thanks! – Saint Razz Aug 03 '17 at 18:58
  • Well, crap. "Error:(5,8) java: class BouncingBall is public, should be declared in a file named BouncingBall.java." Isn't this code starting at the referenced line the definition of the BouncingBall class? Am I missing something? – Saint Razz Aug 03 '17 at 19:01
  • Ok, so now it seems that having separate files for each class is pretty standard. I haven't encountered that yet and didn't see it in the classes/videos I've been exposed to. Did you define a BouncingBall class file distinct from the code block you posted above? – Saint Razz Aug 03 '17 at 19:17
0

@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.

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Erik R.
  • 1
  • 1