Lab 1 - Hello, World
Lab 1 -- Hello, World
Principles of Computer ScienceSpring, 2005
In our first lab, we will do a few simple exercises involving Java programs. The objectives of the lab are to:
- Learn how to log in to Linux and use a Linux shell.
- Learn how to compile and execute Java programs.
- Get started with emacs (a text editor on Linux).
- Investigate some of Java's error messages.
- Learn how to submit a lab using the department's handin program.
Part one. Hello, world.
Computer Science maintains a shared file system on a server called OCCS
. An account has been set up for you on OCCS , which gives you
disk space on the server. Your user ID is the first initial of your first
name followed by (up to) the first seven letters of your last name. Your
initial password was distributed in class; you will be changing it to something
you can remember during the course of this lab. Your account can be accessed
from any of the lab machines under either Linux or Windows XP. We'll be
using Linux for the labs in this course.
Start by logging in to Linux by typing your user ID and password in the login
window and hitting the Enter key. This should start up a Linux
window manager. There are several window managers that are available and it
doesn't really matter which you use for this course. The default for student
accounts is a window manager called GNOME, and this is probably what you will
see when you log in the first time.
You need to start up a "terminal", which is a window with a command
interpreter. In the GNOME manager you can get this from the System submenu of
the Applications menu at the top left of the screen. You can also get a terminal
by clicking the right mouse button in a blank portion of the screen. A window
should appear on the screen with a command prompt.
Just so you get used to this as a command interpreter, here are a few simple unix commands you might try:
cal: prints a calendar for the current month
cal <year>: prints a calendar for the given year
cal <month> <year>: prints a calendar for the given month and year
date: prints the current date and time
cookie: prints a random fortune cookiewhoami: prints your username
One thing you should do as soon as you first log into your account is to change your password. Select a new password that you can remember. It should have at least one digit or numerical character and be at least 6 characters long. You need to log onto occs, the computer science server, to change your password. First type ssh cs.oberlin.edu; the system will prompt you for your password, which is still the default password given to you. Once you are logged on, enter the command passwd. This will prompt you for your old password, then for your new password. As a protection against mistyping it will ask you to repeat the new password. Be sure to remember your password, for if you forget it you will not be able to log into the systems. Any of the faculty, or Nate Daniels, the system manager, can reset your password if you lose it. Nate's email address is "ned@cs.oberlin.edu" if you ever need to reach him. Once you have successfully changed your password you can log off occs by typing <CTRL-D> (i.e., hold down the "ctrl" key and the "d" key at the same time. You are now back on the local lab machine. You probably won't need to go back to occs for this course again unless you decide to change your password.
Now we will look at some unix commands you will need to do your work for this
course:
Enter the command pwd, which is Unix for "print working directory". This
will display the name of your "home" directory, the last part of which is your
account name. Your home directory is your private area on OCCS
where you can store the files you create for all your lab assignments.
Now, create a subdirectory of your home directory called "cs150". You
can use this directory to store all the work you do in this course. The
Unix command to create a directory is mkdir, so enter the command
mkdir cs150.
To verify that the subdirectory has been created, enter the command ls.
ls is the Unix command which is used to display the contents of
the current directory. You should see the name cs150.
Next, create a subdirectory of cs150 that you can use for today's lab. First
enter the command cd cs150 to change directory to the cs150 directory.
You can use pwd to verify that your current directory is now cs150.
Then enter the command mkdir lab1 to create a directory for your
work today, and use cd to make lab1 the current directory.
Note: cd directory_name is the Unix command
to change the current working directory. Whenever you log in to Unix,
the current directory is your home directory. You can use cd to
change to the directory that contains the files you want to work with. To
return to your home directory, just give the command cd with no arguments.
Altogether, the following sequence of commands will create cs150 and lab1 directories after you first log in:
mkdir cs150
cd cs150
mkdir lab1
cd lab1
Editing (i.e., creating, viewing, or modifying) files can be accomplished with
any text editor. One of the most popular text editors in the Unix world
is called emacs . The command emacs file_name
& (where file_name is the name of the file to be edited)
opens another window that enables you to edit the specified file. The &
at the end of the command makes the emacs window run in the background
(so you can still use your Console window). The specified file can be
a new file (in which case an empty file is opened up) or an existing file (in
which case the current contents of the file are displayed). Note that capitalization
is very important, both for Unix commands and filenames.
Now let's actually create a Java program. Type emacs HelloWorld.java
& to bring up an emacs window. Type in the following Java
program:
/*The emacs editor will automatically indent to the appropriate column after you press tab key on a new line. When you have finished typing in the Java program, save your program by selecting files -> save buffer, or by clicking the "save" icon at the top of the emacs window.. For more information on emacs commands, see the GNU Emacs Reference Card .
Hello,world program which writes to the console.
*/
public class HelloWorld
{
HelloWorld()
{
System.out.println("Hello, world");
}
public static void main(String args[])
{
new HelloWorld();
}
}
Now return to your shell window by clicking on it and then hit the return key. The Unix command for listing the files in the current directory is ls. Notice that the file HelloWorld.java that we just created is the only file in the current directory.
Now let's compile (or translate) the Java program into Java bytecodes. The Java compiler is invoked with the command javac file_name . Compiling .java files creates .class files which contain Java bytecode. Note that the Java compiler checks the program for syntax errors. If you did not type in the program correctly, you may see some error messages from the compiler. Don't be alarmed, just go back to the emacs window to make corrections. After correcting the program, save it again and return to the shell window, and recompile the program. Repeat this process until the program compiles without errors.
Go back to the shell window and enter the ls command. You should see listing of both the source file (HelloWorld.java) and the class file (HelloWorld.class).
Java bytecode is executed by invoking the Java Virtual Machine (JVM) using the command java class_name (where class_name is the name of the class, not the file -- it does not have an extension). When you run it, you should see the message "Hello, world" displayed on the command line.
Note: The Unix shell has a feature called tab completion which can save time while typing in commands. Tab completion enables you to type only what is necessary for the system to understand what you want. Whenever you press the Tab key while you are entering a command, the system will complete the command as best as it can with respect to the current environment. For example, there are several commands that start with j and even ja so if you type j or ja and then press the Tab key the system doesn't know which command you want. On the other hand, the only commands that start with jav are commands used by the Java Development Kit (JDK) and they all start with java so if you type jav and then press the Tab key the system will complete the command to be java. It is your responsibility to continue typing to further differentiate which JDK command you want. To invoke the compiler, you would then type a c. At this point, if you hit the space bar (so it knows the command is javac ) and then the Tab key the system will type in HelloWorld. since the current directory contains both HelloWorld.java and HelloWorld.class . Typing a j followed by a tab will complete the command.
Part two.
In this part of the lab, we will experiment with some of the different kinds of errors that can occur in a Java program, by deliberately introducing errors into a working program.
We will use a GUI-based version of the Hello, World program. You can copy the file (HelloWorld2.java) for this program by right-clicking on the hyperlink HelloWorld2.java and saving it in the lab1 subdirectory of the cs150 subdirectory of your home directory.
Compile and run HelloWorld2. The program should create a new window containing the words Hello, world.
Now bring up a blank file in emacs; call it results. (Enter emacs results .)
For each of the following nine tests, make the indicated change to the program, and then try to compile it and run it. Write down in your results file what error resulted, what (if any) error message was reported by Java, and whether the error occurred during compilation or execution of the program. (After trying each test case, return to the original program before going on to the next one. You might want to make a copy of HelloWorld2.java and make changes to the copy for each of the tests.)
1. Rename the .java file to be GoodbyeWorld2.java. (The Unix command to rename a file is mv, so you can do this by entering the command mv HelloWorld2.java GoodbyeWorld2.java.) (You'll find that Java requires the name of the class must match the name of the file in which it is defined.)
2-4. In each of the next three experiments, make a one-character change to the source file and see what happens. Some things you can try:
- Misspell a word.
- Insert an extra punctuation symbol, like ; { }
- Remove a punctuation symbol.
5. Remove the arguments "200,200" from the "setSize" method call.
6-9. Comment out each of the "set" method calls in lines 14-17. (A line can be made into a comment by inserting two slashes (//) at the beginning of the line.)
10. Remove the initializer from the String declaration; that is, replace the line
String message = "Hello, world";
with
String message;
This should result in a "null pointer checking error", which is one of the most common Java run-time errors.
Part three. Submitting your results.
The Computer Science program uses a program called handin to handle submission of labs and homeworks. It copies an entire directory to an area of the disk which is accessible to the instructor. You can use it to hand in the results of this lab as follows:
- Make sure that your cs150/lab1 directory contains the files HelloWorld.java, HelloWorld2.java, and results.
- Change to the cs150 directory and type handin.
- Enter the class number (150), the assignment number (lab1), and the file/directory name (lab1).
Congratulations! You've completed your first lab.