Spring 2003
(Sections 02 & 03 only) |
Lab # 2
Due at the end of your last lab in Week # 2. |
handin
system... |
Important Reminders |
If you are checking this out before lab, because I promised you some tips that might help you to get started with your first program, then you may or may not need or want all of this in advance. See if this short list of resources is enough; if it's not, then come back here and see if the rest of this Lab appears to help. (If neither seems enough, then pay close attention during lecture and lab during week #2 and consider stopping by during my Office Hours and/or forming a study group with some classmates.)
While you are welcome to read over and think about these activities in advance of Week #2 Lab sessions, please realize that part of your grade for this lab will depend on the extent to which you worked with other classmates during the lab. That is, teamwork is expected: it is actually required!
You will be expected to work with others in these labs. I repeat that even if you finish early in the lab and have completed all work, please help just one other person before you leave the lab. Become familiar with the person next to you, when I refer to "team" or your "team member" I will refer to a person nearby with whom you can easily work.
Once again, there is a checklist for this lab.
As you read through this lab, pay special attention when the lab tells you to note what happens in red like that. All those places require you to write something on the checklist. (Be aware that often, the red caution appears only once, but you are to make multiple notations, following the guidelines that come after the signal.)
In this lab, one thing you will do is to deliberately introduce some syntax errors into a Java program, and learn to recognize the kinds of error messages that each one generates. Before we get to that, however, we will review several activities from last week, adding a few new tricks as we go...
To begin with, as you should have already practiced, start today's lab by creating a temporary folder on your lab machine in which to save today's work. It should be on the S drive.
For the rest of this lab, that location will be referred to as: your temporary folder. Note that these notes were written before the students voted themselves a fee increase, and our labs are much better for the investment by the students. Again, I may often refer to "teams" sharing machines, but this is not nearly so necessary. When "teams" are referred to, make sure you work to some extent with another person nearby, the experience is important. Since each team should have at least two machines on which to work, repeat this step as needed, using a different person's name each time, until each team member has at least one, and possibly two, temporary directories.
Next, download (*) the file Hello.java to each temporary folder. When compiled and executed, this program will output the phrase, "Hello, world!" (on a line by itself in your execution window).
(*) Remember: to download a file linked in to a web page you can move the mouse so it points over the link and then either:
Once you see the dialog box, navigate your way to your temporary folder, and save the file under the same name as it was given originally (paying attention to case and extension as well as the basic file name).
Now, launch an editor (I will refer you to PFE (referenced here from an old lab at : PFE ref. From within the editor, do the following:
File: Open
and navigate through until you are able to open your Hello.java file. Then examine this file as downloaded.Then, in the appropriate DOS window, perform these actions:
dir
command (that is, list the contents of the current dir
ectory) to verify that you are in a folder that contains a file with Hello.java
as its name. If not, then you missed a step above, so now you still must change directories or do whatever else is needed until you succeed (e.g., change directories as illustrated in Lab #1, use the system Find feature, or even actually re-download Hello.java to the place where it should be).javac Hello.java
dir
command, and note what, if anything, has changed from the last time you listed the contents of the directory.java Hello
dir
command, and note what, if anything, has changed from the last time you listed the contents of the directory.Now for a little time-saving trick! You may be thinking that, as you edit and re-edit this file, compile and re-compile, execute and re-execute it, you are going to be typing the same commands over an over again many times. You are certainly welcome to do so, if you want. But our machines (and possibly yours at home) have a feature known as DOSKEY activated that can lead to some very handy shortcuts when you are working in your DOS window:
Up Arrow
goes back one step in your command history and shows the previously typed command. Hitting Up Arrow again goes back one more step.Down Arrow
goes forward one step in your command history from the (previous) command currently displayed. Hitting Down Arrow again goes forward one more step.Left Arrow
moves the cursor left one character on the current command line.Right Arrow
moves the cursor right one character on the current command line.Backspace
removes one character to left of the cursor on the current command line. Delete
removes one character at the cursor on the current command line. Enter
executes the command as currently shown (i.e., including any editing you have entered; but you conveniently need not move the cursor to the end of the line).Before moving to the next part of the lab, experiment to make sure you understand the instructions above. On your checklist, in the blank space at the end, note any discrepancies or concerns.
D:\temp\YourName> javac Hello.java
D:\temp\YourName> java Hello
D:\temp\YourName> java Hello
D:\temp\YourName> javac Hello.java
D:\temp\YourName> javac Hello.java
D:\temp\YourName> java Hello
Hello, world!
D:\temp\YourName> java Hello
Hello, world!
Now, return to PFE (or whatever editor you are using) and explore the impact of some relatively simple syntax errors. Make sure you have the line number display turned on. This is going to be a little strange, because we are going to deliberately introduce some errors. But, if you insert them, and note both your actions and the consequences, the hope is that you'll start to learn to recognize the kinds of errors that cause different kinds of messages (an important skill in the long run!).
To take notes on your results:
Note this PFE shortcut: To save
a change, click on the diskette
icon. The icon is bright red if you've made any changes since the last time you saved: get into the habit of saving your work often! The keystroke-equivalent for this is Ctrl+s (press and hold the Control key while you type the letter "s").
OK, here are some changes for you to try:
Hello
on line 54 to hello
. Save the file. Recompile the program in the DOS window (javac Hello.java
). Did you get a compile error?
dir
command to see and then record the last-modified time and file size of both your Hello.java and Hello.class files.Hello
on line 3 to hello
. Repeat the testing steps from the first change, above, and record your results both: in your notebook in a way that makes sense to you, and on the lab checklist next to the number 3.Hello
on line 58 to hello
. Repeat the test process from above. On your lab checklist, document your findings next to 58h.first quotation mark
(") on line 58. Repeat the test process from above. On your lab checklist, document your findings next to 58"1. last quotation mark
on line 58. Repeat the test process from above. On your lab checklist, document your findings next to 58"2.main
to man
on line 56. Repeat the test process from above. On your lab checklist, document your findings next to 56.semicolon
at the end of the statement on line 58. Repeat the test process from above. On your lab checklist, document your findings next to 58;.right brace
} on line 61. Repeat the test process from above. On your lab checklist, document your findings next to 61.Finally, download the file Bugs.java to your temporary directory. Compile it. Note the time (there is a clock in the back of the lab). Use your experience with Hello.java to fix the syntax errors. Recompile the program until there are no compile errors. Note the time again. Run the program.
On your checklist, note how long it took you to fix the errors, and how many you found. (Be accurate. Your lab grade will not depend on how long it took you, but I am collecting data on this task.) Once again, note the clock-time before you proceed with the next step.
Generate a listing of the corrected program, and its output, to be handed in as part of your lab report this week. Follow the instructions below on how to get this, but be sure to note on your checklist the time it took your team to accomplish this part of the lab.
At times, you will be asked to generate hardcopy of your programs and their output. Printing the program file itself should be straightforward. Here are some ways to print the the output.
edit-mark
and/or edit-copy
to copy the block output you want to capture into a buffer. Then, in another appropriate (*) window, use edit-paste
to place a copy of the block there. When all the output you need has been captured, use the Print tab to generate your printout. Note that, if your program generates a lot of output, you may have to change the Properties of your DOS window--enlarge the line count in the window's buffer--so that you'll get a scrollbar you can use to review and collect it all.
Using standard output: | java Hello |
Using redirection: | java Hello > myoutputfile.txt |
type
command, or an editor, to look at the contents of myoutputfile.txt (or whatever name you choose to use before the .txt extension) to check it out before you do anything with it. What should you do next? The best thing is to follow the same approach as in the previous method:
So far, the Java programs we have been examining have been application programs (or just applications). You run them using the java
command. Next, we'll take a quick look at Java applets: they are fun to play with, and you'll find them all over the web, but they are a little harder to work with until you really understand more about Java. (Also, please note that Java and Javascript are two different things entirely. We will not get into Javascript at all.) In this course, we will focus on Java applications, although we will occasionally detour briefly into Java applets.
java HelloApplet
appletviewer HelloApplet.html
Repeat the above steps for this pair of files (note in particular the effect of the additional commands included in the program file):
Now try to compile and execute this applet and note the resulting output:
(Here again, note, that there is no .class file available at this site. So the .html page may well show some text, but you won't see the .java Applet running in that window if you just use the URL above. You are to dowload both files, compile the .java file into a .class file, and then view the .html page on your local machine.)
Everyone should do that much. If you are even more curious, and have the time (in lab or on your own outside of class), see if you can understand enough of the applet code to answer the questions on the .html page. (View the .html in a browser window if the questions aren't visible in the appletviewer.)
To receive full credit for this lab, you must submit the following, stapled into a single set of pages (with the Bugs.java file LAST):
Some individuals will also be asked to exhibit in the lab setting their individual lab notebook, with the various comments and reflections noted. If you are asked to do so, that will count toward this lab grade. If you get are not asked this time, be patient: your time will come in a future lab!
handin
system...
Practice the process you will have to use to hand in your Programming Assignment #1.
Please remember these rules: