The code consists of 3 classes - a Ball class, a Simulation class and the New_Box class. The latter is responsible for initializing the applet, creating instances of the other classes and painting the screen. The Ball class encapsulates all the properties of a ball - its size and color. It also possesses a method draw() that is called by paint() which draws it to the screen. The Simulation class runs the simulation. Its class extends Runnable. This means that a separate Thread of execution is attached to this class which can be stopped and started in response to button clicks.
var[4]=app.ball2.x; var[5]=app.ball2.vx; .....etcAlso, you need to put code in to handle the collisions with the walls and to update the values of the position and speed with their new values given in the array new_var[]). Just mimic everything that is done to ball1 with similar code for ball2.
dy[4]=y[5]; dy[5]=0.0; dy[6]=y[7]; dy[7]=0.0;
private void f(double x, double y[], double dy[]){
double dum,d1,d2,MASS1,MASS2,POWER=7.0,scale;
MASS1=1.0;
MASS2=4.0;
dum=Math.sqrt((y[0]-y[4])*(y[0]-y[4])+(y[2]-y[6])*(y[2]-y[6]));
scale=0.2;
dum=dum/scale;
if(dum<0.33) dum=0.33;
d1=Math.pow(dum,POWER)*scale;
dy[0]=y[1];
dy[1]=(1.0/MASS1)*(y[0]-y[4])*(1/d1);
dy[2]=y[3];
dy[3]=(1.0/MASS1)*(y[2]-y[6])*(1/d1);
dy[4]=y[5];
dy[5]=(1.0/MASS2)*(y[4]-y[0])*(1/d1);
dy[6]=y[7];
dy[7]=(1.0/MASS2)*(y[6]-y[2])*(1/d1);
return;
}
There is now a force of repulsion between the two balls that falls
off rapidly with
distance between them. Recompile and reload. You should now see
the motion of either of the two balls is chaotic. Notice that on
average the small ball is moving faster than the larger one - this
ensures that the mean kinetic energy of the two
balls is about the same - as required by the kinetic theory
of gases !