import java.applet.*; import java.awt.*; import java.lang.Integer; public class DoArray extends Applet { //for ease in finding, set the constants for this specific program final int FONTSIZE = 12; //size of font to indicate spacing final String FONTTYPE = "TimesRoman"; final int ALIGN = 20; //aligning values in from edge final int OFFSET = FONTSIZE + 6; //sets offset of lines final int END = 20; final int YMAX = 400; final int BASE = 100; //set nonconstant values of program int BASE1 = 110; //number indicating top of example String part1[] = {" DO 10 I = 1, 5", " ARRAY[I] = I+1", " 10 CONTINUE"}; int BASE2 = BASE1 + OFFSET*3; String part2[] = {" ARRAY[1] = 10", " ARRAY[4] = 3"}; int BASE3 = BASE1 + OFFSET*5; String part3[] = {" DO 20 I = 1, 5", " IF (MOD(ARRAY[I],2).EQ.0)", " ARRAY[I] = ARRAY[I] / 2", " ENDIF", " 20 CONTINUE"}; String msg, mod, i = "undefined"; String a[] = {"undefined", "undefined", "undefined", "undefined", "undefined"}; Font theFont = new Font(FONTTYPE, Font.BOLD, FONTSIZE); int count; //what step we're on int firstclick; //tells if mouse has been clicked int move; //how far has image moved? int pointat; //current position of pointer Image pointer; //the image used as a pointer /* methods: init() mousedown() restart() paint() moveout() movein() */ //************************************************** public void init() { pointer = getImage(getCodeBase(), "images/arrow.gif"); msg = ""; mod = ""; count=0; firstclick=0; pointat = BASE1; move=0; setBackground(Color.white); nextstep(); //this takes care of step 0! } //********************************************************* public boolean mouseDown(Event e, int x, int y) { count++; nextstep(); //change variables for each click repaint(); //see all new in if (firstclick == 0) firstclick=1; return true; //has to be boolean } //********************************************************** public void moveout() { for (move = 0; move < END; move++) //go through loop END times { BASE1 = BASE1 - 1; BASE3 = BASE3 + 1; repaint(); //repaint each time for animation } } //********************************************************** public void movein() { for (move = END; move>0; move--) { BASE1 = BASE1 + 1; BASE3 = BASE3 - 1; repaint(); } } //********************************************************* public void restart() { count = 0; firstclick = 0; pointat = BASE1; //move pointer back to start move = 0; msg=""; mod=""; i="undefined"; for (int l=0; l<5; l++) a[l] = "undefined"; repaint(); } //************************************************************* public void next() { pointat +=OFFSET; } //************************************************************* public void nextstep() { switch(count) { case 0: msg="Click anywhere in the applet to begin"; break; case 1: //animation occurs msg="This program can be separated into 3 distinct parts"; break; case 2: //array statement moveout(); pointat = pointat - END; next(); msg = "The first spot in ARRAY is set to I+1"; i = "1"; break; case 3: // continue next(); msg = ""; a[0] = "2"; break; case 4: case 7: case 10: case 13: //for statement pointat = BASE1; msg = ""; break; case 5: next(); //array msg = "The second spot in ARRAY is filled with I+1"; i = "2"; break; case 6: //for next(); msg = ""; a[1] = "3"; break; case 8: next(); msg = "The third spot in ARRAY is filled"; i = "3"; break; case 9: next(); msg = ""; a[2] = "4"; break; case 11: next(); msg = "The fourth spot is filled"; i = "4"; break; case 12: next(); msg = ""; a[3] = "5"; break; case 14: next(); msg = "The fifth spot is filled"; i = "5"; break; case 15: next(); msg = ""; a[4] = "6"; break; case 16: pointat = BASE1; msg = "Can't go into loop"; break; case 17: //second part pointat = BASE2; i = "6"; msg = "set the first value directly to 10"; break; case 18: next(); a[0] = "10"; msg = "set the fourth value to 3"; break; case 19: //third part, for pointat = BASE3; a[3] = "3"; msg = "another loop!!"; break; case 20: //if next(); msg = "ARRAY[I] = ARRAY[1] = 10"; mod = "MOD(10, 2) equals 0"; i = "1"; break; case 21: case 30: case 39: //in mod, set next(); msg = "IF statement was true, so proceed inside."; mod = ""; break; case 22: //endif next(); msg = ""; a[0] = "5"; break; case 23: case 27: case 32: case 36: case 41://continue next(); msg = ""; break; case 24: case 28: case 33: case 37: //for pointat = BASE3; msg = "procede to the next loop"; break; case 25: //if next(); msg = "ARRAY[I] = ARRAY[2] = 3"; mod = "MOD(3,2) equals 1"; i = "2"; break; case 26: case 35: //endif, no set next(); next(); msg = "IF statement was NOT true, skip the inside of the IF statement"; mod = ""; break; case 29: //if statement next(); msg = "ARRAY[3] = 4"; mod = "MOD(4,2) = 0"; i = "3"; break; case 31: //endif after set next(); msg = ""; a[2] = "2"; break; case 34: //if statement next(); msg = "ARRAY[I] = 3"; mod = "MOD(3,2) = 1"; i = "4"; break; case 38: next(); msg = "ARRAY[I] = 6"; mod = "MOD(6, 2) = 0"; i = "5"; break; case 40: //endif after set next(); msg = ""; a[4] = "3"; break; case 42: pointat = BASE3; msg = "adding 1 to I puts it over max. Skip loop"; break; case 43: movein(); pointat=pointat-END; next(); next(); next(); next(); msg = "DONE! Click again to restart."; i = "6"; break; case 44: restart(); break; } } //************************************************************* public void paint(Graphics g) { g.setFont (theFont); //set 3 parts of program for (int l=0; l<3; l++) g.drawString (part1[l], ALIGN/2, BASE1+l*OFFSET); for (int l=0; l<2; l++) g.drawString (part2[l], ALIGN/2, BASE2+l*OFFSET); for (int l=0; l<5; l++) g.drawString (part3[l], ALIGN/2, BASE3+l*OFFSET); g.drawImage(pointer, 230, pointat-OFFSET-6, this); g.drawString("I = ", 300, BASE+OFFSET); g.drawString("ARRAY[1] = ", 300, BASE+2*OFFSET); g.drawString("ARRAY[2] = ", 300, BASE+3*OFFSET); g.drawString("ARRAY[3] = ", 300, BASE+4*OFFSET); g.drawString("ARRAY[4] = ", 300, BASE+5*OFFSET); g.drawString("ARRAY[5] = ", 300, BASE+6*OFFSET); if (msg != "") g.drawString(msg, ALIGN/2, YMAX-50); g.setColor(Color.blue); g.drawString(i, 320, BASE+OFFSET); g.drawString(a[0], 375, BASE+2*OFFSET); g.drawString(a[1], 375, BASE+3*OFFSET); g.drawString(a[2], 375, BASE+4*OFFSET); g.drawString(a[3], 375, BASE+5*OFFSET); g.drawString(a[4], 375, BASE+6*OFFSET); if (mod != "") g.drawString(mod, 290, BASE+7*OFFSET+5); g.setColor(Color.black); } }