package examples;
import javax.swing.*;
class IO {
public static float getVal(Object o) {
return Float.parseFloat(JOptionPane.showInputDialog(o));
}
public static void sysout(Object o) {
System.out.print(o);
}
}
public class Bounce {
int S1, pos = 0;
float S2, L, V, C;
float T[] = new float[20];
public Bounce(float _V, float _S2, float _C) {
IO.sysout("\nV = " + (V = _V));
IO.sysout("\nS2 = " + (S2 = _S2));
IO.sysout("\nC = " + (C = _C));
IO.sysout("\nS1=" + (S1 = (int) (70 / (V / (16 * S2)))) + "\n");
}
public void fillTime() {
for (int i = 1; i <= S1; i++) {
T[i] = (float) (V * Math.pow(C, (i - 1)) / 16);
}
}
public float getHeight() {
return (int) (-16 * ((V / 32) * (V / 32)) + (V * V) / 32 + 0.5);
}
public float getXCoor(int i, float j, float H) {
return (float) Math.abs((H - (0.5 * (-32) * (j * j) + V * Math.pow(C, (i - 1)) * j)));
}
public void plotHeight() {
float t, height = getHeight();
for (float H = height; H >= 0; H -= 0.5) {
if (H == (int) H) {
IO.sysout("" + (int) H);
}
L = 0;
for (int i = 1; i <= S1; i++) {
plotVal(i, H);
t = T[i + 1] / 2;
if (((-16) * (t * t) + V * Math.pow(C, (i - 1)) * t) < H) {
pos = 0;
IO.sysout("\n");
break;
}
}
}
}
public void plotVal(int i, float H) {
for (float j = 0; j <= T[i]; j += S2) {
L += S2;
float temp = getXCoor(i, j, H);
if (temp <= .25) {
IO.sysout(padString((int) (L / S2)) + "0");
}
}
}
public void printAxis() {
IO.sysout("\n");
for (int i = 1; i <= (int) (L + 1) / S1 + 1; i++) {
IO.sysout(". ");
}
}
public void labelAxis() {
pos = 0;
IO.sysout("\n0");
for (int i = 1; i <= (int) (L + .9995); i++) {
IO.sysout(padString((int) (i / S2)) + i);
}
IO.sysout("\n" + padString((int) ((L + 1) / (2 * S2) - 2) + pos) + "Seconds");
}
public String padString(int l) {
String s = "";
for (int i = 0; i <= (l - pos); i++) {
s += " ";
}
pos += s.length();
return s;
}
public void run() {
IO.sysout("\n");
this.fillTime();
this.plotHeight();
this.printAxis();
this.labelAxis();
}
public void debug(Object o) {
IO.sysout(o);
}
public static void main(String[] args) {
Bounce b = new Bounce(IO.getVal("Velocity (FPS): "),
IO.getVal("Time Increment (Sec): "),
IO.getVal("Coefficient: "));
b.run();
}
}