CS124, Fall 2001
Lab 2: Variables and TextIO
In this week's lab, you will be writing two short programs. These programs use the non-standard class TextIO to read data typed in by the user. In a third exercise, you will set up a "home page" on the Web.
Any program that you turn in for this course, including the two programs that you write for this lab, should obey the rules of good programming style. We will encounter programming style rules throughout the course. Here are a few that you should already be using:
- Put a comment at the beginning of the program that explains what the program does. This comment should explain the purpose of the program. It should not explain how you have coded the program to accomplish this purpose. Include your name in this comment.
- Use meaningful variable names when possible.
- Usually, you should declare each variable on a separate line and include a comment stating the purpose that the variable serves in your program. When several variables are closely related, it might be OK to declare them on the same line.
- Use indentation to show the structure of the program. This means that you should indent the main() routine inside the class, and you should indent the statements inside the main() routine. Use three or four spaces for indentation. It's not a good idea to use tabs for indentation.
- Never use comments to explain how Java works! Assume that the reader knows Java. A comment such as "declare a variable named interestRate" is useless. The comment "annual interest rate, expressed in decimal form" gives information that makes it easier for the reader to understand the program.
All the files that you need for this lab can be found in the directory /home/cs124/lab2. (They are also linked to this page so that you can read them on-line if you want.) To begin the lab, you should copy this directory into your home directory. You can do this using the GUI or with the command line
cp -r /home/cs124/lab2 .
Don't forget the period at the end, which stands for your current directory. The "-r" tells the computer to copy the directory with all its contents. Once you have copied the directory, enter the command "cd lab2". Then the directory that you have copied will be your current directory. You can use the ls or dir command to see the list of files in this directory. The rest of this lab assumes that you are working in your copy of the lab2 directory.
Exercise 1: Using TextIO
One of the files in the lab2 directory is TextIO.java. As discussed in Section 2.4, this file defines some subroutines that you can use for reading data typed by the user into your program. You don't have to read this file or understand the Java code in it. All you need to know is the names of the subroutines and what they do.
In fact, you don't really need the source code file, TextIO.java, at all! What you need is the compiled class file. To get this file, compile the source code with the command "javac TextIO.java". You will see that the file TextIO.class has appeared. At this point, you could even delete TextIO.java because you have no further use for it in this lab. TextIO.class is not a program. You can't run it. It is merely a "helper class" that can be used by other programs.
The file Compute.java contains a short program that uses TextIO. Currently, all it does is read one number from the user and then print out that same number. Compile and run the program to see what it does. The program uses TextIO.getlnInt() to read the user's number. When the computer sees the reference to TextIO, it looks for the subroutine in the TextIO.class file.
You should try running the program and entering some non-numerical data. The program won't accept it. TextIO does this for you automatically. If you tell it to get an integer from the user, it won't be happy until the user actually types in an integer.
For your first exercise, you should modify Compute.java. The modified program should read two integers from the user. It should compute and print out the sum, product, quotient, and remainder of the two numbers. (This is not meant to be difficult -- just a little program to get you started.)
Make a print out of your program using the a2ps command. Don't forget to follow all the rules of good programming style. Turn in your printout in class next Wednesday, September 13.
Exercise 2: Talk to your Computer
For your second exercise, you will write a program from scratch. The program should hold a short conversation with the user. The program should ask the user for information and should use that information in its responses. It should ask for at least one number, and it should do some calculation with that number. Here is a sample conversation, with the user's input underlined:Hi, my name is Data. What's your name? Spock Gee, Spock is a nice name. Say, how many years old are you? 187 Let's see, this is 2471, so you must have been born in 2284. That was a good year. . . .
Remember that your program is essentially a "script" for the conversation. You are welcome to be amusing, obnoxious, or profound. You should have at least five questions and answers. For input values of type String, you can use TextIO.getln() to read the user's response.
Make a printout of your program using a2ps and turn it in along with your answer to Exercise 1.
Exercise 3: Web Page, with Applet
Web pages are defined in a language known as HTML (HyperText Markup Language). An HTML source file contains the words that are to be shown on the web page, along with special "tags" that define the format of the page: how the words are to be laid out, what size and color the characters should be, the images that should appear on the page, and so on. Tags are enclosed in angle brackets. For example, <hr> is a tag that tells the computer to draw a line across the page. ("hr" stands for "horizontal rule", a printer's term for horizontal line.) Most tags come in pairs consisting of an opening tag and a closing tag. Tags of this form apply to the text that comes between the tags. For example, <h1>My Web Page</h1> tells the computer to display the text "My Web Page" as a headline in large type. Similarly, the text between <p> and </p> is displayed as a paragraph. You don't need to know much about HTML for this lab, but if you want to know more, you can look ahead to Section 6.2 in the text.
One of the things that you can do with Java is write applets, which are programs that appear on Web pages. For this exercise, you will set up a Web page that displays an applet. Later in the course you will write your own applets. For now, you will just use an applet that I give you.
The file cs124.html is a starting point for your web page. (To see the HTML source for this page on line, click here). You should edit this file and personalize the Web page by changing some of the text on it. This will become your main page for the course, and you will link the other applets that you write for the course to this page.
The following lines in the HTML source add an applet to the Web page, when the page is displayed by a Web browser:<applet code="KaleidaAnimate.class" width=150 height=150> </applet>
KaleidaAnimate.class is a compiled class file for the applet. It must be in the same directory as the HTML file. As it happens, this class needs a "helper" class, just as the programs in the previous two exercises needed the helper class TextIO.class. The helper class is named SimpleAnimationApplet.class. This class must also be in the same directory as the HTML file. I have not given you the class files, but I did give you the Java source code files, KaleidaAnimate.java and SimpleAnimationApplet.java.
Compile the files SimpleAnimationApplet.java and KaleidaAnimate.java to get the class files that you need. You'll notice a few new things when you do this. The compiler will list a "warning" for SimpleAnimationApplet.java, saying that it uses a "deprecated API". This means that it uses some old commands that you are encourged to avoid in new programs. However, it is only a warning, not an error, and the .class file is created correctly. The other thing to note is that compiling KaleidaAnimate.java produces two classes: KaleidaAnimate.class and KaleidaAnimate$Figure.class. You need both these classes to run the applet. For now, you should be aware that such complications are possible without understanding them.
To publish the Web page on the Web, you just have to copy some files into the www directory that is located in your home directory. Anything that you put in this directory can be accessed on the web. You can copy the files with the following four commands, except that you should replace "username" with your own user name:cp cs124.html /home/username/www cp SimpleAnimationApplet.class /home/username/www cp KaleidaAnimate.class /home/username/www cp KaleidaAnimate$Figure.class /home/username/www
This is a good time to use the up-arrow key and the TAB key! Alternatively, of course, you could use the GUI to copy the files. Be sure that you understand why you don't have to copy the .java files. Once you have done this, you should be able to see your page on the Web at the addresshttp://math.hws.edu/user/username/cs124.html
but with username replaced by your own username.
There is nothing to turn in for this exercise. I will check for your page on the Web next Thursday, September 14. Make sure that your page is completed and that it is accessible on the Web by that time.
Two Configuration Notes
When you start up nedit, you might see a message in the console window saying that your .nedit file is for an old version of nedit. This is because we have installed a new version of the program. To fix the error, just go to the "Preferences" menu in nedit and select the "Save Defaults" command. This will make a new .nedit file in your home directory.
I might send email to you at your @hws.edu address. If you do not read mail sent to this address, it would be a good idea to have your mail forwarded to an account that you do use. To set up mail forwarding, you have to telnet to the computer hws3.hws.edu. For example, you can do this from your Linux account with the command "telnet hws3". (If you don't know your hws3 password, you will have to check with the people at Williams Hall. Initially, at least, it was the same as your Windows password.) Once you are logged on to hws3, type in the command "mail". You will see a MAIL> prompt. At this prompt, enter "set forward <email-address>", where <email-address> is the address to which your mail is to be forwarded. You can then exit from the mail system by entering "exit" and log off from hws3 with the command "logout". You should send an email to your @hws.edu address, to make sure that it is forwarded properly.