In this lab you will write your first Java applet using the subroutines you have studied in class. You will learn how to compile applets, test them, and add them to your web page.
First you will create a subdirectory of your public_html directory to store your applet files for this lab. Access the P: drive from My Computer on your desktop. Open the folder for your public_html directory. Create a new folder titled cps001. Open the folder for the cps001 directory you just made. Create a new folder titled Lab4.
The following Java program is an applet for computing the minimum of two integers. You should recognize the minimum subroutine from class.
Here is what the applet looks like when run from a web page:
Look over the Java code. How hard would it be to write a Java applet to compute the sum of two integers instead? The difference? The product? How about all three? This is what you will do in lab today!
We will start out by writing a Java applet to compute the sum of two integers. Before you start writing code, you should think carefully about what you need to do. Remember what you learned in class about problem solving and algorithm design. What will you need for input? Output? How will you perform the computation? You should see that you can easily modify the Minimum program to compute the sum of two integers instead.
Here is what your modified applet should look like:
Open GNU Emacs from the Start menu. Go to File->Open File on the drop-down menu. Click through the directories until you get to the Lab4 folder you just made. Type in the file name Arithmetic.java. You should get a blank screen - this is because you haven't typed anything yet. Resize the Emacs window so you can see the entire window on your monitor. You should be able to read the minibuffer at the bottom of the Emacs window.
Type in your Java program. Remember that the class name should match the name of your .java file, so be sure the class declaration is
Once you have entered all the text, you are ready to compile.
Now let's compile. Emacs has a Compile option that will do this for you. Go to Tools->Compile on the drop-down menu. The first time you compile, the minibuffer in Emacs will read Compile command: make -k. In the minibuffer, backspace over the make -k and replace it with javac filename.java --- but don't actually type in filename.java!. In this case, filename.java should be replaced by Arithmetic.java. For other programs, it will obviously be different. When you compile, Emacs splits into two frames. One still contains your code, and the other will display the output of the Java compiler. If all you get for output is Compilation finished at ... then your code compiled just fine. If you get errors, skip ahead to the next section.
Some good things to know when using Emacs: If you want to get back to one window, go to File->Unsplit Windows on the drop-down menu. You can see a list of all the files you have open by clicking on Buffers. You can switch between files by selecting the file you want from the Buffers drop-down menu. If the file name has a * after it, it means you haven't saved it since the last time you changed the file.
Once successfully compiled, the code is automatically saved in a file called Arithmetic.class. You should see the .class file in your My Computer directory window.
If you get compile errors, you need to fix them. There will be a list of errors that the compiler will output. It will tell you the line number in the file where it found the error. Here is an example of what the compiler says when it finds a class name it doesn't recognize:
The actual error is that the class name TextField is misspelled. Notice that there is an "arrow" (^) that points to TextFied. It also says the line number in the file where it found the error -- in the file Arithmetic.java at line 11.
Do your best to go through your errors and try to fix them. Start with the top error, attempt to correct it, and compile again. Then move to the next error and do the same thing. Sometimes fixing the first error will make some of the ones below it disappear, so it's best to start at the beginning and go through them one at a time.
To run our applets on the WWW, you need to create an HTML document to serve as its "home". Go to File->Open and open a new file in your Lab4 directory named Arithmetic.html.
Type in the following HTML:
Save the file. If you did everything correctly, you should be able to click on your name below and see the webpage with your applet!
If something goes wrong, first make sure that you compiled Arithmetic.java, you have a Arithmetic.class file, you saved your Arithmetic.html file, and that all the files are in the same directory. If all this checks out and you are still having problems, yell for help.
All you need to do now is add some functionality to subtract and multiply the input integers. You will definitely need to implement the following items:
Add two new buttons labeled "Subtract" and "Multiply".
Add two new subroutines to perform these computations.
Modify the actionPerformed to call the subroutine corresponding to the button clicked and to display the appropriate text.
Here is what your final applet should look like:
In Lab 3 we made a webpage for this class named cps001. Add a link to your applet on your CPS1 page. The easiest thing to do is to start a list, as you will be creating a similar link for the applets you write in future labs. For example, you could do something like this: