The purpose of this lab is to introduce you to the Unix computers in the Computer Science Labs and to provide you with an opportunity to write, compile and execute some simple Java programs. Each section of this lab will introduce a new Unix or Java command and walk you through an example of how to use that command. Please read and follow the instructions carefully. If you get stuck, ask the instructor or one of the TA's for assistance. Sprinkled throughout this lab are exercises designed to give you practice with these command and to test your understanding of their use. Your grade for this lab will be based on your written answers to these exercises.
An account has been created for each of you on the CS lab computers. You log into your account by providing your username and password. The username for your account is the same as your username for the campus e-mail system. The initial password for your account has been provided by your instructor. If you have taken other CS courses in the past, you should already know your password. If you have forgotten your password see the instructor.
Most of the work on computer which use a Unix operating system is done using a terminal window. The terminal window allows you to type instructions to the computer. Many of these instructions perform actions that are similar to what you might do with a mouse in other operating systems.
Open a terminal window by clicking on the terminal icon that appears at the bottom of the screen. The icon looks like this:
Clicking on the terminal icon will open a terminal window on your screen that should look similar to this:
The terminal window contains a command prompt. Instructions to the computer can be entered at the command prompt. In the terminal window above, the command prompt reads: geneva:~> . The name of the machine that you are logged into appears in the command prompt. Therefore, your command prompt will probably contain a different name.
The password you entered to log into your account is the key to your account. It is what allows you, and no one else to access your files. Therefore, you will want to pick a password that you can remember but is difficult for anyone else to guess. It is also a good idea to change your password periodically (maybe once a semester). If this is the first time you have used your account you will want to change your password immediately. If you have changed your password in the past you might want to change it again or you can skip to the next section of the tutorial.
To change your password enter the yppasswd command at the command prompt in the terminal window and press the The new password that you select must be a combination of uppercase and lowercase letters and must also include at least 1 number or punctuation mark. This makes it much harder for someone else to guess your password.
If you make a mistake typing your old password, yppasswd will exit and your password will not be changed. Similarly, if you do not select an appropriate password or err when retyping your new password, yppasswd will also exit without changing your password.
Once you have successfully changed your password you will need to use the new password when logging into your account in the future.
In this class we will be using the emacs text editor. It takes a little bit of work to learn how to use emacs. However, emacs is a very powerful editor and has many features that will make it easier to write programs.
Setting up emacs
Before starting emacs for the first time, enter the command:
Note that in the above command, there is not a space before the first period but there is a space before the second period. If you receive an error message please see a TA or the instructor before continuing. This command configures emacs to use some Java specific tools that will be helpful in this class. Note that it is only necessary to use the command once. Therefore, you will never need to enter this command again, so don't worry too much about understanding it.
Starting emacs
Start emacs by typing the emacs & command at the command prompt and pressing the Exiting emacs
Exit emacs by clicking on the Files menu and choosing the Exit Emacs option. The emacs window will disappear from the screen. Note that you can also click on the X in the upper right hand corner of the window to close emacs.
Exercise 1: Explain what happens if you start emacs but forget the & at the end of the command? To figure this out go back to the terminal window and start emacs without the Creating a New File
In emacs you create a new file by clicking on Files menu and choosing the Open File... option. When you do this the emacs window will change. In particular notice the text that appears at the very bottom of the window. In the figure above it reads For information about the GNU Project and its goals type C-h C-p. After you choose Open File... it will read: Find File: ~/ . This line of text at the very bottom of the emacs window is called the mini-buffer.
At this point, the cursor will be positioned following ~/ in the mini-buffer. Type JavaRules.java into the mini-buffer. The mini-buffer should now contain: Find File: ~/JavaRules.java Pressing enter will change the contents of the mini-buffer to (New file) indicating that emacs just created a new file. The main area of the emacs window, called the buffer, will now be blank. Notice that the line just above the mini-buffer (white text on black background) shows the name of the file currently displayed in the buffer.
Type the following program into the emacs buffer:
You should notice that as you enter this program emacs color codes parts of the program and automatically indents new lines for you. These emacs feature will help you to write well formatted programs that are easy to read.
Saving a File
After you have entered the above program into the emacs buffer you need to save the changes to your file. Save the changes by clicking on the Files menu and choosing the Save Buffer option. When you do this the mini-buffer will display Wrote /export/home2/stu/yourname/JavaRules.java indicating that emacs saved the contents of the buffer into the file. You may continue editing after the file is saved. You should save your files frequently to prevent losing your work due to a computer glitch or power failure.
What is /export/home2/stu/yourname/JavaRules.java? Each name that appears between a pair of Closing a File
Closing a file in emacs is a rather violent event called "killing the buffer." Close the Opening an Existing File
The process for opening an existing file in emacs is identical to that for creating a new file. Click on the Files menu and choose the Open File... option. The mini-buffer will again display something like: Find File: ~/ . If you type the name of an existing file at the end of the mini-buffer, emacs will open that file. Open the Exercise 2: Try to open Executing the programs you write in this class will require two steps. The first step is to Compile the the Java source code program. Compiling translates the program from Java source code into Java Byte-Codes which are the machine language for the Java Virtual Machine. The second step is to execute the Java Virtual Machine which interprets and executes the Java byte-codes.
Compiling Programs
To compile your program type type the command javac JavaRules.java at the command prompt. If you entered the program correctly the computer will pause for a second and then return another command prompt:
Running a Program
To run your java program enter the command java JavaRules at the command prompt. The computer will load the Java Virtual Machine and provide your program to it as input. The Java Virtual Machine will then execute your program and you will see the following output:
Notice that you do not include the Exercise 3: It is a very common mistake to include Dealing with Compiler Errors
Restart the emacs program, if it is not already running, and open the Save the changes to this file. From the terminal window compile this new version of the From this error message it is possible to see that the error occured in Exercise 4: Does emacs display the line number on which the cursor is currently located? If so where is this information displayed?
Exercise 5: The Java language is case sensitive. This means that capitalization matters. So if you try to compile a program with incorrect capitalization the compiler will report errors. What error message is reported if the programmer incorrectly types Exercise 6: Not all error messages are so easy to interpret. The following version of the What error messages are displayed when this program is compiled? Are there really two errors in this program? Explain. Hint: A Exercise 6: In all of the Java programs that we write, the name of the class must match the name of the file in which it is saved. For example, the class We saw above that emacs saved your Listing Your Files
To see a list of the files currently stored in your home directory, enter ls at the command prompt. If this is the first time you have used your account the ls command output will look something like:
In order to keep things organized in your home directory Unix provides several commands which allow you to manipulate directories and files. We'll see commands that let you create or delete directories within your home directory and copy or delete files.
Creating New Directories
Clearly if you were to save all of your files in your home directory, it would become quite cluttered. In this class you will store all of your files in a sub-directory named Exercise 8:In the output of an Copying Files
In Unix you can make a copy of a file using the It is also possible to copy a file from one directory into another. Enter the command Exercise 9:Copy your Exercise 10:Are filenames case sensitive in Unix? Explain how you determined this.
Changing into Sub-Directories
It is most conveninent to perform operations on files, such as compiling, editing or executing, when you are in the directory containing those files. For example, to operate on files in the Notice that the command prompt has also changed to indicate that you are currently in the Exercise 11: Create a new sub-directory inside your Changing into Super-Directories
Once you change into a sub-directory you will often need to return to the super-directory. For example, after you changed into your Using Path Names
Sometimes you will want to change into a directory which may be inside several other directories. For example, you may want to change from your home directory into the It is also possible to combine these two Similarly, you can return to your home directory using two Or you can specify the path to your home directory from the Exercise 12: Are directory names case sensitive in Unix? Explain how you determined this.
Exercise 13: Change into your Exercise 14: What does the command Removing Files
You will frequently need to remove old or unnecessary files from your directories. For example, the Wild-Cards
Sometimes you will want to remove or copy a large number of files. In these cases it is often convenient to use a wild-card character in the filename. For example, enter the command Exercise 15: Move the Removing Directories
In order to remove an unwanted directory in Unix you must use the Exercise 16: What happens if you try to use Exercise 17: A common mistake in Unix is to try to use the The emacs editor has many more features than were introduced above. We will see a few more of emacs' features in this section. Before continuing start the emacs editor again if it is not already running.
Directory Listings
By asking very nicely you can get emacs to display a list of the files that are contained in a directory. You can then select a file from the directory list for emacs to open. Click on the emacs File menu and select the Open Directory... option. The emacs mini-buffer will display something like: Dired (directory): /export/home2/stu/yourname/. Press the Opening Multiple Files
You will often find it convenient to have more than one file open in emacs at a time. Click on the Buffers menu to see a list of all of the emacs buffers that are currently open and the files that they contain. You will see an entry on this menu for your home directory, your cs132 directory, your lab1 directory and the Switching Between Files
To switch between open files in emacs, click the Buffers menu and select the file to display from the menu.
When you are finished using one of the lab computers you should always log out. To log out of the computer click on the small X icon that appears at the bottom of your screen.
Exercise 18: Solve Programming Exercise 1.1. Write your program in a class named Bonus Exercise: Solve Programming Exercise 1.2. Save your program in a file named
1. Logging In:
2. Opening a Terminal:
3. Changing Your Password:
![]()

4. Editing a File:
Enter key. The text below shows the contents of the terminal window during the execution of a yppasswd command. The lines that end with a : indicate that yppasswd is waiting for you to enter the requested information.
5. Compiling and Running Programs:
Enter key. Be sure to include the & at the end of the command. When you enter the emacs & command your terminal window will display a line with several numbers and then the command prompt will reappear:
In another few moments the emacs window will appear and will look something like this:

&. Then try to start a second copy of emacs without exiting the first! In the end you can return to the command prompt by closing the emacs window.
/ is the name of a directory (i.e. folder) on the computer science file server. So when emacs says that it Wrote /export/home2/stu/yourname/JavaRules.java, it means that the file named JavaRules.java was saved into a directory named yourname that is inside a directory named stu and so forth. Every computer science student has his/her own directory inside the stu/ directory.
JavaRules.java file by clicking on the Files menu and choosing the Kill Current Buffer option.
JavaRules.java file by entering its name in the mini-buffer.
JavaRules.java but make an intentional mistake and mistype it as JavaDRules.java. What happens? Why does what happened make sense?
If an error message appears in the terminal window, go back to emacs and check that you entered the program correctly.
.java after the filename when running the program.
.java after a filename when trying to run the program. What happens if you enter the command java JavaRules.java?
JavaRules.java file. Often in editing a program you will make mistakes when entering statements. If these mistakes create a program that is gramatically incorrect, the compiler will report these errors and will not generate an executable program. Errors of this type are called syntax errors (or compile errors). The compiler will make an effort to inform you of the location and cause of any syntax errors in your program. As an example, make a change to the JavaRules.java file by deleting the semicolon following the line System.out.println("Java Rules in CS132!") as shown below:
JavaRules.java program. When you attempt to compile this program the compiler will respond with the error message:
JavaRules.java on line 5. It is also clear that the compiler expected to see a ';'. In this case the compiler is even kind enough to show you where the ';' belongs. Correct the program and save the changes.
6. Your Home Directory:
System.out.println("Java Rules in CS132!"); as System.out.Println("Java Rules in CS132!");? What do you think the compiler means when it says "cannot resolve symbol"?
JavaRules program has a missing quotation mark following CS132!.
String literal is any combination of characters between quotation marks, (e.g. "This is a String Literal"). A String literal starts with the first quotation mark and ends at the second quotation mark.
JavaRules was saved in the file JavaRules.java. In the JavaRules.java file change the name of the class to JavaDRules in the first line and save the file without changing its name. What error message is reported by the compiler when you try to compile the JavaRules.java program now?
7. Managing Directories and Files:
JavaRules.java file in a directory named /export/home2/stu/yourname. This directory is called your home directory. Each time you open a terminal window after logging into your account you will automatically be placed into your home directory. The command prompt indicates this by ending with :~> . In Unix the ~ is an abbreviation for your the full name of your home directory. The appearance of :~> in the command prompt indicates that you are currently in your home directory.
JavaRules.java is the file you created using emacs above. JavaRules.java~ file is an automatic backup of the JavaRules.java file that was created by emacs. JavaRules.class is the executable program that was created by the compiler. Note that if you have used your account before, the output you see may contain many more files. You should be able to recognize the names of some of these files from your previous courses.
8. More Emacs:
cs132 within your home directory. Enter the command mkdir cs132 at the command prompt. Using ls to list your files will show that you now have a directory named cs132 within your home directory.
ls command, how can you tell the difference between a file and a directory?
cp command. Enter the command cp JavaRules.java AnotherName.java at the command prompt. This command will make a copy of the file JavaRules.java and name it AnotherName.java. If you list the files in your home directory now you will see something like the following:
cp AnotherName.java cs132/ at the command prompt. This command makes a copy of the AnotherName.java file and stores it in the cs132 directory.
JavaRules.java and JavaRules.class files to your cs132 directory. List the commands that you used to copy these files.
cs132 directory it will be convenient to first change into that directory. The cd command is used to change directories in Unix. Use the command cd cs132 to change into your cs132 directory. Using the ls command now shows the files contained in your cs132 directory:
cs132 sub-directory of your home directory.
cs132 directory named lab1. List the commands that you used.
cs132 directory above you may find a need to return to your home directory. Unix defines the name .. (two periods) as a special name for the super-directory. Enter the command cd .. to change from the cs132 directory into the its super-directory (i.e. your home directory). Notice that the command prompt also changes:
lab1 directory that you created above. It is possible to change into your lab1 directory using two cd commands:
cd commands by specifying the path to the lab1 directory:
cd commands:
lab1 directory:
cs132 directory. Type the command cd lab1/.. at the command line. What happens? Explain.
cd ~ do? Explain how you determined this. (Hint: Try this command from a variety of directories.)
AnotherName.java, JavaRules.java and JavaRules.class files in your home directory. Since you copied them to your cs132 directory there is no need to keep a copy of them in your home directory. Change into your home directory and enter the command rm JavaRules.class at the command prompt. This command will remove (i.e. delete) the JavaRules.class file from your home directory.
rm *.java to delete both AnotherName.java and JavaRules.java. The * in the command rm *.java indicates that any file whose name ends with .java should be deleted.
AnotherName.java, JavaRules.java and JavaRules.class files from your cs132 directory into the new lab1 directory that you created earlier. Use a wild-card to copy/remove both .java files with one command. When you are finished the cs132 directory should contain only the lab1 directory. List the commands that you used.
rmdir command. For example, the command rmdir testDir would remove a directory named testDir. However, for safety the rmdir command will only remove an empty directory.
rmdir to remove a directory that is not empty?
rm command to remove a directory. What happens if you try to use rm to remove a directory?
9. Logging Out:
Enter key. Emacs will display a listing of all of the files in your home directory. Find the cs132 directory and click on it. After you click on the cs132 entry, press the Enter key. Emacs will now show a list of the files in your cs132 directory, which should contain only the lab1 directory at this point. Open the lab1 directory by clicking on it and pressing the Enter key. A list of the files contained in the lab1 directory will appear in the emacs buffer. Click on the JavaRules.java file in the lab1 directory and press Enter. The emacs buffer will display the contents of the JavaRules.java file.
JavaRules.java file you opened in the last section. Select the lab1 directory from the Buffers menu. The buffer will change back to the list of files in your lab1 directory. Open the AnotherName.java file from the lab1 directory.
10. Additional Exercises

A.1. Skills:
NameBox and save your program in a file named NameBox.java in your cs132/lab1 directory. Turn in a printout of your program as the answer to this exercise.
Tree.java in your cs132/lab1 directory. The information in Advanced Topic 1.1 will help you solve this problem. Turn in a printout of your program as the answer to this exercise.
Basic Unix Commands:
A.2. Vocabulary:
ls, mkdir, cd, rm, rmdir, cp
Using emacs
Compiling a Java program
Running a Java program
Interpreting a Java Compiler error message
Command Prompt
Home Directory
Mini-Buffer
Buffer
Sub-Directory
Syntax Error (Compiler Error)
Case Sensitive
Case Insensitive
String Literal