CPSC 124: Introductory Programming
Lab #1: Meet the Computer

ALTHOUGH MOST PEOPLE in CPSC 124 presumably have some experience with computers, most will be new to the operating system, Linux, and to the style of interaction, command-line, that will be used in the course. An operating system is the basic software that runs the computer and gives it its "personality." Most of you are probably familiar with one of the many Microsoft Windows operating systems or possibly with the Macintosh operating system. Linux is an advanced, free operating system that can be run on the same type of hardware as Windows. In a command-line interface, you interact with the computer by typing commands and waiting for the computer's response. In Windows, this type of interface is provided by a "DOS" window. In Linux, it is provided by a similar type of window known as a "Console" or "Terminal". The Console is only part of the Linux interface. You also get the same type of "GUI" (Graphical User Interface) that you get in Windows. This lab will introduce you to Linux and the command-line interface. It will also give you a first small taste of programming in Java.

Connecting with X-Win32

The Department of Mathematics and Computer Science has a computer lab in room Lansing 310 which has ten computers running the Linux operating system. These computers are named cslab0.hws.edu, cslab1.hws.edu, ..., cslab9.hws.edu. These are the computers on which you will actually do your work for the course. You are welcome to work in Lansing 310. However, the cslab computers can also be used remotely from other computers on the Hobart and William Smith campus network. They can be used remotely from a Microsoft Windows computer using a program called X-Win32. This program is installed on the computers in the computer labs in the Library and in Gulick Hall. It is also possible to install X-Win32 on your own computer, if it runs Windows and is connected to the campus network.

To use X-Win32 in one of the computer labs, you must first log on to the computer with your Windows network username and password. Once you have logged in, you can start up the X-Win32 program, which you should find in the "Programs" submenu of the "Start" menu. You should see a gray screen with a small window that lists the available computers. The list should include cslab0, cslab1, and so on. Select one of the cslab computers by double-clicking on it. (Note that the number of people currently using the computer is listed. Linux can be used by several people simultaneously. It's a good idea to choose a computer that doesn't already have a lot of users, since you will be sharing the computer's time with the other users.)

Logging on to the KDE Desktop

After selecting a computer, you should get a log-in screen that asks you to enter a user name and password. This is the user name and password for the Math/CS Linux system. The log-in screen that you see is the same one that you would see if you logged on to the computer directly in room Lansing 310, and for the most part, the computer will work the same whether you use it directly or remotely.

Enter your user name and password. Leave the pop-up menu below the user name set to "KDE" for now. (This menu lets you choose among various GUI's. You can experiment with them later if you want, but for now stick to KDE.) Press return, or click the "Go" button. Now, you will have to wait while KDE starts up. This takes a while, especially the first time you log in when the computer needs to set up your KDE desktop.

Eventually, you will see some icons on the KDE desktop. For example, there is an icon for starting the Konqueror WWW browser, a Web-browser that is part of KDE. You can click once on an icon to start the program. KDE, by default, uses single-clicks rather than double-clicks, although you can change this behavior if you like. (Note that the "Floppy" and "CD-ROM" icons are not functional when you use KDE remotely. They refer to the floppy and cd-rom drives on the computers in Lansing 310.)

Along the bottom of the desktop is the "panel". The left end of the panel looks something like this:

left end of KDE panel

The button marked with a "K" is the K menu, which is similar to the Start menu in Windows. It contains many programs and other commands. Next to it is a button that can be used to open a directory window that shows the contents of your "home directory". More about this later. Next to that is a button that can be used to open a Console window that you can use as a command-line interface. (The icon is a computer screen with a seashell. This is because a Linux command-line interface is also called a "shell.") You won't have all the buttons that are shown in the above image. You can add your own selection of buttons to your panel later if you want.

The panel also includes a section that looks something like this:

left end of KDE panel

The right section in this image shows a tab for each window that has been opened on the desktop. You can bring a window to the front by clicking its tab. To the left is the "pager" which lets you choose among four desktops. Linux provides multiple desktops to help you organize your work.

Take some time to familiarize yourself with the KDE desktop. Open some windows. Try a few games. Try clicking and right-clicking on various things. For more information about using Linux, you can look through the brief introduction at


Basic Commands

In Linux, most things can be done using either a command-line or a GUI interface. You are probably more comfortable with a GUI interface, but there are a few aspects of programming that require the command line, so you will have to learn at least the basics of that.

As in any operating system, files in Linux are organized into "directories" or "folders." You have a "home directory" which contains your personal files. The name of your home directory is /home/username with username replaced by your own user name. You can see the contents of this folder (in a GUI) interface by opening a home directory window. (Click the home directory button in the panel.) This window allows you to navigate through all the directories and files on the system in the usual GUI way.

When you work in a command line window, you are always working in a "current directory." When you first open a console window, the current directory is your home directory. To see a listing of the contents of the current directory, type the command ls and press return. The command dir will give a listing with more information. To change from one directory to another, use the cd command. For example, the directory named "/home/cs124" contains files for this course. To change to that directory, use the command cd  /home/cs124. The cd command with no directory name will always bring you back to your home directory.

The directory /home/cs124 contains a file named HelloWorld.java. Later in the lab, you will need to have a copy of this file in your home directory. Copy it now. There are two ways to do this:

  1. Command-line style: Open a console window, or if one is already open, make sure that you are in your home directory. Type the command "cp  /home/cs124/HelloWorld.java  ." Don't forget to include the "." at the end; this is an abbreviation for "the current directory." cp is the copy command, so this copies the file into your home directory. If you want to verify this, use the dir command.
  2. GUI-style: Open a directory window for your home directory. Open another directory window and navigate to /home/cs124. (For example, enter "/home/cs124" in the "Location" box and press return, or else hit the up-arrow icon to get to "/home", then find the cs124 folder and click on it.) Drag the HelloWorld.java file icon from /home/cs124 into your home directory. You will get a little pop-up menu asking you whether you want to copy it, move it, or make a link to it. Choose copy. A copy of the file will appear in your home directory.

Your most common task in the lab will be editing text files, usually the source code files for java programs. You have a choice of several editors. I have always liked an editor called "nedit", but the most recent version of "kwrite" is also very good. In a command line window, you can edit a file named filename by typing the command nedit  filename or the command kwrite  filename. For example, you could edit the file "HelloWorld.java" with the command nedit  HelloWorld.java. To create a new file, just use a file name that doesn't already exist. (In nedit, you will be asked to confirm that you want to create the file.)

You can also use the GUI to edit a file. Just right-click the file icon. In the pop-up menu that appears, go to the "Open With" sub-menu and select "Advanced Editor" to edit the file with kwrite. (The kwrite program appears as "Advanced Editor" in KDE menus.) To open it with nedit, select "Other" from the "Open With" submenu and type in "nedit". To create a new text file, just right-click in a directory window, go to the "Create New" submenu, and select "Text File". (If you left-click a text file, its contents will just be displayed right in the directory window. However, it's possible to configure KDE to open an edit window for the file instead.)

Both nedit and kwrite will do "syntax coloring" when you edit a java source code file, which can make the program easier to read on the screen. You should try both programs and decide which one you prefer.

A Bit of Programming

You should already have the file HelloWorld.java in your home directory. If not, copy it there, as directed above. The file HelloWorld.java contains the very basic HelloWorld program, which simply prints out the message "Hello World!". Since I have not already compiled this file for you, to run the program you must first compile the source code file. Do this with the command

javac HelloWorld.java

"javac" is the Java compiler program. (This is one case where you really need to use the command-line interface.) After doing this, you can verify with the ls command that the compiler has created a file named HelloWorld.class. This is the compiled, Java-bytecode version of the Hello World program. Now that the compiled program exists, you don't have to compile the program ever again, unless you want to edit it and change what it does. Run the program with the command:

java HelloWorld

"java" is the name of the java bytecode interpretor. The computer will say hello to the world. (Note that you say "java HelloWorld" to execute the class file, and not "java HelloWorld.class".) You now know how to compile and run a program.

For your first exercise, you will write a program that does a computation and prints out a result. Here are some facts: Every year, 724 billion cubic yards of water flow out of the Mississippi. There are 1760 yards in a mile. There are 365 days in a year. Here is the question: How many cubic miles of water flow out of the Mississippi every day? Some Java commands that will get the job done are:

           double cubicYardsPerYear = 724000000000.0;
           double cubicYardsPerDay = cubicYardsPerYear / 365;
           double cubicMilesPerDay = cubicYardsPerDay / (1760*1760*1760);
           System.out.println("Cubic miles per day is " + cubicMilesPerDay);

For a complete program, you have to put this inside the standard Java program wrapper:

        public class Mississippi {

           public static void main(String[] args) {
              double cubicYardsPerYear = 724000000000.0;
              double cubicYardsPerDay = cubicYardsPerYear / 365;
              double cubicMilesPerDay = cubicYardsPerDay / (1760*1760*1760);
              System.out.println("Cubic miles per day is " + cubicMilesPerDay);


This must be in a file named Mississippi.java. Create this file and type in the program. Then compile your program using javac and run it using java.

If your program contains any typing errors, the javac command will produce some error messages. If this happens, you have to go back and edit the file again to fix the errors. It would be a good idea to introduce some deliberate errors into the program to see what the error messages look like.

There are a few convenient features of the command-line interface that you should know about:

Printing a File

Most GUI programs in Linux have a "Print" command in the "File" menu. By default, this will print to the laser printer in the Math/CS computer lab in Lansing 310. However, it is also possible to print to several other computers on campus. You just need to know the printer's name. (Linux can only print to printers that have been configured into it. There is no way to print to a printer in your dorm room.) The names for the available printers are:

To print from nedit, enter the printer name in the "Queue" box in the "Print" dialog window. To print from kwrite, fill in the printer name in the box labeled "Print to printer" in the Print dialog. (You should also make sure that the "Media" in the dialog window is set to "Letter" rather than "A4".) To print from netscape, the "Print command" in the "Print" dialog window should be set to "lpr  -P  printer-name", where "printer-name" is replaced by the name of the printer. You get the idea.

One way to print from the command line is to use the lpr command, which takes the form "lpr  -P  printer-name  file-name". (You can leave out "-P  printer-name" if you want to print to the Math/CS lab laser printer.) For text files, including java source code, there is another print command that makes much nicer printouts. The command is a2ps and it works just like lpr. That is, it takes the form "a2ps  -P  printer-name  file-name". I would like you to use a2ps to print out the java programs that you write for this course. ("a2ps" stands for "ASCII to PostScript". ASCII is the format for standard text and PostScript is a standard for printing.)

Logging Out

Don't forget to log out when you are done. You can use the "Logout" command in the K menu to do this.


Here are the exercises that you should do for this week's lab report. The report is due in class next Wednesday, September 5. Send me the email for exercise 3 before class, and turn in exercises 1 and 2 in class.

Exercise 1:  Add a comment to the beginning of your Mississippi.java program, to explain what it does. Make a printout of your Mississippi.java program using a2ps to turn in.

Exercise 2:  Write a few paragraphs about what you found while exploring and learning about Linux. You should show that you've gone beyond the bare minimum that is mentioned in this lab. For example, did you look at http://math.hws.edu/eck/about_linux/? What did you find there? What interesting things did you find in the K menu?

Exercise 3:  Send me an email (to email address eck@hws.edu). Tell me, briefly, about your background with computers, and explain what it is that you hope to get out of this course.

David Eck, August 2001