/** * CS139 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Spring 2016 */
Use an IDE (Integrated Development Environment).
Edit, save, compile, and run a simple Java program.
Recognize and correct syntax errors in a Java program.
jGRASP is a text editor designed to simplify the process of editing, compiling and executing Java programs.
Open jGRASP and click "File –> New –> Java" from the menu.
Type in the code below into the editor window.
Change the @author
to your name
and @version
to today's date. Pay attention to
all spelling, punctuation, and indentation.
/** * Hello world program. * * @author Nathan Sprague * @version 1/18/2016 */ public class Hello { public static void main(String[] args) { System.out.println("Welcome to CS139!"); } }
Compile your Java program by clicking the button on the toolbar.
If it compiles successfully you should see a message like the following under the "Compile Messages" tab:
----jGRASP exec: javac -g Hello.java ----jGRASP: operation complete.If you have other messages indicating errors, check your typing 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.
Execute your program from jGRASP by clicking the button on the toolbar. Under the "Run I/O" tab, you should see:
----jGRASP exec: java Hello Welcome to CS139! ----jGRASP: operation complete.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.
Download the Lab02B.txt worksheet and open/edit the file as plain text.
This part of the lab will give you some practice in reading and interpreting syntax errors. As you make each error, pay particular attention to the message produced, and in some cases, a single error will cascade several other errors. Record the answers to the following questions in your lab worksheet.
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?
Now compile your program. Keep the public class ello
mistake
in the code.
(Question 2) What error message do you get during the compile?
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.
(Question 3) What line number was your error on?
Correct the mistake above, save, and compile. Next, delete one letter 'l' from the Welcome in the message to be printed (inside the quotation marks). Save the program and recompile it.
(Question 4) Why is there no error message?
Now run the program, and review the "key terms" at the top of this lab.
(Question 5) What type of error just occurred?
Correct the spelling in the string, then delete the opening brace at the end of this line:
public static void main(String[] args) {Save the program and recompile it.
(Question 6) What error message(s) do you get?
Put the brace back, then remove the closing brace after the println
statement. Save
and recompile.
(Question 7) What was different about the errors this time?
Put the missing brace back, and 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?
Remember: A good practice to follow when you have multiple errors is to focus on the first error, correct it, then recompile. Do not try to figure out all of the errors at once!
Recall that a variable can be though of as a named box
that holds data. In Java, a variable declaration creates a
variable and gives it a name. All variables must be declared prior to
their first use in the program. A variable declaration consists of a
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 that can hold
a String
. Note that String
must be capitalized. Skip one line (make one line of
white space) and add an assignment statement. This statement will store the String
literal "Hello, World" into the variable named message.
message = "Hello, World";
You may use a different string for your message if you prefer.
Finally, change the println
statement so that it
prints the contents of message
instead of a String
literal:
System.out.println(message);
Save and recompile your program. Then run it to make sure that the message prints the way that you want it to.
(Question 9) Why doesmessage
not have quotes around it?
System.out.println
sends a string to standard output and adds a newline
character at the end. What happens if we use System.out.print
instead? This
section will have you experiment with the output.
In your program, add in a second String variable named message2
.
Assign to it the value "I'm happy to be a programmer."
Change your println
to use print
instead:
System.out.print(...);
Then add another println
statement on the next line:
System.out.println(message2);
Compile and run your program.
(Question 10) How many lines of output do you get?
After the word "World"
in message
, put in the code \n
.
This is one of the escape characters in Java. Recompile and run
your program.
(Question 11) How many lines of output do you get?
In Java, \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 newline after the last character.
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 12) What output do you get?
Submit both your completed Lab02B.txt and Hello.java via canvas.jmu.edu by the end of the day.
For those who finish before the end of the lab period, I have an extra challenge for you. Write a program named Miles.java that converts miles to kilometers (i.e., 1 mile = 1.60934 km). You may use the example code in yesterday's slides as a starting point.
This activity is based on a BlueJ lab originally developed by Chris Mayfield.