CS 124, Fall 2009
Lab 1: Introduction to Linux and Java

Welcome to CS 124, Introduction to Programming. In this course, you will learn how to write computer programs in the Java programming language. In the labs for this course, you will be using the Linux operating system. For programming outside of lab, you can use your own computer (with almost any operating system that you prefer), or you can continue to use Linux on the computers in the Colleges' computer labs.

This first lab of the semester introduces you to the Linux operating system and to Java. Because of all the new material on using Linux, this lab handout is rather long, and you should read through it in advance.


Getting Into Linux

The computers in the Library Multimedia Lab and in Gulick 208 are ordinarily running Windows. To use Linux on one of these computers, you must first restart the computer. After hitting Control-Alt-Delete, click the "Shutdown" button, choose "Restart" under "What do you want the computer to do?", and click "OK". During the restart process, there is a point where you will have the option of selecting Linux. You have about ten seconds to do so; if you do not change the selection to Linux, the computer will boot into Windows. To boot into Linux, use the down-arrow key to move the selection to "Kubuntu Linux", and press return.

Once the computer has booted into Linux, you will see a login screen. Use your usual user name and the password for Linux that you were given in class. (Your Windows password will not work.) You will log into the KDE 4 Desktop environment. The first time that you log in, it will take a while to set up KDE; subsequent logins won't take as long. (One of the first things you may see the first time you log onto a given computer is a dialog box with the title "Removed Sound Devices". It is safe to check the "Do not ask again..." box, and click "Yes".)

This year, we have installed Version 4 of KDE, which has some annoying features compared to older versions. To get rid of some of the worst of them, I suggest that you do the following:

Take some time to look around. Check out some of the many entries in the "K" menu. Try out some of the games (briefly! -- you can play the games on your own time).

Note that KDE is not your only alternative. Another popular desktop, called Gnome is also available. To use Gnome instead of KDE, you must select it in the "Sessions" menu on the Linux login screen, before you log in. You can try out Gnome if you want and ask for help if you need it. Note that the same set of applications is available whether you use KDE or Gnome.

At the end of the lab, before you leave, you will need to reboot the computers into Windows. Just select "Leave" from the "K" menu, and click on "Restart Computer" in the dialog box that pops up.


A Basic Command-Line Environment

This section introduces the command-line environment, where you interact with the computer by typing in commands. Most of the things that you can do on the command line can also be accomplished by menu and mouse. The command line can be more efficient and is essential for a few tasks.

To access the command-line environment, choose the command "Konsole Terminal" from the "System" submenu of the "K" menu. This will open a window where you will enter your commands. When this Terminal window is active and waiting for a command, you will see a blinking cursor next to a prompt. You type certain commands at this prompt, and press return. The computer will attempt to carry out the command. If there is an error, you will usually see an error message and another prompt. If the command is carried out successfully, you might not get any message at all, just the prompt where you can type another command.

Your files are organized into "directories", also called "folders." When working on the command line, you are always working in a "current" directory. Many of the commands that you can enter will apply to the current directory. Here are some common commands:

You will want to spend some time experimenting with commands on the command line.

You should store all the work that you do for this course in a directory named cs124, inside your home directory. Inside that directory, you make a separate directory for each lab. To get this started for the first lab, type in this sequence of commands on the command line:

cd
mkdir cs124
cd cs124
mkdir lab1
cd lab1
kate Greetings.java

At the end of this, you will have an editor window open where you can type your first Java program. The traditional first program just says "Hello World" -- but let's make it a little more personal. Type the following lines of Java code into kate's edit window, except replace XXX with your own name. (Hint: You could also open this lab handout in a web browser, and use copy-and-paste to copy the code into kate.)

public class Greetings {

   public static void main(String[] args) {
      System.out.println("Hello, XXX!");
      System.out.println("Pleased to meet you.");
   }
   
}

After typing this, save your file and exit from Kate. Back on the command line, you can compile the program with the command

javac Greetings.java

If there are no errors in the program, this command will succeed with no error messages; you will simply get another command prompt. If there are errors in the program, they will be listed. In that case, you have to edit the file to fix the errors and compile the program again. Once the program has been compiled successfully, you will have a new file named Greetings.class. Use the ls command to check that it is there. Greetings.class is the compiled program. You can run the program with the command

java Greetings

The GUI Approach

You are encouraged to learn to use the command line, and you will have to use it for certain things, such as compiling and running programs. However, you might be more comfortable with a Graphical User Interface for many tasks.

You should have a "Home" icon on your desktop. Click this icon to open a file browser window. You can use this folder to view files and folders. (In a GUI context, directories are more commonly called folders.) You can also use the file browser to create, delete, move, copy, and rename files and directories. Specifically,

To test this, open a file browser window, navigate to the "lab1" directory that you have already created, and make a new text file in that directory named "Miles.java". Open the new file in kate and enter the following Java program into it:

public class Miles {

   public static void main(String[] args) {
      double miles, feetPerMile, feet;
      miles = 2.5;
      feetPerMile = 5280;
      feet = miles * feetPerMile;
      System.out.print("The number of feet in");
      System.out.print(miles);
      System.out.print(" miles is ");
      System.out.print(feet);
      System.out.println();
   }

}

Now, you want to compile and run this program. The nice thing is, you can do this without even leaving kate. On the bottom of the editor window is a button labeled "Terminal". Click this button, and a small terminal pane opens within the kate window. You can type commands there just like you would in a separate Terminal window. Furthermore, when you first open the terminal pane, it automatically does a cd into the directory that contains the file that you are editing.

Compile and run Miles.java, using the terminal pane in the kate window. Be sure to save the file before compiling it -- you will be compiling the version that is saved on disk, not the version that is in the edit window. The same thing applies if you have to fix errors in the program: Remember to save it before re-compiling it!


Further Assignments

Here are a few other short assignments for you to work on. Remember that you might have to complete these outside of lab.

Make a copy of the program Miles.java, and name the copy Pounds.java. Modify the new copy so that instead of converting 2.5 miles into feet, it will convert 178 ounces into pounds. A pound contains 16 ounces. (Warning: For this program, you have to divide, not multiply. Multiplication in Java is indicated by "*"; division is indicated by "/".)

As a final assignment, write a program to solve the following problem: You are throwing a little party for all of HWS's 2040 students. You want to have 3 glasses of root beer available for each student. A keg of root beer costs $92.95 and serves 180 glasses. How many kegs should you buy, and how much will they cost? Your program should do all the calculations starting from the basic data here, and then print out the answers. In your program, give names to the initial data, and work from there:

people = 2040;
glassesPerPerson = 3;
glassesPerKeg = 180;
costPerKeg = 92.95;

You can name your program anything you want.


Turning in Your Work

After doing all the assignments in this lab, you should have a directory named "lab1" that contains four programs. For each program, there should be a ".java" source code file and a ".class" compiled file. If you don't have all this, read though the lab again, and see what you missed!

To complete the assignment, you must copy your lab1 directory into a homework directory that I have created for you. Your homework directory is in the directory named /classes/f09/cs124/homework, and the name of the directory is the same as your user name. You must copy your lab1 directory into the correct location by the beginning of next week's lab. You can copy it using either the GUI or command-line interface.


Working Outside of Lab

You will need to do a lot of programming work outside of lab. You have many options for working on Java programs on your own.


Command-line Tricks, Part 1

To be really comfortable using the command line, whether in a Terminal or inside kate, you need to know some tricks. The more you know, the more you will appreciate the command line. Here are a few of the most basic tricks: