CPS 1 (Ramm) Page 1 of 6 20 February Spring Semester, 1998 Section _____ CLOSED BOOK 50 min Test NAME_____________________ Note, if there is any chance that your notes and work might be confused for the answers, circle your answers. If it's ambiguous, it's wrong. (10) A _____ (10) B _____ (20) C _____ (10) D _____ (10) E _____ (20) F _____ (20) G _____ ------------- ----------------------------------------------------------------------------- HONOR CODE I have not received or given any improper help on this exam. (your signature) 50 min Test Page 2 of 6 A.1) The following Java applet has had some of its punctuation marks removed. Restore all of these in their proper position. (Don't let the spaces fool you. Not all spaces indicate missing punctuation.) public class HelloWorld extends java.applet.Applet TextField m1 m2 public void init () { m1 new TextField( 40 ) m1.setText( Hello World! ) m2 new TextField( 80 ) m2.setForeground( Color.blue ) m2.setBackground( Color.red ) m2.setText( This is a simple demo of Java. ) add( m1 ) add( m2 ) } A.2) Is the applet above interactive? (i.e. does it change its output when you click something with the mouse?) 0 1 2 3 4 5 6 7 8 9 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ dataA | 95.0 | 5.0 | 50.0 | 50.0 | 90.0 | 10.0 | 85.0 | 15.0 | 80.0 | 20.0 | ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ B. The following code fragment "processes" two arrays of 10 doubles called dataA and dataB. Assume that dataA initially contains the data shown above. How does the data in dataB differ from that in dataA when this fragment completes execution? n = 10; k = 0; while (k < n) { dataB[k] = dataA[9-k]: k = k + 1; } 50 min Test Page 3 of 6 C. Assume The following Java applet is run. The user types the string PARTNER into the TextField, followed by the Return (Enter) key. Show exactly what output would appear in the TextField. import java.awt.*; public class Manip extends java.applet.Applet { TextField mWord; String word, newWord; public void init () { mWord = new TextField(20); add(mWord) ; } public boolean action(Event e,Object obj) { int k, leng; if (e.target == mWord) { word = mWord.getText(); leng = word.length(); newWord = ""; k = leng; while (k > 0) { k = k - 1; newWord = newWord + word.substring(k, k+1); } mWord.setText(newWord); return true; } return false; mWord } ----------------------- } | | ----------------------- D.1) Below, write the HTML code it would take to add an applet to your home page named FunGame.class D.2) Assume that the instructions for the game are located in a file whose URL is http://www.cs.duke.edu/~dr/FunDocs.html Show how you would set up a hyper-text link labeled "help" which would get you to that file from your home page. 50 min Test Page 4 of 6 E. True/False 1) We could run out of IP addresses fairly soon (a matter of a few years). 2) The World Wide Web is designed to make use of the Internet less difficult. 3) When using domain addressing, the "org" domain indicates that this message comes from the originating site. 4) Every Ethernet controller in a local area network (LAN) has a different address. 5) When using Ethernet, you are secure from all electronic eavesdropping. 6) The IP layer of the TCP/IP software used for Internet communications tries to make all networks look the same, regardless of underlying technology. 7) Anonymous ftp allows you to access information on a distant machine without having an account and password. 8) If a packet is found to be defective (using a checksum) a request is made of the originating host to send it again. 9) In addition to LAN's, we have Wide Area Networks (WAN's). F. Here are some simple rules defining Java syntax. #1 -> //a sequence of letters and/or digits that begin with a letter// #2 -> = ; #3 -> #4 -> #5 -> #6 -> "//any string of printable characters//" #7 -> + #8 -> #9 -> //a sequence of digits// #10 -> { // a sequence of 's // } In the style shown in the lectures, show how these rules can be used to generate the following Java program fragment (thus verifying that it is syntactically correct. Identify the rule used at each stage. ---- CONINUED ON NEXT PAGE ---- 50 min Test Page 5 of 6 { m = size + 4; p = "success!"; k = 17; } Start with: #10 -> 50 min Test Page 6 of 6 G. The program below (similar to Tallies.java shown in class) is designed to tally votes in a two person election. Here Andrea and Jim are running. Assume we get a last-minute addition to the field and Pat is added to the ballot. Modify the program below to handle the third candidate, Pat. Just write in the changes. Be neat. Use arrows to show where your additions go if you don't have space to put them in the correct place. If it's ambiguous, it's wrong! public class Vote extends java.applet.Applet { TextField mInstruct,mAnsJim, mAnsAndrea; Button bJim, bAndrea; int noJim, noAndrea; public void init () { mInstruct = new TextField(70); mInstruct.setText("Count votes by pushing correct buttons."); bJim = new Button("Jim"); bAndrea = new Button("Andrea"); mAnsJim = new TextField(15); mAnsAndrea = new TextField(15); add(mInstruct) ; add(bJim) ; add(bAndrea) ; add(mAnsJim) ; add(mAnsAndrea) ; } public boolean action(Event e,Object obj) { if (e.target == bJim) { noJim = noJim + 1; mAnsJim.setText(noJim + " for Jim"); return true; } if (e.target == bAndrea) { noAndrea = noAndrea + 1; mAnsAndrea.setText(noAndrea + " for Andrea"); return true; } return false; } }