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

Compiling Java Lab

Introduction

The goal for this lab is to gain experience editing, compiling and executing Java programs in the terminal. You may work on this lab individually or in a group of no more than three people.

Compiling and Executing Java in the Terminal

Each of the steps below should be completed entirely inside the terminal: no GUI applications allowed. Refer back to the Unix Tutorial for Beginners if you need to refresh yourself on the necessary commands.

  1. Create a folder inside your home directory named lab3.
  2. Copy the file /cs/shr/examples/code/Welcome.java into the lab3 directory.
  3. Use the cd command to move into the lab3 directory.
  4. Confirm that you completed the last two steps correctly by using the pwd command (to confirm that you are in the lab3 directory) and the ls command (to confirm that you successfully copied Welcome.java).
  5. Examine the contents of Welcome.java using the cat command.
  6. Compile Welcome.java:
    $ javac Welcome.java
    
  7. If all goes well, this command should not produce any output to the terminal window, but it should create a new file named Welcome.class.
  8. Examine the contents of Welcome.class using the cat command.

    Don't worry! The contents shouldn't make sense to you. They wouldn't make much sense to anyone. Why not?

    (Click for the answer.)

  9. Now that Welcome.java has been compiled, it can be executed:
    $ java Welcome
    
    Notice that the .class extension is not included.
  10. Congratulations! You've successfully compiled and executed your first Java program.

Editing Files in the Terminal

Normally, we will be using an Integrated Development Environment (IDE) to edit and compile Java programs. However, it can sometimes be convenient to edit a file directly in the terminal. There are many terminal-based editors. Today we'll try nano because it is easy to use for beginners.

  1. Open Welcome.java using nano:
    $ nano Welcome.java
    

    You should see something like the following:

    The two lines of text at the bottom show the set of actions available in the editor. The "^" symbol indicates the "Ctrl" key. For example, pressing Ctrl-O will "WriteOut" (save) any changes you have made to the file.

  2. Edit the file so that the welcome message says "It's REALLY fun." instead of "It's fun.". Save your changes and exit.
  3. Try executing your program again:
    $ java Welcome
    

    Does the output reflect your changes? Why not?

    (Click for the answer.)

  4. Execute your modified Java program.

Web-CAT Submission

We will be using Web-CAT this semester for automated submission and testing.

  1. Access https://webcat.cs.jmu.edu in a web browser.
  2. You should be able to log in using your eid and password.
  3. Use the "Browse..." button to select your modified Welcome.java file.
  4. If you are working with a partner, use the "Choose Partners..." button to add them to the submission.
  5. Click the button labeled "Upload Submission". After a few seconds you should be taken to a page labeled "Your Assignment Submission Results". Near the top of that page you should see a box that looks like the following:

    Congratulations! You have submitted successfully.

  6. If your submission didn't receive 10/10 points, take a few minutes to figure out what went wrong. The section labeled "Estimate of Problem Coverage" may provide some hints. If you find the problem, fix it and resubmit. If you can't find the problem, ask your instructor for help.

Fixing Syntax Errors

  1. Copy the file /cs/shr/examples/code/Personal.java into your lab3 directory.
  2. Compile Personal.java.
  3. You should see several error messages printed to the terminal:
    Personal.java:6: error: unclosed string literal
          System.out.println("Hello " + args[0] + "!);
                                                  ^
    Personal.java:6: error: ';' expected
          System.out.println("Hello " + args[0] + "!);
                                                      ^
    Personal.java:7: error: illegal start of expression
          System.out.println("Welcome to CS139.");
                ^
    Personal.java:7: error: ';' expected
          System.out.println("Welcome to CS139.");
                    ^
    4 errors
    
  4. Compiler-generated error messages often seem overwhelming and difficult to understand. Two rules of thumb can help:
    • Address errors in the order they appear. Fix the first error, then re-compile. In many cases the later errors aren't really errors at all; they occur because the compiler was confused by an earlier error.
    • Read the error messages carefully. The text of the error messages can be confusing, but they often contain useful information if you take the time to read them.
  5. Fix the errors and try out the resulting program. This program takes a single command-line argument specifying the name of the person who should be welcomed:
    $ java Personal Nathan
    Hello Nathan!
    Welcome to CS139.
    

If You Have Extra Time: Mad-Libs

Take a few minutes to read over Personal.java. This program uses some Java features we haven't covered yet: command line arguments and combining strings using the "+" operator.

Write a short Java Program that takes three command line arguments: a noun, an adjective and a verb. Your program should then produce a short Mad-Libs-style story using the provided words.

Executing your finished program should look something like the following:

$ java Madlibs CAT HAPPY SWIM
Bob was having a HAPPY day.  His CAT broke down on the way to work.
Now he will need to SWIM to make it up to his boss.