Search code examples
javaphysicsparticles

Trying to make a simple falling ball program


This is what I have so far. It tells me that I have a syntax error on the lines marked below, saying that it expected a "{" and a "}" respectively, without the double quotes around them. Any suggestions?

public class attempt1 {
//use Euler-Richardson algorithm

//defining the initial conditions
double v0=30;
double theta=40;
double x0=0;
double y0=0;

double v0x=v0*Math.cos(Math.toRadians(theta));
double v0y=v0*Math.sin(Math.toRadians(theta));
double dt= .01;
double ax=0;
double ay=-9.8;

double x=0;
double y=0; //this line here, on the semi-colon
while (y>=0) {
    double vx= v0x+1/2*ax*dt;
    double vy= v0y+1/2*ay*dt;   

    double x= x0+1/2*vx*dt;
    double y= y0+1/2*vy*dt;

        if (y<=0){System.out.println(x);}
}
} //and right over here, on the brace itself

Solution

  • You are trying to run statements inside a class body. They should be in a method. like this:

    public class Attempt1{
        private void doSomething(){
            //example code
            int a = 1 + 1;
            while (a < 2) {
                //do random stuff
            }
        }
    }
    

    Right now you have something similar to this:

    public class Attempt1 {
        //while{...}    <---- WRONG!
    }
    

    Statements should be in a method (sometimes called function) at all times. You can however put variables outside a method. This will make them accessible for all methods in that class.

    You should check out some starting tutorials like the one Oracle gives: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/