|ITEC 120 LAB 1|
Lab must be completed and reviewed by Peer Instructor on Wed, Jan 12 during lab class and no later than the first 10 minutes of lab class on Fri, Jan 14.
Recognizing syntax errors
When you make syntax errors in your program the compiler gives error messages and does not create the bytecode file. It saves time and frustration to learn what some of these messages are and what they mean. Unfortunately, at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred. Your only choice is to carefully study your program to find the error. In the following you will introduce a few typical errors into a simple program and examine the error messages.
1. Download the file called Hello.java. (This is the traditional first program a computer scientist writes in a new language.)
---Double click on My Computer to see if your have a drive mapped to H: (your home directory).
---Create a folder on your network drive named "ITEC120" (this is where you should store all your work for this class)
---Copy Hello.java to your ITEC120 directory on your network drive: Right-click on the link above, choose Save link as.., and save to your ITEC120 folder on your virtual drive (H:).
Compile and run the program to see what it does.
---TO COMPILE and RUN in
-- Open Hello.java in the PFE by double clicking on it.
-- To compile the program, once the file has been opened in PFE, select Execute, then Compile
-- This should give you back a window without error messages, if it compiled correctly - which it should.
-- You must close this window before you can continue working with original source file, Hello.java.
-- Now select the Execute, then Run
-- You should see the output from the program, again in its own DOS window
-- Make sure you look back at a listing of the files in your ITEC120 directory (on your H: drive), what file was created?
Then make the changes below, answering the questions as you go.
2. Class name different from file name. Delete one l (el) from the name of the class (so the first non-comment line is public class Helo), save the program, and recompile it. What was the error message?
3. Misspelling inside string. Correct the mistake above, then delete one l from the Hello in the message to be printed (inside the quotation marks). Save the program and recompile it. There is no error message-why not? Now run the program. What has changed?
4. No ending quotation mark in a string literal. Correct the spelling in the string, then delete the ending quotation mark enclosing the string Hello, World!. Save the program and recompile it. What error message(s) do you get?
5. No beginning quotation mark in a string literal. Put the ending quotation mark back, then take out the beginning one. Save and recompile. How many errors this time? Lots, even though there is really only one error. When you get lots of errors always concentrate on finding the first one listed!! Often fixing that one will fix the rest. After we study variables the error messages that came up this time will make more sense.
6. No semicolon
after a statement. Fix the last error (put the quotation mark back). Now
remove the semicolon at the end of the line that prints the message. Save the
program and recompile it. What error message(s) do you get?
Correcting syntax errors
File Problems.java contains a simple Java program that contains a number of syntax errors. Save the program to your directory, study it and correct as many of the errors as you can find. Then compile the program; if there are still errors, correct them.
Some things to remember:
Once you've completed the
lab, you should show the Peer Instructor or Instructor to be checked off:
-- Answer the questions about the Hello.java file.
-- Run the corrected Problems.java program.