/** * CS139 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Spring 2016 */
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.
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.
lab3.
/cs/shr/examples/code/Welcome.java
into the lab3 directory.
cd command to move into
the lab3 directory.
pwd command (to confirm that you are in
the lab3 directory) and the ls command (to
confirm that you successfully copied Welcome.java).
Welcome.java using
the cat command.
Welcome.java:
$ javac Welcome.java
Welcome.class.
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.)
Welcome.java has been compiled, it can be executed:
$ java WelcomeNotice that the
.class extension is not included.
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.
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.
"It's REALLY fun."
instead of "It's fun.". Save your changes and exit.
$ java Welcome
Does the output reflect your changes? Why not?
(Click for the answer.)
We will be using Web-CAT this semester for automated submission and testing.
Welcome.java file.
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.
/cs/shr/examples/code/Personal.java
into your lab3 directory.
Personal.java.
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
$ java Personal Nathan Hello Nathan! Welcome to CS139.
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.