CS 124, Spring 2013
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 computers in HWS labs. See the last section of this lab worksheet for more information about programming outside of class.

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.

As part of the lab, you will write your first Java programs. You will turn in your work before the start of next week's lab. See the section on "Turning in your work", later on this lab worksheet, to learn how to do that.

Getting Into Linux

The computers in Gulick 208 are usually running the Windows operating system, but they are also set up to run Linux. There are actually many versions of Linux. The one we are using is called "Linux Mint." You can read more about Linux on the About Linux page, http://math.hws.edu/about_linux

To get into Linux, restart the computer, or start it if it is turned off. (To restart, hit Control-Alt-Delete to get to the login screen. Then, use the small "power button" menu on the bottom-right of the login screen and select Restart.) As the computer starts up, there is a point where you are given a choice between "Windows" and "Linux Mint". Press the down-arrow key once to select "Linux Mint", and press return. You have about 30 seconds to do this. After a short time, you will see a Linux login screen.

Log in using the Linux user name and password that were assigned to you. (Your Linux user name is the same as your user name for email and for the campus network, but the password is different.) Your Linux desktop will have an applications menu at the bottom left of the screen. You can explore some of the applications in that menu, when you have time.

When you are finished with Linux, you should restart the computer so that it will boot back into Windows. You can do this by selecting the icon on the bottom left of the applications menu, then selecting Restart in the dialog box that pops up.

Introduction to the Command Line

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, but the command line can be more efficient and is essential for a few tasks.

To access the Linux command-line environment, open a "Terminal", by selecting the "Terminal" command from the "Applications/Accessories" menu. This will open a window where you can type 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 a command at this prompt, and press return. The computer will attempt to carry out the command that you enter. 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.

An important concept in a command line environment is the "current directory." Your files are organized into directories, which are also called "folders." When you work in a Terminal window, you are "in" a current directory, and the commands that you type apply to that directory. When you first open a Terminal, the current directory is your main or "home" directory. There are commands that you can type to create new directories and to move into a different current directory.

To get started, open a Terminal window, and enter these commands:

mkdir cs124
cd cs124

The first command, mkdir, creates a new directory, named cs124. The second command, cd, changes the current directory. It moves you "into" cs124, so that you are now working in that directory.

I suggest that you keep all the work for this course in your cs124 directory. I also suggest making a new directory, inside cs124, for each lab. To make the directory for this lab, and to move into that directory, enter the commands

mkdir lab1
cd lab1

Do not use spaces or other funny characters in directory or file names! That would only make the directory or file on the command line.

For the rest of this lab, you will be working in your lab1 directory. However, when you have time, you should learn more about the command line. Although you don't need to do that right now, here is a list of commands that you will want to learn about before the next lab:

Your First Java Program

To write Java programs, you need to use a text editor. A text editor lets you type plain text files. It is not the same as a word processor, which would allow you to add all kinds of formatting to the text. For the first part of the course, you will use a text editor named gedit to write and edit your Java programs.

You should still be working inside your lab1 directory. To begin writing your first Java program, type the following command into the Terminal window:

gedit Greetings.java

An editor window will open where you can type the 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 the editor window, except replace XXX with your own name. (Hint: Instead of typing it, you could also open this lab worksheet in a web browser, and use copy-and-paste to copy the code into gedit!)

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 gedit by typing CONTROL-Q or by selecting "Quit" from the "File" menu. Back on the command line, you should compile the program with the command

javac Greetings.java

If you have made no mistakes while typing 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. Once you have Greetings.class, you can run the program by entering the command

java Greetings

The computer should say Hello to you. Before proceeding, you should experiment a bit. If you haven't already seen an error from the compiler, you should change the program to introduce an error, and see what happens when you compile it. (Try removing one of the semicolons, for example.) See if you can figure out how to add another line to the output from the program.

It can quickly become tiresome to have to close the gedit window every time you want to compile and run your program. To avoid this, add a "&" at the end of the command that you use to run the editor:

gedit Greetings.java &

This will allow you to move back and forth between the gedit window and the Terminal window. However, you must always remember to save your program before you compile it. The compiler always works on the version that has been saved, not the version that is in the editor window

As another command-line trick, try using the arrow keys when you type commands. The left- and right-arrow keys work as you probably expect. The up- and down-arrow keys can be used to go back through the list of previous commands, to retrieve a command that you typed earlier. This can save a lot of typing when you are going through the edit-compile-run cycle over and over.

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.

To open a file browser window where you can see all your files and folders, just double-click the "Home" icon on the desktop. (In a GUI context, directories are usually referred to as folders.) You can 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 your lab1 directory, and make a new text file in that directory named "Miles.java". Double-click the file icon to open it in gedit, 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. You can't do that in the GUI; you have to use the command line in a Terminal window. Open one, or go to one that you already have open. In that window, you must be working in the lab1 directory, which contains Miles.java. If not, use the cd command to enter that directory. (You can use the ls command to check that the file is there.) Now, you can use the command

javac Miles.java

to compile the program. Once it has compiled without error, you can use

java Miles

to run the program. Always 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 gedit window. The same thing applies if you have to fix errors in the program: Remember to save it before re-compiling it!

Improved Editing

The default preference settings in gedit are not ideal for editing Java programs. For improved editing, you should make some changes to the preferences. In any gedit window, select the "Preferences" command from the "Edit" menu. Change the settings in the "View" pane and in the "Editor" pane as follows:

Gedit Preferences Settings Dialog

That is, you want to turn off Text Wrapping, turn on Line Numbers, turn on Bracket Matching, set Tab Width to 4, turn on Insert Spaces Instead of Tabs, and turn on Automatic Indentation.


Further Assignments

Here are a few other short assignments for you to work on. Remember that you will 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 "/".) You will need to change the first line of the program to read "public class Pounds" to match the name of the file.

As a final assignment, write a program to solve the following problem: You are throwing a little party for all of HWS's 2216 students. You want to have 3 glasses of root beer available for each student. A keg of root beer costs $93.95 and serves 210 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 = 2216;
glassesPerPerson = 3;
glassesPerKeg = 210;
costPerKeg = 93.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 entire lab1 folder into a homework folder that I have created for you. Your homework folder is in the folder named /classes/cs124/homework, and its name 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 either by dragging it in the GUI or by using the cp command in the command-line interface. To use the command line, cd into your cs124 directory, which contains the lab1 directory, and use a command like:

cp -r lab1 /classes/cs124/homework/zzzzzz

replacing zzzzzz with your own last name, in all lower case letters.

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.