/Users/lyon/j4p/src/bookExamples/ch08ArraysAndVectors/array/Bounce.java
|
1 /*
2 * bounce.java
3 *
4 * Created on December 12, 2002, 6:57 PM
5 * @author greg stewart
6 * reproduction prohibited without authorisation
7 */
8
9 package bookExamples.ch08ArraysAndVectors.array;
10
11 import javax.swing.*;
12
13 class IO {
14 public static float getVal(Object o) {
15 return Float.parseFloat(JOptionPane.showInputDialog(o));
16 } /*end getVal()*/
17
18 public static void sysout(Object o) {
19 System.out.print(o);
20 } /*end sysout()*/
21 } /*end IO{}*/
22
23 public class Bounce {
24 int S1, pos = 0;
25 float S2, L, V, C;
26 float T[] = new float[20];
27
28 /** Creates a new instance of Bounce */
29 public Bounce(float _V, float _S2, float _C) {
30 IO.sysout("\nV = " + (V = _V));
31 IO.sysout("\nS2 = " + (S2 = _S2));
32 IO.sysout("\nC = " + (C = _C));
33 IO.sysout("\nS1=" + (S1 = (int) (70 / (V / (16 * S2)))) + "\n");
34 } /*end constructor()*/
35
36 /* OVER-modularised & time-consuming code: */
37 public void fillTime() {
38 for (int i = 1; i <= S1; i++) {
39 T[i] = (float) (V * Math.pow(C, (i - 1)) / 16);
40 } /*end for(i)*/
41 } /*end fillTime()*/
42
43 /* More OVER-modularised & time-consuming code: */
44 public float getHeight() {
45 return (int) (-16 * ((V / 32) * (V / 32)) + (V * V) / 32 + 0.5);
46 } /*end getHeight()*/
47
48 /* Still More OVER-modularised & time-consuming code: */
49 public float getXCoor(int i, float j, float H) {
50 return (float) Math.abs((H - (0.5 * (-32) * (j * j) + V * Math.pow(C, (i - 1)) * j)));
51 } /*end getXCoor()*/
52
53 public void plotHeight() {
54 float t, height = getHeight();
55 for (float H = height; H >= 0; H -= 0.5) {
56 if (H == (int) H) {
57 IO.sysout("" + (int) H);
58 } /*end if(H)*/
59 L = 0;
60 for (int i = 1; i <= S1; i++) {
61 plotVal(i, H);
62 t = T[i + 1] / 2;
63 if (((-16) * (t * t) + V * Math.pow(C, (i - 1)) * t) < H) {
64 pos = 0;
65 IO.sysout("\n");
66 break;
67 } /*end if([math])*/
68 } /*end for(i)*/
69 } /*end for(h)*/
70 } /*end plotHeight()*/
71
72 public void plotVal(int i, float H) {
73 for (float j = 0; j <= T[i]; j += S2) {
74 L += S2;
75 float temp = getXCoor(i, j, H);
76 if (temp <= .25) {
77 // debug("[" + (int)(L/S2) + ":" + pos + "]");
78 IO.sysout(padString((int) (L / S2)) + "0");
79 } /*end if(temp)*/
80 } /*end for(j)*/
81 } /*end plotVal()*/
82
83 public void printAxis() {
84 IO.sysout("\n");
85 for (int i = 1; i <= (int) (L + 1) / S1 + 1; i++) {
86 IO.sysout(". ");
87 } /*end for(i)*/
88 } /*end printAxis()*/
89
90 public void labelAxis() {
91 pos = 0;
92 IO.sysout("\n0");
93 for (int i = 1; i <= (int) (L + .9995); i++) {
94 // debug("[" + (int)(i/S2) + ":" + pos + "]");
95 IO.sysout(padString((int) (i / S2)) + i);
96 } /*end for(i)*/
97 IO.sysout("\n" + padString((int) ((L + 1) / (2 * S2) - 2) + pos) + "Seconds");
98 } /*end labelAxis()*/
99
100 public String padString(int l) {
101 String s = "";
102 for (int i = 0; i <= (l - pos); i++) {
103 s += " ";
104 }
105 pos += s.length();
106 return s;
107 }
108
109 public void run() {
110 IO.sysout("\n");
111 this.fillTime();
112 this.plotHeight();
113 this.printAxis();
114 this.labelAxis();
115 } /*end run()*/
116
117 /* remove all debug() when satisfied. */
118 public void debug(Object o) {
119 IO.sysout(o);
120 } /*end debug()*/
121
122 /**
123 * @param args the command line arguments
124 */
125 public static void main(String[] args) {
126 Bounce b = new Bounce(IO.getVal("Velocity (FPS): "),
127 IO.getVal("Time Increment (Sec): "),
128 IO.getVal("Coefficient: "));
129 b.run();
130 } /*end main()*/
131
132 } /*end class Bounce{}*/