This program models a ball bouncing around a room.

#### Sample input:

``````What do you want your box's side length to be?
100
10
What do you want your velocity to be? (units are: units per second)
3
What do you want your starting horizontal position to be?
50
What do you want your starting vertical position to be?
50
Now, most importantly, what do you want your starting angle to be?
17
How many collisions do you want to occur before ending the simulation?
5
```
```

#### Output:

```
```Wall I
163
Wall III
17
Wall II
343
Wall I
197
Wall III
343
```
```

#### bouncingBall.java

```
```import java.awt.Rectangle;

public class bouncingBall {
//Try adding vectors on the ball's path
public double starty = 400;
public double startx = 200;
public double startXVel = 50;
public double startYVel = -20;
public double xvelocity;
public double yvelocity;
public double xposition;
public double yposition;
public double bounceCoef= .01;
public int angleOfTravel= 30;
public double currentTime = 0;
public double speedModulator=.1;
public int ydirection=1;
public int xdirection=1;
public boolean firstRun = false;
public int yincrementer=0;
public int xincrementer=0;
public int abound = 20;
public int bbound = 20;
public int cbound = 730;
public int dbound = 730;
public int g = 3;
public double currentxbouncecoeff=1;
public double currentybouncecoeff=1;

public void xymove(){
if (!this.firstRun){
this.xposition=this.startx;
this.yposition=this.starty;
this.xvelocity=this.startXVel;
this.yvelocity=this.startYVel;
this.firstRun=true;
}

this.yvelocity=currentybouncecoeff*(this.ydirection*(this.yvelocity)+this.g);
this.xvelocity=currentxbouncecoeff*(this.xdirection*this.xvelocity);

if (this.ydirection==-1){
this.ydirection=1;
}
if (this.xdirection==-1){
this.xdirection=1;
}

this.xposition=this.xposition+(this.xvelocity);
this.yposition=this.yposition+(this.yvelocity);
yincrementer--;
xincrementer--;

if((this.yposition>=dbound||this.yposition<=bbound)&&yincrementer<=0){
this.ydirection=(this.ydirection*-1);
currentybouncecoeff-=bounceCoef;
yincrementer=1;

}

if((this.xposition>=cbound||this.xposition<=abound)&&xincrementer<=0){
this.xdirection=(this.xdirection*-1);
currentxbouncecoeff-=bounceCoef;
xincrementer=5;//this incrementer is not great enough for when the ball has a very small vertical velocity

}

}

}

```
```

#### drawingBall.java

```
```import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Scanner;

/**
* Example1Applet
*
* This is a template applet for animation.
* It shows how to write the basic applet so
* that it draws one frame of animation
* at intervals defined by a frames per second (fps)
* parameter to the applet.
*
* @author Arthur van Hoff
*/
public
class drawingBall extends java.applet.Applet implements Runnable {
int frame;
int delay;
int increment=1;
double endpoints[][] = new double[2][100000];
int angles[]= new int [10000];
int numberoflines=0;

/**
* Initialize the applet and compute the delay between frames.
*/
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 10;
delay = (fps > 0) ? (1000 / fps) : 100;
}

/**
* This method is called when the applet becomes visible on
* the screen. Create a thread and start it.
*/
public void start() {
animator.start();
}

/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run() {

// Display the next frame of animation.
increment++;
repaint();

// Delay for a while
try {
} catch (InterruptedException e) {
break;
}

frame++;
}
}

/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop() {
animator = null;
}

/**
* Paint a frame of animation.
*/
theBall hey = new theBall();

boolean collision=false;
boolean drawn=false;
int constantincrementor=0;
public void paint(Graphics g) {
// try {
// } catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
hey.setA(500);
hey.setR(10);
hey.setV(1);

if (!drawn) {
hey.collision1 = true;
endpoints[0][numberoflines] = hey.getX();
endpoints[1][numberoflines] = hey.getY();
drawn = true;
this.setSize(hey.a + 2*hey.r, hey.a + 2*hey.r);

}

g.drawLine(hey.a+4*hey.r, (int)(.5*hey.a),(hey.a+hey.r)+360,(int)(.5*hey.a));
g.drawRect(hey.r, hey.r, hey.a, hey.a);//I just realized that my points are the center of the circle
for (int i = 0; i < numberoflines; i++) {//perhaps you can make a graph of all the thetas beside the ball
g.drawLine((int)endpoints[0][i],
(int)endpoints[1][i], (int)endpoints[0][i+1],
(int)endpoints[1][i+1]);

}
for (int i = 0; i < numberoflines; i++) {//perhaps you can make a graph of all the thetas beside the ball
g.drawOval((hey.a+4*hey.r)+angles[i], (int) (.5*hey.a)+i, 2, 2);
g.drawString(Integer.toString(angles[i]), (hey.a+4*hey.r)+angles[i], (int) (.5*hey.a)-10);

}

collision = hey.xymove(increment);
if (collision) {
increment = 1;
numberoflines++;
endpoints[0][numberoflines] = hey.getX();
endpoints[1][numberoflines] = hey.getY();
collision = false;
angles[constantincrementor]=hey.theta;
constantincrementor++;
}

g.fillOval((int) hey.getX(), (int) hey.getY(), 2*hey.r, 2*hey.r);

}

}
```
```

#### modelingBoucingBallMovement.java

```
```import java.util.Scanner;
import java.math.*;

public class modelingBoucingBallMovement {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int a; // Side of box length
double t=0; // Time elapsed
int v; // Velocity of ball (in distance per 1 second)
int startx; //Starting horizontal position
int starty; //Starting vertical position
int theta; //Starting angle and all other subsequent angles
int iterations; //Number of times the user wishes the ball to collide with the wall
double x; //Current x position of the center of the circle
double y; //Current y position of the center of the circle
//Below this line are additional declarations needed for calculations
int aa;
int normal=0;
boolean corner = false;
boolean equaltonormal = false;

System.out.println("What do you want your box's side length to be?");
a=scan.nextInt();
r=scan.nextInt();
System.out.println("What do you want your velocity to be? (units are: units per second)");
v=scan.nextInt();
System.out.println("What do you want your starting horizontal position to be?");
startx=scan.nextInt();
System.out.println("What do you want your starting vertical position to be?");
starty=scan.nextInt();
System.out.println("Now, most importantly, what do you want your starting angle to be?");
theta=scan.nextInt();
System.out.println("How many collisions do you want to occur before ending the simulation?");
iterations=scan.nextInt();
x=startx; //This should not be in the loop
y=starty; //This should not be in the loop
while(startx0){
//Determine if the angle is 0,90,180, or 270.
equaltonormal=true;
normal=270;

}
equaltonormal=true;
normal=90;
}
}

equaltonormal=true;
normal=180;
}
equaltonormal=true;
normal=360;
}
}

//Determine if the angle hits a corner, if it does, the resulting angle is easily calculated.
if(!equaltonormal){
corner=true;
normal=135;
}}
double firstOperand = Math.abs(((a-x)-r)/ (v * Math.cos(Math.toRadians(theta)) ));
double secondOperand = Math.abs(((a-y)-r) / (v * Math.sin(Math.toRadians(theta))));
if (Math.abs(firstOperand - secondOperand) < .001) {
//				if((Math.abs(((a-x)-r)/ (v * Math.cos(Math.toRadians(theta)) )) - Math.abs(((a-y)-r) / (v * Math.sin(Math.toRadians(theta)))))<.001){
corner=true;
normal=225;
}}}

corner=true;
normal=45;
}}
corner=true;
normal=315;
}}}

//If none of the above conditions are satisfied, then the last ifs are called. Although, another step is needed to determine the angle.
if(!corner){
System.out.println("Wall II");
normal=270;
}
System.out.println("Wall I");
normal=180;
}}
System.out.println("Wall I");
normal=180;
}
System.out.println("Wall IV");
normal=90;
}
}

}
System.out.println("Wall II");
normal=270;
}
System.out.println("Wall III");
normal=360;
}
}
System.out.println("Wall IV");
normal=90;
}
}
}
}

}
while(theta>360){
theta-=360;}

theta=(180+(2*normal)-theta);
while(theta>360){
theta-=360;}
System.out.println(theta);
iterations--;
}
}
}
```
```

#### theBall.java

```
```import java.awt.Graphics;
import java.awt.Rectangle;

public class theBall {
public double x=450; //Current x position of the center of the circle
public double y=250;
public int a; // Side of box length
public int r; // Circle's radius
public double t=0; // Time elapsed
public int v; // Velocity of ball (in distance per 1 second)
public int startx; //Starting horizontal position
public int starty; //Starting vertical position
public double x1;
public double y1;
public int theta=30; //Starting angle and all other subsequent angles
public int iterations; //Number of times the user wishes the ball to collide with the wall
public boolean collision1;
public int normal;
public void paint(Graphics g) {
g.drawRect(0, 0, a, a);

}
//Below this line are additional declarations needed for calculations

public boolean xymove(int increment){//.001*frame
boolean corner = false;
boolean equaltonormal = false;
double test=0;
int theta=this.getTheta();

if(collision1){
x1=this.getX();
y1=this.getY();
equaltonormal=true;
test=(a-y)-r;
normal=270;

}
equaltonormal=true;
test=(y-r);
normal=90;
}
}

equaltonormal=true;
test=(a-x)-r;
normal=180;
}
equaltonormal=true;
test=(x-r);
normal=360;
}
}

//Determine if the angle hits a corner, if it does, the resulting angle is easily calculated.
if(!equaltonormal){
corner=true;
test=(y-r);
normal=135;
}}
double firstOperand = Math.abs(((a-x)-r)/ (v * Math.cos(Math.toRadians(theta)) ));
double secondOperand = Math.abs(((a-y)-r) / (v * Math.sin(Math.toRadians(theta))));
if (Math.abs(firstOperand - secondOperand) < .001) {
//				if((Math.abs(((a-x)-r)/ (v * Math.cos(Math.toRadians(theta)) )) - Math.abs(((a-y)-r) / (v * Math.sin(Math.toRadians(theta)))))<.001){
corner=true;
test=(a-y)-r;
normal=225;
}}}

corner=true;
test=(y-r);
normal=45;
}}
corner=true;
test=(a-y)-r;
normal=315;
}}}

//If none of the above conditions are satisfied, then the last ifs are called. Although, another step is needed to determine the angle.
if(!corner){

normal=270;
test=((a-y)-r);
}

normal=180;
test=((a-x)-r);
}}

normal=180;
test=(((a-x)-r));
}

normal=90;
test=((y-r));
}
}

}

normal=270;
test=(((a-y)-r));
}

normal=360;
test=((x-r));
}
}

normal=90;
test=((y-r));
}
}
}
}

}
this.collision1=false;
}
while(theta>360){
theta-=360;}

double speed =.5;
this.setX(x1+((increment*speed*(t*(v*Math.cos(Math.toRadians(theta)))))));//t should not change until theta changes
System.out.println(this.getX()+" , "+this.getY());

if(increment*speed==1){

switch(normal){
case 270: this.setY(this.getY()); break;//put it back the amount it went over, we can easily put the one that
case 90: this.setY(this.getY()); break;//(the x or y that) hits the wall back to either 0 or a-r,
case 180: this.setX(this.getX()); break;//but we have to subtract the overature from the other coord
case 360: this.setX(this.getX()); break;
}

System.out.println(theta);

theta=(180+(2*normal)-theta);
System.out.println(theta);
while(theta>360){
theta-=360;}
this.setTheta(theta);
this.collision1=true;

return true;
}
return false;

}

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

public int getR() {
return r;
}

public void setR(int r) {
this.r = r;
}

public double getT() {
return t;
}

public void setT(double t) {
this.t = t;
}

public int getV() {
return v;
}

public void setV(int v) {
this.v = v;
}

public int getStartx() {
return startx;
}

public void setStartx(int startx) {
this.startx = startx;
}

public int getStarty() {
return starty;
}

public void setStarty(int starty) {
this.starty = starty;
}

public int getTheta() {
return theta;
}

public void setTheta(int theta) {
this.theta = theta;
}
}
```
```

#### theBallBouncing.java

```
```	import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Scanner;

/**
* Example1Applet
*
* This is a template applet for animation.
* It shows how to write the basic applet so
* that it draws one frame of animation
* at intervals defined by a frames per second (fps)
* parameter to the applet.
*
* @author Arthur van Hoff
*/

public class  theBallBouncing extends java.applet.Applet implements Runnable {
int frame;
int delay;
int increment=1;
int numberOfPoints=0;
int xycoords[][]= new int [2][100000];
double endpoints[][] = new double[2][100000];
int angles[]= new int [10000];

/**
* Initialize the applet and compute the delay between frames.
*/
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 50;
delay = (fps > 0) ? (1000 / fps) : 100;
}

/**
* This method is called when the applet becomes visible on
* the screen. Create a thread and start it.
*/
public void start() {
animator.start();
}

/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run() {

// Display the next frame of animation.
increment++;
repaint();

// Delay for a while
try {
} catch (InterruptedException e) {
break;
}

frame++;
}
}

/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop() {
animator = null;
}

/**
* Paint a frame of animation.
*/
bouncingBall hey = new bouncingBall();

boolean collision=false;
boolean drawn=false;
int constantincrementor=0;
public void paint(Graphics g) {
// try {
// } catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// }
//hey.setA(500);
//hey.setR(10);
//hey.setV(2);

if (!drawn) {
//hey.collision1 = true;
drawn = true;
this.setSize(1000, 1000);

}

g.drawRect(hey.abound, hey.bbound, hey.cbound, hey.dbound);//I just realized that my points are the center of the circle

hey.currentTime=this.increment;

hey.xymove();

xycoords[0][numberOfPoints]=(int) hey.xposition;
xycoords[1][numberOfPoints]=(int) hey.yposition;

numberOfPoints++;

if(numberOfPoints>1){
for(int i=0; i<numberOfPoints-1;i++){

g.drawLine(xycoords[0][i], xycoords[1][i], xycoords[0][i+1], xycoords[1][i+1]);

}

}

//collision = hey.xymove(increment);
if (collision) {
increment = 1;
collision = false;
//angles[constantincrementor]=hey.theta;
constantincrementor++;
}

g.fillOval((int) hey.xposition, (int) hey.yposition, 20, 20);

}

}
```
```