CS 124, Fall 2017
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 Rosenberg 009 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 10 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 "Administration/Accessories" menu. (Or just click the Terminal icon on the left edge of the menu, or type Terminal in the input box at the top of the menu and press return.) 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 working "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 inside the current directory. 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 your 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 following commands while working in the cs124 directory:

mkdir lab1
cd lab1

Do not use spaces or other funny characters in directory or file names! Such characters can make some things more difficult when working on the command line.

If you log into the computer later and want to go back to working on Lab 1, you can get directly to the lab1 directory by giving the command

cd cs124/lab1

after logging in. The cs124/lab1 in this command is called a "path" and it refers to something named "lab1" inside a directory named "cs124" that is in your current directory. If you ever get confused about which directory you are working in, you can use the cd command with no directory name to return to your home directory.

You can read more about the command line at http://math.hws.edu/about_linux/commandline.html, and a few more commands will be mentioned later in this lab. You will use the command line mostly to edit, compile, and run Java programs, but there is a one more basic command that you should be familiar with: Use the command passwd to change your Linux password. When you give this command, you will be asked to enter your current Linux password. Type it and press return. The characters that you type for your password will not be shown on the screen. You will then be asked to enter your new password twice in the same way. Your password will only be changed if the two copies of the new password are identical.

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 xed 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:

xed Greeting.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 the editor window!)

public class Greeting {

   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 xed 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 Greeting.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, using the command xed Greeting.java again, and then compile the program again. Once the program has been compiled successfully, you will have a new file named Greeting.class. Use the command


to check that Greeting.class has in fact been created. The ls command outputs a list of the files that are in the current directory. Greeting.class is the compiled program. Once you have Greeting.class, you can run the program by entering the command

java Greeting

When you give this command, the computer will run the program. It 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 xed 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:

xed Greeting.java &

This will allow you to move back and forth between the xed 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 xed, 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 is ");


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. (As a shortcut for opening a Terminal window, right-click the file browser window and select "Open in Terminal" from the menu.) 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 xed window. The same thing applies if you have to fix errors in the program: Remember to save it before re-compiling it!

Improved Editing — Make Sure to Get This Done!

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

Xed Preferences Settings Dialog

In particular, you want to turn off "Enable text wrapping", turn on "Display line numbers", turn on "Highlight matching bracket", set "Tab Width" to 3 or 4, turn on "Insert spaces instead of tabs", and turn on "Enable automatic indentation". Changing these preferences will make xed much more suitable to be used as a program editor!

Further Assignments

Here are two more short assignments for you to work on. Remember that you will have to complete these outside of lab if you do not finish them during the lab period.

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 2262 students. You want to have 3 glasses of root beer available for each student. A keg of root beer costs $87.69 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 to both questions. In your program, give names to the initial data, and work from there:

people = 2262;
glassesPerPerson = 3;
glassesPerKeg = 210;
costPerKeg = 87.69;

The name of the file must be RootBeer.java.

To get full credit for this program, you should output the total cost in the standard format for dollar amounts, with two digits after the decimal point. Use the System.out.printf statement, which you can read about in Section 2.4.1 in the textbook. Also, you might want to read about the Math.ceil function in Section 2.3.1.

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. The Java files should be named Greeting.java, Miles.java, Pounds.java, and RootBeer.java. If you don't have all this, read though the lab again, and see what you missed!

In general, your work for a lab must be turned in before the start of the next lab. For this first lab, the cutoff time is 12:30 PM next Thursday, in case you need last-minute help with turning in your work. Lab work should be turned in using the web application at


You should bookmark that page! Your username for the Submit web site is the same as your Linux username, and your password has been set to be the same as your (original) Linux password. There is a link on the login page that you can use to reset the password, if you forget it or would like to use something different.

When you log in to the Submit124 web site, you will see a list of assignments for this course. For now, there is one assignment, named "Lab1". There is a table of files that you need to submit to complete the assignment. To submit a file, click on the "Browse" button for the file, and select the file that you want to submit from your computer. Note that the file name must be correct! After browsing for any files that you want to submit, click the "Submit" button. The files will be uploaded to the server, and the server will try to compile them. It will take several seconds to get the response. You will see a report on each file. Note that the system tests whether the files can be compiled, but does not check that they work correctly!

You can submit programs from any computer, using any web browser. You do not have to submit all the files at once. You can select just the file or files that you want to submit. You can submit a file as many times as you want. When you resubmit a file, it will replace the previous version.

Before you leave lab today, you should submit at least one program, to make sure that you understand how to use the file submission system.

Working Outside of Lab

For future labs, 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.