// cash.java // flow of from a money tank // adapted from a program in basic by H.T. Odum & E. Odum, 1994. import java.applet.*; import java.awt.*; public class cash extends Applet { Label prompt1, prompt2, prompt3; TextField input1, input2, input3; Choice chooseEvent; Button drawButton; double m, k0, k, t1, mi, dm, j0, j; int a, t, ti; public void init() { prompt1 = new Label("Start value for M : "); input1 = new TextField("10", 5); prompt2 = new Label("K Spending coefficient : "); input2 = new TextField("5", 4); prompt3 = new Label(" t_1 : "); input3 = new TextField("96", 4); chooseEvent = new Choice(); chooseEvent.addItem("No change of income/spending"); chooseEvent.addItem("Get a better salary at t_1"); chooseEvent.addItem("Win one Million at t_1"); chooseEvent.addItem("Get robbed at t_1"); chooseEvent.addItem("Loose your job at t_1"); drawButton = new Button("Draw"); add(prompt1); add(input1); add(drawButton); add(prompt2); add(input2); add(prompt3); add(input3); add(chooseEvent); } public boolean action(Event e, Object o) { m = (float)Integer.parseInt(input1.getText()); k0 = 0.01f*(float)Integer.parseInt(input2.getText()); t1 = Integer.parseInt(input3.getText()); if (e.target instanceof Choice) a = chooseEvent.getSelectedIndex(); repaint(); return true; } public void paint(Graphics g) { g.drawRect(0,90,320,250); t=0; j0=4; j=j0; k=k0; while (t<320) { ti=t+1; if (ti == t1) { switch(a) { case 0: break; case 1: j=j+2; break; case 2: m=m+100; g.setColor(Color.red); g.drawLine((int)t, (int)(340-mi), (int)ti, (int)(340-m)); break; case 3: m=m-10; g.setColor(Color.red); g.drawLine((int)t, (int)(340-mi), (int)ti, (int)(340-m)); break; case 4: j=1; k=k0-0.01; break; default: break; } } dm=j-k*m; mi=m+dm; g.setColor(Color.blue); g.drawLine((int)t, (int)(340-m), (int)ti, (int)(340-mi)); t=ti;m=mi; } } } // Manuel Basler & e. Ortega, October 26th 2000