Objectives: |
|
Background: |
This is the first programming lab in Java. You will experiment with different kinds of compiler errors and will learn to recognize when an error occurs, what information is conveyed in the error message, where the error occurred and how to resolve it. This lab also emphasizes the cycle of edit, save, compile then run. |
New Terms: |
|
Materials Needed: |
|
Submission: |
Upload these items via the Blackboard assignment link:
|
Part 1: Setting up your environment
- Log into the lab computer.
- Create a directory (Lab02B) on your desktop or removable
drive for this lab.
- Open JGrasp. (You may use another editor if you prefer.)
Part 2: The edit→save→compile→run cycle

- Create a new java file. File/New/Java
- Type the following code into the editor window. Pay
attention to all spelling, punctuation, and indentation.
NOTE: Java is case sensitive, meaning that "System" is different than "system". Using the wrong case anywhere will cause an error.
- Make sure you change the name and the date to your name
and the current date.
- Save the file into the folder that you just created for
this lab, using the file name "Hello.java".
- Compile your Java program (use the green +
icon at the top of the page).
- If it compiles successfully you will get a message: ----jGRASP: operation complete . If you have other messages indicating errors, check your typing very carefully. Your error message will give you the line number of the first place the compiler was "confused" by what you typed.
- Upon successful compilation, examine the directory
(use the file browser) in which you placed your Hello.java file and you should
see a Hello.class file,
which is the executable version of your program, in byte
code.
- Execute your Java program from JGrasp. (Use the red
running man icon at the top of the page.)
- In the Run I/O tab, you should see the message, "Hello, World!" appear. If not, please see the instructor before proceeding.
You have just completed the edit→save→compile→execute cycle.
Each time you change and save your program, you will need to
recompile the source file to see the changes reflected in the
executed program.
Part 3: Syntax Errors
This part of the lab will give you some practice in reading
and interpreting syntax errors. Recall that syntax errors are
errors in the form of the statements. As you make each error,
pay particular attention to the error message produced, and in
some cases, a single error will cascade several other errors.
Return to JGrasp to do this and the following portion of
the lab.
Record the answers to the following questions in your
Worksheet, to be handed in at the end of the lab.
- Class name different from file name. Delete the
beginning ‘H’ from the name of the
class (so the first non-comment line is public
class ello) and save the program.
(Question 1) What happens when you try to save it?
- Class name different from file name. Go
ahead and save your file anyway as Hello.java.
Compile your program. Keep the public class ello
statement in the code.
(Question 2) What error message do you get during the compile?
- Interpreting the error message. All
compiler messages will begin with the name of the source
file (Hello.java) and the line
number in that file that contains the error. This will be
followed by the text describing the error condition, and
will be followed by the statement with an up carat (^) to show where the error was found.
(Question 3) Under what character is the up carat found?
- Misspelling inside string. Correct the
mistake above, and save, and recompile. Next, 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
--
(Question 4) Why is there no error message?
Now run the program.
(Question 5) Is this output correct using normal English spelling?
This is an example of a logic error, an error that produces an incorrect result.
- 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.
(Question 6) What error message(s) do you get?
- No beginning quotation mark in a string literal.
Put the ending quotation mark back, then take out the
beginning one. Save and recompile.
(Question 7) How many errors this time?
- No semicolon after a statement. Now
remove the semicolon at the end of the line that prints the
message. Save the program and recompile it.
(Question 8) What error message(s) do you get?
Part 4: Declaration and assignment statements.
We have seen declarations in our Scratch work as building a variable. Now we will build our variable using the Java language. All variables must be declared prior to their first use in the program. A declaration is an abstract data type followed by an identifier followed by a semi-colon. For example, int sum; declares the variable sum to be an int (short for integer). Add the following line to the program prior to the System.out.println statement.
String message;
This statement declares message to be a variable container that can hold Strings. Note that String must be capitalized. Skip one line (make one line of white space) and add an assignment statement. This statement will put the String literal “Hello, World” into the container called message.
message = “Hello, World”;
You may use a different string if you prefer for your message.
Finally, change: System.out.println ("Hello,
World!"); To: System.out.println(message);
This change will print the contents of message. Save and
recompile your program. Then run it to make sure that the
message prints in the way that you want it to.Part 5: Manipulating output.
System.out.println sends a string to standard output and adds a
new line character at the end of the line. What happens if
we use System.out.print? This
section will let you manipulate the output.- In your program, add in a second String variable
declaration for message2.
Assign it the String value "I'm
happy to be a programmer."
- Change your println
to a print command: System.out.print(...); Then add
another print command: System.out.println(message2).
Compile and run your program.
(Question 9) How many lines of output do you get?
- After the word, "world!", in message,
put in the symbol "\n". This
is one of the "escape characters". Recompile and run
your program.
(Question 10) How many lines of output do you get?
- The \n is the newline
character and can be used to force a new line wherever we
want it. In this case, it is doing the job that the println did before, adding a new line
after the last character prints.
- Finally, remove the second println
command which is printing the second line of the message.
Change the other print command to read: System.out.println(message + message2);
- Recompile and run the program.
(Question 11) What output do you get?
- The "+" symbol is our first operator. Applied to Strings, it performs a concatenation of those two Strings. We will see in later labs that this is also the operator that we use to add two numbers together.
Part 6: Submit your work
- Upload two files: Hello.java and your completed worksheet. Do not upload your .class file, however.