You are on page 1of 11

CSCI 455: Lab 1

1 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

CS 455 Lab 1: Using the editor and compiler


Fall 2015 [Bono]

Goals and Background


This lab will give you practice using the Unix operating system, as well as editing, compiling, and running
Java programs using emacs and javac, and dealing with compile errors in your programs.
Resources:

Horstmann, Ch. 1
Unix and Emacs tutorials and documentation (also linked from the Documentation link on the course
web page)

General information
You will earn up to 4 "checkoff points" in this lab section by showing your lab t.a. that you have successfully
completed the various tasks in the assignment. In general, it is better to get each point checked-off as you go,
rather than waiting until the end of the lab. Check-offs will not be done outside of lab for non-DEN students.
Lab pairs
Labs will generally be done working in partnerships (2 people). This is a version of the programming
technique called Pair Programming. The pair of you will sit at one workstation. One person will be the
designated driver, doing the typing and mousing to use the tools, and the other will be the scribe, writing
down the answers to lab questions. In subsequent labs you will switch off roles. This does not mean only one
person is doing the work. In pair programming both members of the team are active participants in solving
the problems, discussing as you go, but with each with a designated role. For this lab, if one of you has more
experience with Unix or Linux and emacs, we recommend the lesser experienced of you is the driver this
time.
DEN Students
DEN students will generally not be working in pairs since they are working remotely and away from other
students in the class. DEN students will need to electronically submit their labs. Because of this their answers
to the lab questions will go in the README file, rather than on paper. Each lab includes instructions about what
DEN students need to submit, and how (for example, see the end of this lab).
For DEN students that are new to unix and emacs, for this first lab we recommend that you write your
answers for Exercise 1 on paper, and then type them into the README later (editing the README is
discussed in Exercise 3).

Getting Started

10/6/2015 6:39 PM

CSCI 455: Lab 1

2 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

If you are new to the computing environment at USC you will want to make sure you have everything set up
properly. We outline the steps here, and provide links to USC Information Technology Services (ITS) pages
related to this.

Both members of the partnership should do these steps to make sure they can successfully get onto their Unix
account.
Step 1 Firstlogin.

You already have an account on SCF (student computing facility), the file system used by aludra, and can log
in using the same USC loginid and password you use to access the USC mail system.
You probably have already done this, but if you have never used your USC email account, which is also your
login for the Unix account you will use in this course, you will need need to do this step one time only to
activate your account (i.e., if you already have done it, you don't need to do it again here):
Link to ITS Firstlogin page

Note: the results of this setup could take up to an hour to take effect, so it's best to do firstlogin well before
your first lab session.
Step 2 Connecting to aludra.

This and all other labs assume you are starting out either remotely logged into aludra (a Unix time-share
machine) from a Mac running Mac OS X (using x11 or XQuartz) or from a PC or Mac running Windows
(using a program such as X-Win32 or putty).

If you are in the classroom using a lab machine, you should make sure the lab machine is booted with Mac
OS (not Windows). To connect from a Mac running OS X, you'll use a program that comes with Macs called
X11 (or the equivalent XQuartz). (Note: The windowing system on Unix is called X Windows, or X11.) ITS
has documentation on using x11. But we'll give you a summary here.
1. For lab machines running OS X you can find the icon for X11 in the Applications folder and the
Internet Tools subfolder. (On your own machine, it can be found in the Utilities subfolder of the
Applications folder.) Start the application. An interactive command prompt will come up (this is an
xterm window local to the Mac).
2. Whether you are running X11 or XQuartz, at the command prompt type
ssh -Y yourusername@aludra.usc.edu

such that you substitute your own USC loginid for yourusername in the above command. This
connects you to aludra.

Connecting to aludra from off-campus or from a non-USC Mac/PC: For DEN students doing lab work or
programming assignments, or for other students programming for this course off campus (e.g., for
programming assignments), how you do so depends on whether you are running MacOS or Windows on your
local machine:

From a Mac. You will first need to download and install XQuartz (available at
xquartz.macosforge.org). (Versions of OSX before 10.8 (Mountain Lion) come with X11 installed; so if
10/6/2015 6:39 PM

CSCI 455: Lab 1

3 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

that's you, you won't need to do the download.) Once you install it, XQuartz will be in a different
location than on the USC lab machines: you should be able to find it in the Utilities subfolder of the
Applications folder. Then you will be able to follow the directions given above for students working in
the lab.
From a Windows machine. The CS 455 documentation page (also linked at the beginning of this
document) has more information about installing and using x-win32 to connect to aludra from a
Windows machine (the first section, entitled "Remote access to aludra").

What is an xterm? That's the program you're running on the remote machine (aludra), but that is getting
displayed to the monitor on the Mac or PC in front of you. It's a terminal window, that's running a Unix shell.
The Unix shell is a command-based interface for controlling the Unix operating system. If you have ever
used DOS and the DOS prompt, it's similar to that.
Later, when we run other programs such as emacs, or java programs we create, the programs will be running
remotely on aludra, but will be displayed in windows locally. (Java programs with a GUI will run in their
own windows -- today we'll stick to text-based Java programs that run in the shell window.)
Here is a screenshot of an xterm window running a Unix shell on aludra: [This one shows me connecting to
the csci455 account using xquartz on my Mac. The first few lines won't be there if you are connecting using
x-win32 on a Windows machine.]

Exercise 1 (1 checkoff point)


In this part of the lab you are going to play around a little bit with Unix to familiarize yourselves with some
commands and the file system. We are going to ask some questions here, and you'll write down the answers
on a piece of paper, or a lab notebook if you have one. The questions to be answered by the scribe are in bold
and labeled Question 1.1, 1.2, etc.

10/6/2015 6:39 PM

CSCI 455: Lab 1

4 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

When you log into Unix (which you should have done as described in Step 2, above) you start out in your
home directory (directory is the rough equivalent to a Windows folder). To see what directory you are in type
the command
pwd

This is short for Print Working Directory.


Question 1.1. What was the result of the command?
Unix has a file system that's organized like a hierarchy or tree with one root. (This is similar to the folder
structure on Windows.) The root of the tree is called "/". The results of pwd showed the absolute path from
the root to your home directory.
To look at all the files in a directory you use the command ls. Try it now.

You probably have no files in the directory yet. If you use a different form of the command, it will also show
you some hidden files, that generally start with a ".", and it will also give you more information about the
files, such as their sizes and who can access them:
ls -als

Note: By convention, Unix program options are given with a "-" prefix.
Question 1.2. What files or directories were listed? (The names are on the far right.) Just list the first four
items if there were more than that.
Two of the files in the output should have been named .login and .cshrc. These are profile files that direct
how Unix should be customized for you. Sometimes you will have occasion to modify them but we're not
going to do so now.
Now you're going to make a new directory for our lab inside the current directory:

mkdir lab1

To change the current directory we use the command cd. We don't have to use absolute pathnames when we
are changing directories, but can just use a path relative to the current directory. Let's change our current
directory to be the new directory we made:

cd lab1

Question 1.3. Predict what the results of the pwd command would be at this point.
For any Unix command that takes a directory or filename as an argument we can identify that argument with
a relative path instead of an absolute path. That is a path that's relative to your current directory. For example,
if you're in your home directory, the following command will print out the contents of the "lab1" directory:
ls lab1

There are also shorthands for some particular paths (some relative). Here are a few of them:
10/6/2015 6:39 PM

CSCI 455: Lab 1

5 of 11

.
..
~

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

the current directory


the directory one level up from the current directory
your home directory

~foobar
SCF user with loginid foobar's home directory
So, here it looks like we can access anything on the file system, but really we only can access files and
directories that have permissions set so we can read them. (The ITS Unix documentation covers permissions
in more detail on this page.)
Question 1.4. Assuming you are still in the lab1 directory, write down three different commands that will get
you to your home directory from there (i.e., will make your home directory your current directory). You can
test them out by going back to the lab1 directory after trying each one.
These tricks are useful for navigating around in general, but there's actually another special shortcut for going
to your home directory:
cd

(i.e., cd with no arguments).


Question 1.5. Make sure you are in your home directory. Write down the command to list what's in the
directory one level up from your home directory. Try it.
If you get more than a screenful of output from some Unix program it just goes scrolling on by. We can get a
screenful at a time by "piping" the output to the more program. Pipes are a generally powerful tool for
connecting the output of one program to be the input of another program. Here's a command to get the file
listing one screenful at a time (the vertical bar key is what's used below to make the pipe).

ls .. | more

When running the more program, to advance a screenful use the space-bar. If you want to get out of the
program before you get to all the output type q. Try the command shown above. The listing is showing the
home directories of other users, although only the ones that are on the same disk (e.g., scf-03).

Sometimes a program will have an infinite loop, or will otherwise take longer than you are willing to wait to
produce results. To abort such a program you type Ctrl-c. Try running
ls ..

and aborting it before it finishes.

10/6/2015 6:39 PM

CSCI 455: Lab 1

6 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

The course account, csci455 is just another one of the SCF accounts. Using the relative path tricks shown
earlier, we can list the files in the lab directory on the course account for this lab as follows (try it):
ls ~csci455/labs/lab1

To make a copy of a file, we use the command, cp, used as follows:

cp fromFile toFile

Similarly, mv (move) renames a file, and rm (only has one argument) removes a file.

Question 1.6. Assuming you don't know what directory you are starting from, write down a sequence of
commands such that after they are done, the lab1 directory on your account has a copy of the file
Hello.java from lab1 directory on the course account (the path for the latter was shown above). Try out
your command sequence. Use ls to check if it worked.
To look at the contents of a text file we will often use a text editor. But if we want to look at a file quickly
(especially if it's a small file), we can use the cat command (so called because we can also use it to
concatenate files together). Or if we want a screenful at a time, we can use more (without a pipe). Try the
following:
cat Hello.java
more Hello.java

Now, let's organize our work for this class a little better.
Question 1.7. Write down (and execute) the command(s) to make a directory in your home directory called
This is where you'll keep all your work for this course.

cs455.

Hint: when you are done with this task, doing "ls" of the home directory should show:
cs455

lab1

(plus any files you might have have in there if you have used your Unix account before today)

You'll want a separate directory for each assignment and lab for the course. You can move your alreadycreated lab1 directory into this new directory using the mv command as follows:
cd
mv lab1 cs455

make sure we are in home dir

Once you do this, you should see the lab file you copied earlier listed by doing:

ls cs455/lab1

Note: when you use mv or cp with an existing directory as the second argument, it moves or copies the first
argument into the second one such that the first one has the same name as before the command. (The first
argument can't be a directory for this to work with cp.) Here are some other examples of this:

cd cs455
cp ~csci455/labs/lab1/Hello.java lab1
cp lab1/Hello.java .

does same thing we did in Qn 1.6

10/6/2015 6:39 PM

CSCI 455: Lab 1

7 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

Question 1.8. What does the last "cp" command above do?
Question 1.9. Assuming you are starting from your home directory write down and execute a single
command that will get us into our lab1 directory.
Once you are in the lab1 directory, do the following command:
cd ../..

Question 1.10. What directory did you just change to?


Question 1.11. "cd" into the lab1 directory. Using relative path names as seen in the previous examples,
write down and execute a single command to make a directory called foo that's also inside the cs455
directory (but that is not a subdirectory of lab1. By "a single command", we mean you will still be in the
same directory (lab1) after making the new directory. (When you are done a listing of the cs455 directory
should show both lab1 and foo.)
Question 1.12. Write down and execute a single command to get into the new "foo" directory.
Question 1.13. Write down and execute a single command to list the files in the "lab1" directory (i.e.,
without changing directories).
While "rm" removes a file, you need to use "rmdir" to remove a directory. Use this command to remove the
"foo" directory.

Exercise 2 (1 checkoff point)


A note on partnerships for Exercises 2 & 3: You may each want to do these two exercises, or take turns
working at the same computer if it's the first time you are using emacs. You'll want to make sure that each of
you can do things such as work with the mini-buffer, and do some of the weirder keyboard sequences. You
may still collaborate on a single answer, but this is a good opportunity to get help (from the lab TA or your
partner) if you get stuck.
1. Make sure you are in the lab1 directory you created in the previous exercise.

2. Load the source code file Hello.java into emacs (you should have a copy of this file inside of lab1 as
part of doing Exercise 1). If you don't know how to do this, one way is to start up emacs with the file
loaded. The ampersand below is part of the command, and it means that emacs will start up in a
different window, so you can still interact with your shell window.
emacs Hello.java &

This is the traditional first program, that prints "Hello world!"

3. Compile the program within emacs by doing

10/6/2015 6:39 PM

CSCI 455: Lab 1

8 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html
M-x compile

(M-x stands for "meta"-x. If you have an "Alt" key on your keyboard, you can perform it by pressing
the Alt key at the same time as "x". If you don't have an Alt key, you can use the sequence ESC-x,
where the ESC key is often at the upper left corner of your keyboard. All the other "meta" emacs
commands work the same way. )
Alternatively, you can use the mouse to accomplish the same thing by choosing "Compile..." on the
"Tools" menu.

The default command emacs offers to execute for compilation is make -k (it shows this at the bottom
of the window). This assumes the existence of a "make file" that we do not have for this lab (we'll be
talking about them later in the semester). You can edit the "mini-buffer" at the bottom of the emacs
window that shows the compile command to use. Replace the make command with the following
command (we'll show you how to do this presently if you don't know already):
javac Hello.java

To delete the old compile command you can use the backspace key, or you can try the shortcut
described in the next paragraph, that also works for editing files. (I.e., not only in the mini-buffer.)

First, many commands in emacs involve the control key. In Unix documentation here and other places,
when we say something like C-x (or alternately Ctrl-x), it means to hold the control key down while
you are typing an x. For Mac users, this is the real control key: the one labeled "Ctrl" or "control" (not
the Command key).
1. C-a Go to the beginning of the current line
2. C-d Delete the rest of the current line
3. Type in the desired text.

4. The program should successfully compile and you'll get a message like "Compilation finished at..."
The compiled version of the program gets put in a file called Hello.class. Using either an emacs shell
(M-x shell) or the shell window you already have open, execute the program by typing in
java Hello

at the Unix command line.

Note: if you ever initiate a command in emacs that requires arguments in the mini-buffer, such as the compile
command we just saw or the visit-file command we'll see in the next exercise, you can cancel the command
with C-g if you change your mind about doing it. C-g also causes a beep.

Exercise 3 (1 checkoff point)

A note on partnerships for this exercise: You may each want to do this exercise, as discussed at the beginning
of exercise 2.

A common beginner mistake is to leave out a semi-colon. Edit out the semi-colon on the statement that prints
out "Hello world!", save your changes (C-x C-s), and compile again from inside emacs (it should show the
correct compile command now). Leave the *compilation* buffer output displayed, and follow the directions
in the next paragraph.
10/6/2015 6:39 PM

CSCI 455: Lab 1

9 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

One advantage to compiling from inside emacs is you can have it go right to the line where it found the error
in the edit buffer for the program so you can change it right away. Use the control sequence C-x ` (control-X
backquote) to jump directly to error found by javac.
Note the error message produced, and where it finds the error. Sometimes error messages can be difficult to
interpret, and they don't always occur exactly where we might have made a typo or other error.

To get a little more practice with emacs here we're going to have you answer the next two questions in a
README file rather than on paper. Label each answer with the question number. After the questions below are
some directions on how to accomplish it in emacs.
Question 3.1. Copy and paste the text of the compiler error message into the README file.
Question 3.2. Where does it report the error occurring in relation to the actual location of the missing
semi-colon?
How to do this in emacs: Right now you should have two "windows" open in emacs (actually two panels in
one Windows window). One is labeled *compilation* and contains the output of the compile command, and
the other is the Hello.java buffer. Make sure the cursor is currently in the window for Hello.java (to
switch where the cursor is, type C-x o). Type
C-x C-f README

and it will load README, showing it in the window in place of Hello.java. (Hello.java is still in emacs, it's
just not being displayed currently.) You can cut and paste between your *compilation* window and the
window displaying README. (The Edit menu has the cut and paste commands.)

When you complete the error message and answer to the question save your changes to README (C-x C-s).
More about viewing multiple buffers at once in emacs: In emacs you can have multiple "windows"
displayed, all within the same Sun or Windows window. We also have the idea of buffers, which are the files
you currently have open in emacs (or I/O of programs you can run in emacs like your compilation output, or
a Unix shell), although you may not necessarily be viewing all of them simultaneously. Here are a few useful
Emacs commands related to this:

split the window into two windows.


C-x C-f open up a new or existing file in an emacs window (then type ? and you can see a list of files
to choose from)
C-x o put the cursor in the other displayed window
C-x 1 unsplit screen
C-x C-b display a list of the buffers (files currently open in emacs)
C-x b display the buffer named (you will be prompted in the mini-buffer)
C-x 2

Make it so Hello.java is displayed in emacs again (C-x b command shown above). Keep the missing
semi-colon, and add another mistake: take out the second " (i.e., the double-quote after Hello world!).
Compile again, and use C-x ` again, but this time you will be able to use it more than once to go from error to
error in the program. Note the error message given for this new mistake, but you don't have to put it in the
10/6/2015 6:39 PM

CSCI 455: Lab 1

10 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

README file. Once you have done this, take out the errors in the program and compile and run the corrected
version.

Exercise 4 (1 checkoff point)


In this exercise you are going to type in a program from scratch, although you can look at Hello.java as a
pattern to follow. You could cut and paste that program as a starting point, but we recommend you type the
whole thing instead to get used to the pieces that go in every Java program; so it soon becomes automatic for
you, and you don't have to be looking at one to write one.

Create a file called Name.java (C-x C-f command in emacs will create it and load it). The program you write
must generate output similar to the following:
My name is Joe Blow.
My major is underwater basket-weaving.
I am a PhD student.

Your program output must be formatted as shown (i.e., 3 lines of output), but it will display your own name
and information instead of that for Joe Blow.

Hint: The name of the class containing the main method must match the prefix of the filename containing that
class; for example, Hello.java contained class Hello.
If you get compile errors along the way, write down a summary of what the error message said and what
caused the error. You can put this in your lab notebook or add it to your README file as you did with the
error from Hello.java (DEN students: put it in the README). Keeping a journal of error messages and
their causes can be useful so that when you get the same error (possibly much) later, you don't have to figure
out what caused it the second time around.

Summary for checkoff


Show your lab t.a. your answers for exercise 1, a run of Hello, and your answers from exercise 3, your error
messages and causes from part 3, and a working Names.java program.

Checkoff for DEN students only

Make sure you put your name, loginid, course, and assignment (e.g., lab1) in all the files to be submitted
(README and Name.java). For Java source code files, such as Name.java you will need to put this information
in one or more comments at the beginning of the file, e.g.,
// Name: Tommy Trojan
// loginid: ttrojan
// cs455, lab1
public class . . .

When you are all done with the lab, you can turn it in by typing in the following Unix command exactly as
you see it below. The last two arguments are the names of the files to submit, so it will only work if you are
in the directory that has those files.

submit -user csci455 -tag lab1 README Name.java

10/6/2015 6:39 PM

CSCI 455: Lab 1

11 of 11

http://scf.usc.edu/~csci455/curr/labs/lab1/lab1.html

DEN students must submit this (and all labs) by 11:59pm on Sunday Pacific Time for the Sunday that occurs
just after the lab sessions for that week (e.g., if labs are on Thur, 8/27 and Fri, 8/28, your final deadline is
Sun, 8/30). The deadline is delayed to give DEN student working full time more flexibility in when they do
the lab. However, if possible we recommend you try to do it during the weekdays (well before the deadline)
when course staff will be more available to help you by phone or email if necessary.
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy,
integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such
content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees

10/6/2015 6:39 PM

You might also like