/**
 * CS139 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Spring 2016
 */

jGrasp Lab

Objectives

Key Terms

source file
the Java program as written by the programmer
class file
the executable program produced by the compiler
compile
process of checking syntax and producing a class file
syntax error
mistake in the source code that prevents compilation
logic error
mistake in the program that causes incorrect behavior
execute
the process of running a program on a computer

Part 1: Java Development Cycle

jGRASP is a text editor designed to simplify the process of editing, compiling and executing Java programs.

  1. Open jGRASP and click "File –> New –> Java" from the menu.

  2. 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!");
    }
    
    }
    
  3. Indentation doesn't matter to the Java compiler, but proper indentation makes code much easier for humans to read. Click the "Generate CSD" button generate CSD in the toolbar to indent the code and display a "Control Structure Diagram". Clicking the "Remove CSD" button remove CSD removes the diagram, but leaves the code properly indented. Try it out.
  4. Compile your Java program by clicking the compile 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.

  5. Execute your program from jGRASP by clicking the run 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.

Part 2: Syntax Errors

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.

  1. 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?

    Now compile your program. Keep the public class ello mistake in the code.

    (Question 2) What error message do you get during the compile?

  2. 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.

    (Question 3) What line number was your error on?

  3. Misspelling inside a string literal.

    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?

  4. No ending quote mark for a string literal.

    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?

  5. No beginning quote mark for a string literal.

    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?

  6. No semicolon after a statement.

    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!

Part 3: Declaration and Assignment

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 does message not have quotes around it?

Part 4: Manipulating Output

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.

  1. In your program, add in a second String variable named message2. Assign to it the value "I'm happy to be a programmer."

  2. 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?

  3. 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?

  4. 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.

  5. 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.

If You Have Time...

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.

Acknowledgments

This activity is based on a BlueJ lab originally developed by Chris Mayfield.