CPSC 124, Spring 2006
Lab 1: Introduction to Linux and to Java

Welcome to the first lab for CPSC 124: Introduction to Programming. In this lab, you will be introduced to the Linux operating system, which you will be using for the rest of the course. You will also get a short introduction to programming in Java.

This is an introductory lab. Since we have only had one day of class so far, there is not a lot that you can do with Java. However, this lab will get you started on some essential skills that you will need to do the rest of the course. There are just a few little exercises, one that will be done in lab, and one that will be turned in next week.

Before you leave the lab, please remember to log out of Linux: Select "Log Out" from the Start Menu, then click on "Restart Computer" in the dialog box that pops up.


Part 1: About Linux

Linux is a computer operating system. That is, it is an alternative to Microsoft Windows and to Mac OS. It will run on almost any computer that can run Windows. Many people believe that it is superior to Windows in a lot of ways -- starting with the fact that it is free and comes with a large quantity of free software.

The computers in Gulick 208 and in the Library Multimedia lab are dual-boot machines, meaning that they can run either Windows or Linux. You can't run both operating systems at the same time -- one operating system must be selected when the computer boots. The computers in the lab will boot into Windows unless you actively select Linux during the boot process. Since you will be using Linux in all of the labs for this course, you will have to restart your computer and boot it into Linux at the beginning of each lab.

Begin the lab, and all labs for this course, by restarting the computer. (If you do not know how to do this, please ask for help.) After a few minutes and two reboots of the computer, you will get to a screen that allows you to select between Windows XP Professional and Linux. Hit the down-arrow key to select "Linux", then hit the Enter key. You have 15 seconds to do this -- if you don't actively select Linux, the computer will boot into Windows after 15 seconds. It will take some time for Linux to boot, but eventually you will get to a log-in screen where you can enter your Linux user name and password.

Enter your Linux user name and password into the login screen and press Enter. If the user name and password are correct, you will see a screen that says "Initializing Desktop System". This can take a while, especially the first time that you log in. Eventually, you will see a Linux desktop that is not all that different from a Windows desktop. Linux actually offers a choice of desktops. The one you will be using is called the KDE. If you want to try another desktop, you can select one from a menu in the login screen before you log in. However, instructions in the lab will generally assume that you are using KDE.

Many programs are available under Linux. You can find a lot of them in the "Start Menu." To see this menu, click on the icon at the lower left corner of the screen. You are encouraged to try some of these programs. For example, there are some nice games: Try "FooBillard" under Games/Sports, "TuxRacer" under Games/Action, or "Frozen-Bubble" under Games/Arcade. If you want to do instant messaging under Linux try "Kopete" or "Gaim Instant Messenger" under Internet/Chat. For Web browsing, you can try Firefox -- you will find a Firefox icon on your desktop. Click the icon once to start the program.

Linux is highly configurable to your own taste. For example, by default it is a single-click environment (you only click once on an icon to open it), but you can change it into a double-click environment if you want. Most of the configuration options are available in the Control Panel. To run the Control Panel, select the "Control Panel" command from the Start Menu.

Make some changes to your Linux configuration. In the Control Panel, click on "Appearance & Themes." Then click "Splash Screen," and change the ugly KDE startup screen to something nicer. ("Default" is OK.) Click "Test" to see what it looks like. Click "Apply" to make your selection permanent. You might want to check out some other options under "Window Decorations" -- I like "KStep" myself, but if you use this, you should also click on the "Buttons" tab and check "Use custom title bar button positions." Under "Background," you can select a different background image -- I rather like "Eagle Nebula," Try some other changes too.

If you would like a double-click environment, go back to the main screen of the Control Panel. Click on "Peripherals" then on "Mouse." In the Mouse panel, select "Double click to open files and folders," and click "Apply."

For the first few weeks of the course, you will be using a command-line environment where you type in commands and the computer responds. To use a command-line environment, you have to open a window where you can type the commands. There should be an icon for this on the bottom of the screen. It looks like a little computer monitor. If not, select System/Terminal/Konsole in the Start Menu. The Konsole program presents you with a prompt where you can type commands. The Konsole program is configurable. Use the "Settings" menu. (Try "Transparent, Dark Background" under "Schema" in the "Settings" menu.) To make the settings permanent, you have to choose "Save As Default" in the "Settings" menu.

Exercise 1: After you have changed some of the configuration of your Linux environment and tried out some Linux programs, call me over and tell me about what you've done.

If you would like to know more about Linux, you can start by visiting the "About Linux" pages at http://math.hws.edu/eck/about_linux/.


Linux Outside the Lab

This section is not really part of today's lab. However, it contains information that you will need when you want to do computer course work outside of lab.

First of all, the Department of Mathematics and Computer Science has its own small lab with 12 dedicated Linux machines. These machines can be found in room Lansing 310. They have large screens, and you don't have to wait for them to reboot before you get to use Linux. I encourage you to use them.

If you want to use Linux in your dorm room, you can use a program called VNC to access the Math/CS lab computers from Windows or Mac OS. VNC allows you to open a window that connects you to a remote computer. Linux is actually running on the remote computer, not on your computer, but you can log in and use it just as if you were sitting in front of the Linux computer itself.

Note that VNC connections can only be used over the local HWS network. You can't use it from off campus. (Actually, you can, if you use VPN software to "tunnel" into the HWS network, but the resulting connection will be slow and not very pleasant to use.)

TightVNC is a version of VNC for Windows. You can find it on the Internet, or you can download it from http://math.hws.edu/local/vncviewer.exe. (This link can only be used on the HWS campus network.) This is an executable file that you can just download and run. For Mac OS X, you can use the oddly named program "Chicken of the VNC." You can get a copy over the local HWS network at http://math.hws.edu/local/MacOS-vncviewer-installer.dmg.

When you start your VNC program, you will be asked to enter the VNC server to which you want to connect. You connect to any of the machines cslab1.hws.edu, cslab2.hws.edu, ..., cslab12.hws.edu. Please pick one at random. You also need a VNC port number, which can be 1, 2, or 3. For example, you can connect to the VNC server at cslab5.hws.edu:1 or the one at cslab10.hws.edu:3. The difference between ports number 1, 2, and 3 is the size of the desktop. Using 1 for the port number will give you a 1024-by-768 Linux desktop. Port number 2 gives 1280-by-1024, and port 3 gives 800-by-600. Use a port number that will give you a window size that will fit onto your Windows or Mac OS desktop.

It is also possible to install Linux to run on your own computer, but that is not something to be undertaken lightly. You can read more about this on the About Linux pages, or ask me for more information.


Part 2: About Java Programming and the Command Line

Java is a computer programming language that is the major focus of this course. In class yesterday, you saw what a short Java program can look like. Today, you will learn how to type in, compile, and run a Java program. Starting in the third week of the course, you will use a programming environment that will simplify the process, but for now you will do everything by hand so that you can see all the details that are involved.

A directory (or "folder") named cs124 already exists in your account. You should put your work into the cs124 directory, since I have permission to read the contents of this directory. You will be working on the "command line," which means that you will be typing in commands for the computer. While you can do a lot using a GUI (that is, a point-and-click Graphical User Interface), there are some essential command-line skills that you will need to learn. To use the command line, you must open a console window where you can type the commands.

So, begin this section of the lab by opening a Konsole window, as described above, if you do not already have one opened.

You can create a Java program with any text editing program. You need a text editor, which uses plain text files, not a word processor. I like a little program called nedit. To use it, you should should first open a Konsole window (or use the one that you already have open). To work in your cs124 directory, give the command

            cd cs124

Then, create a Java program file with the name Measures.java. To do this, give the command

            nedit Measures.java

A box will pop up to tell you that the file Measures.java does not exist. Click on the "New File" button to confirm that you want to create a new file.

Now, type in the following Java program, exactly as you see it here (or, if you are smarter, you can go to this page on the Web and copy-and-paste the program into nedit):

         public class Measures {
         
            public static void main(String[] args) {
               int secondsPerMinute, secondsPerHour, secondsPerDay, secondsPerYear;
               secondsPerMinute = 60;
               secondsPerHour = 60 * secondsPerMinute;
               secondsPerDay = 24 * secondsPerHour;
               secondsPerYear = 365 * secondsPerDay;
               System.out.println("The number of seconds:");
               System.out.print("   in an Hour is ");
               System.out.println(secondsPerHour);
               System.out.print("   in a Day is ");
               System.out.println(secondsPerDay);
               System.out.print("   in an Year is ");
               System.out.println(secondsPerYear);
            }
                      
         }

(You do not have to completely understand this program for now, but note that a "*" tells the computer to do a multiplication.)

After typing in the program, select the "Save" command (Control-S) from nedit's File menu, then the "Exit" command (Control-Q). Before you can run your program, you have to compile it. You can do this with the following javac command:

            javac Measures.java

If you just get a prompt, with no messages, then the program has been compiled successfully. If you didn't type the program exactly right, you will get some error messages. If that happens, use the command nedit Measures.java again to edit the program and make the changes necessary to fix the errors. Once you've done that, try javac Measures.java again. Once you get through a successful compilation, you will have a new file named Measures.class. This file contains the compiled program. To see that the class file does actually exist, use the command

            ls

which produces a listing of the files in the directory where you are working. To run the program, use the java command as follows:

            java Measures

When you do this, the computer will carry out each of the commands in the program, resulting in several lines of output in the Konsole window.

Although you will be working with much more complicated programs as the course goes on, this "edit/compile/run" cycle is at the heart of what happens whenever you program in Java.

There is one other thing you need to learn how to do to complete the lab. To make an attractive printout of a program, you should use the a2ps command. To print the program Measures.java on the printer in Gulick 208, use the command

            a2ps  Measures.java

The a2ps command formats the program nicely. When you print in the Gulick 208 lab, the output goes to the printer in Gulick 208. If you are working in the Library Multimedia Lab or in the Math/CS lab in Lansing 310, the output will go to the appropriate printer.

Exercise 2: Modify the program Measures.java so that instead of printing the number of seconds in an hour, day, and year, it prints the number of inches in a foot, a yard, and a mile. (Note that there are 1760 yards in a mile.) Make a printout of the modified program using the a2ps command, and turn it in at the beginning of lab next week. Then modify the program by removing the line that starts "int...". What errors do you get when you compile the program, and what do you think the errors mean? Write your answers to these questions on the printout of the program that you turn in next week. Note that in addition to looking at your printout, I will look in your cs124 directory to see that the .java and .class files are there.

Exercise 3: After you have finished the rest of the lab, take some time to explore the Linux environment. To prove that you have done this, write a sentence or two about a feature of Linux that was not mentioned in this lab. You can write this on the program that you turn in next week. The feature can be an interesting configuration option, a program, or some fact about Linux that you find on the net.


Logging Out

When you are finished working in Linux, you should remember to log out. You can do this using the "Log Out" command in the Start Menu. When you do this, a box will pop up asking what you want to do. On the machines in Gulick 208 and the other dual boot machines in public labs, you should select "Restart" in this dialog box, which will reboot the computer into Linux. In the Math/CS computer lab in Lansing 310, you should choose "End Session" (which will be the only option), which will return the computer to the Linux login screen. You should not shut down or reboot the computers in Lansing 310, since other people might be using them remotely.


David J. Eck, January 2006