In this lab you will write a simple Java applet and a slightly more complex one. You will learn how to compile applets, test them, and add them to your web page.
During the course of the lab, if you run into problems or error messages that are not described on this lab sheet, ask a TA to help you out. Some of the software you're using is new to this class, so if you're having a problem it might be our fault. Don't struggle with some error the whole lab period.
This lab will be graded on line, so you don't need to print anything out at the end of lab. Approximate values of the parts of this lab:
After you have entered your login id and password, you should get a prompt that looks something like this:
[1] ecj@godzilla5%At that point you should type
cd public_htmland press <Enter>. Now you should type
ln -s ~gk6/public_html/awband press <Enter>.
You can now close the telnet session.
If you're curious, this step is just putting a "link" to the awb collection of Java classes into your web directory, so that applets you write can access them.
Last lab we had you use telnet and pico to edit your HTML home page. You can't use a common editor like Microsoft Word because the files you need to edit are not available directly on the computer you are sitting at. With Java programming this gets even more cumbersome. In the past, we have had students work on the local computer during lab, then move their work to their public_html directories. In order to make this easier, we have developed cpsEdit which allows you to work directly on your public_html HTML and Java files.
To run cpsEdit, double-click on the My Computer icon at the top left of your screen. Now double-click on the H: drive, which may be labeled Pgms on 'Duke-ac-cs...' . Next open Class and then CPS1 . You should now see "cpsEdit"--double-click on the icon to start the editor.
Since cpsEdit works in your acpub account, you first need to log in to the acpub system. Select the Connect menu item from the File menu. Enter your user id and password, and press OK. You should wait for the message box to print Connected before proceeding.
Note that this should be your normal telnet/ftp/mail login/password, not the password you gave for grade reports. You will stay connected until you exit the program (by closing all the cpsEdit windows). You should treat this application like telnet or ftp, and always make sure not to leave yourself logged in at the end of lab.
Your first applet will simply write "Hello, World" on the screen. The point of this applet is to introduce you to Java in the most painless way possible. Here is the code for your first applet:
import java.awt.*; import awb.*; public class HelloWorld extends java.applet.Applet { StringField sf; public void init() { sf = new StringField(40); sf.setString("Hello, World!"); add(sf); } }
After you've typed this text into the blank cpsEdit window, go to the File menu and choose "Save As...". A dialog box will appear. In the blank labeled "File name:" type HelloWorld.java. Capitalization is important in Java, both in the code you typed in and in the name you save it under.
Your next step is to compile your applet. Compile means to translate it to a format the computer can understand. To compile the applet, choose the Project menu, and then Compile. You should see "Success!" and no error messages. If there are error messages, check the line numbers where there were problems, and make sure you typed everything exactly as above. Once successfully compiled, the code is automatically saved in a file called HelloWorld.class.
You can now run the applet by choosing "Run" from the Project menu. You may encounter another window before your applet runs that asks you to "Accept" a license--OIT will fix this, but for now you can just click Accept each time. You should see a new window on the screen that has a StringField with "Hello, World!" in it.
If you are interested in avoiding the Assert Another way of doing the compile/run sequence is to not close the run window of the applet ("Appletviewer"). Instead just go back to cpsEdit and make whatever changes you'd like to make in your code, and compile again. Then, instead of doing Project->Run, go back to the applet window ("Appletviewer") and do File->Reload to run the new version of your applet. A warning here is that a running applet window is associated with a particular class file, so if you open another java file in cpsEdit, you need to do Project->Run the first time you compile.
When you are done looking at the applet, you can close it by choosing Close from the File menu
Now that the applet is working, you can add it to a web page. cpsEdit has already placed a copy of HelloWorld.java and HelloWorld.class in your public_html directory.
From the File menu choose New, and type in the following HTML:
<html> <body> <applet code="HelloWorld.class"> </applet> </body> </html>
Now use Save As to save this file as hello.html. Now you should be able to open this page in Netscape http://www.duke.edu/~login/hello.html, where login is your login id, something like gk6) and see the "Hello, World" applet again. In order to make this applet easier to get to from your home page, you should add a reference to it from your home page. You can Open the index.html file you made last class in cpsEdit, and add the following line somewhere in the body:
<a href="hello.html">Hello World Page</a>
You should go to your home page in Netscape and verify that this link works.
You have now written an applet, compiled it, tested it, referenced it from a web page, and then connected that new page to your existing pages. Most of your programming in this class will follow the above steps. In more complicated programs, you can expect the "edit, compile, test" steps to repeat several times before it working as you intended.
Now that you've learned the process of writing an applet, you are ready to create your decision tree. Hopefully you brought an outline to class. You'll need to create another new document in cpsEdit for this program.
The Java code below is similar to the one Dr. Biermann handed out in class. You can begin by simply copying the code and substituting in your own questions and answers where appropriate. If you do this successfully, and connect it your web page, you can get the 12 points for this section. You should test all the responses to the questions to make sure that everything is working.
Here is an outline of what is happening in the Decision Tree applet:
import java.awt.*; // THE PROGRAM import awb.*; public class BookAdvice extends java.applet.Applet { StringField messageField; Button startButton, answerButton1, answerButton2; int questionNumber; public void init() { messageField = new StringField("Book advice.",30); startButton = new Button("Start"); answerButton1 = new Button(" "); answerButton2 = new Button(" "); add(messageField); add(startButton); add(answerButton1); add(answerButton2); questionNumber = 0; } public boolean action(Event e, Object obj) { if (e.target == startButton) { questionNumber = 1; messageField.setString("Do you wish a math. approach?"); answerButton1.setLabel("Yes"); answerButton2.setLabel("No"); startButton.setLabel("Reset"); return true; } if ((questionNumber == 1) && (e.target == answerButton1)) { questionNumber = 2; messageField.setString("Interested in prog or theory?"); answerButton1.setLabel("Prog"); answerButton2.setLabel("Theory"); return true; } if ((questionNumber == 1) && (e.target == answerButton2)) { questionNumber = 3; messageField.setString("Interested: prog or overview?"); answerButton1.setLabel("Prog"); answerButton2.setLabel("Overview"); return true; } if ((questionNumber == 2) && (e.target == answerButton1)) { questionNumber = 4; messageField.setString("I recommend 'Oh! Pascal'."); answerButton1.setLabel(" "); answerButton2.setLabel(" "); return true; } if ((questionNumber == 2) && (e.target == answerButton2)) { questionNumber = 5; messageField.setString("I recommend Algorithmics."); answerButton1.setLabel(" "); answerButton2.setLabel(" "); return true; } if ((questionNumber == 3) && (e.target == answerButton1)) { questionNumber = 6; messageField.setString("I recommend 'Karel the Robot'."); answerButton1.setLabel(" "); answerButton2.setLabel(" "); return true; } if ((questionNumber == 3) && (e.target == answerButton2)) { questionNumber = 7; messageField.setString("I recommend 'Great Ideas'."); answerButton1.setLabel(" "); answerButton2.setLabel(" "); return true; } return true; } }
As we did with "Hello, World", you should create an HTML page that references the Decision Tree applet, and then connect that page to your home page.
In order earn the final 4 points, you need to modify the above code in some interesting way. For example, instead of just two answer Buttons, you might allow three responses at some or all steps. Or you could increase the total number of questions (increase the "depth" of some of the decision tree branches).