For this lab, you will do two exercises involving try..catch statements. Both exercises deal with the conversion of strings to numbers. The second exercise is a GUI program that lets you work with text input boxes for the first time.
There is also a third exercise, which involves making a couple of applets, putting them on web pages, and publishing them on the web.
You should do Exercises 1 and 2 (or at least Exercise 1) during the lab, since try..catch statements will be on Monday's test. It is not important to do Exercise 3 before the test. If you haven't finished Project 1, which is due tomorrow, you might want to do some work on that.
To get started, create a directory named lab5. Copy all the files from the directory /classes/cs124/lab5-files into your lab5 folder. A command that will do this, if given while working in the lab5 directory, is:
cp /classes/cs124/lab5-files/* .
This lab is due at the beginning of next Thrusday's lab. Also remember that Project 1 (from Lab 3) is due by tomorrow, September 30, at 2:00 PM.
Exercise 1: The file Stats.java is an example that we did in class. It computes the count, sum, average, and maximum of a set of numbers input by the user. The user enters a zero to signal the end of the input data. However, I don't like having to type the extra "0", which doesn't really mean anything. And I don't really like having to exclude zero as a possible data value. You should modify the program so that the user simply enters an empty line (by pressing return without typing anything) to signal the end of the data. Zero will be treated as a possible data value, like any other. Furthermore, if the user enters any illegal value, like "fred" or "forty-two", the program should not end. You should tell the user about the error and continue processing.
This is harder than it might sound. You can't use TextIO.getlnDouble() to read the user's input, since you don't know whether the user will enter a number, some junk, or an empty line. The solution is to use TextIO.getln() to read a String. If the string is empty, that is a signal that all the data has been input. If not, then the input should be a number. You can convert the input string into a number using Double.parseDouble(). If the input string does not represent a legal number, then the method will throw a NumberFormatException. If such an exception is thrown, you should catch it and use it as a signal that the user's input was illegal.
Note: You should change the comments in the program to reflect the changes that you make!
Exercise 2: The file GUIStats.java is essentially a GUI version of the Stats program from the preceding exercise. It's not quite functionally the same because instead of getting all the data and showing the stats at the end, the GUI version continually shows the stats for all the numbers that have been entered so far. These values change each time the user enters a number. The GUI version can already react to numbers entered by the user. However, if the user enters an empty or illegal value, that value is simply ignored. (If you try it, you will see that an "exception" is reported in the Terminal.) Your job is to make it give an error message when the input value is not a legal number. You will also implement a "Reset" functionality, that will allow the user to start over, with everything in its initial state.
For this exercise, you only need to work inside the subroutine named updateStats, and for the most part, you don't need to understand the rest of the code. However, there are a few things that you do need to understand. You have already noticed that a class can contain more than just a main() routine. In fact, it can also contain other subroutine definitions, and it can contain variable declarations. Variables that are declared outside of any subroutine can be used in all the subroutines in the class, and they keep their values between subroutine calls. Variables that are declared inside a subroutine only last as long as the subroutine is running, and their values are lost when the subroutine ends.
The GUIStats program declares three variables named count, sum, and max to keep track of important values. You need to understand that these variables are declared outside updateStats(), but they can still be used inside updateStats().
You should add a try..catch statement to updateStats() to catch the NumberFormatException that is thrown when the user's input is illegal. Use the following statement when an error occurs:
JOptionPane.showMessageDialog(this, "Illegal Input!");
You will have to think a bit (or experiment) about where all the other code in the subroutine should go...
Note: This is not a long exercise. Part of the reason for doing it is to let you get a glimpse of what it's like to work with GUI components and events. You should think about the difference between the GUI version and the command-line version. Note that there is no loop in the GUI program. Instead, the program just reacts to "events" generated by actions taken by the user.
Exercise 3: For the last exercise, you will publish an applet version of the GUIStats program on the web. And you will make an applet version of the Art program from Lab 4 and publish it on the web. (If you want, you can do the same for the graphics program from Lab 2.)
Web pages are defined by HTML files. HTML is a language for describing web pages. A web page can contain applets, which are just Java GUI programs that run in a rectangular area on a web page. The basics of writing HTML code are discussed in Subsection 6.2.3 and the first few paragraphs of Subsection 6.2.4. You can read that material, but you don't need to understand much about HTML to complete this lab.
The GUI programs that you have worked on are defined as "JPanels." A JPanel is just a rectangular area that holds GUI components. Each program contains a main() routine that does nothing but create a window and a panel, add the panel to the window, and show the window on the screen. To have an applet, you just have to add the panel to an applet instead of to a window. And you need a web page on which to show the applet. I have already done this for you, for the GUIStats program. The file GUIStatsApplet.java defines the applet.
The file GUIStats.html is a web page that shows a GUIStats applet. The file index.html is a web page that contains a link to GUIStats.html. To view your copies of these files in a Web browser, open a file browser window, navigate to your lab5 folder, and double-click the icon for index.html. This will open that file in a browser. Click the link to go the page that shows the applet. Note that in order to be functional, the two class files GUIStats.class and GUIStatsApplet.class must be in the same folder with the HTML file GUIStats.html.
Only you can see these files. They are not published on the web. However, your home folder contains a folder named "www". Any files that you put in that folder are visible on the web and can be seen by anyone on the Internet. You will need to copy some files into the www folder. You can do one of two things:
Do number 2 if you already have some stuff in www, or if you want to allow for web pages in future courses. So, copy the following files: index.html, GUIStats.html, GUIStats.class, and GUIStatsApplet.class. Once you do this, you can view your pages from any computer on the Internet by using a URL of one of the following forms, with zz9999 replaced by your own user name:
Now, you just have to publish another applet! You will need to do several things:
setContentPane( new GUIStats() );replace GUIStats with the name of your art program. This line is what tells the applet which panel it should display.
<li><a href="GUIStats.html">GUIStats applet</a> -- an applet that computes basic statistics for numbers entered by the user.</li>and make the appropriate changes. The main thing is to replace "GUIStats.html" with the name of your new HTML file. You can open index.html in a web browser and test the link.
When I grade this lab, I expect to see your pages on the web!