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:
- ls -- List the contents of the current directory. The contents can include files and more directories, referred to as "sub-directories."
- ls -l -- Same as ls, but gives a "long" listing, with extra information such as the size of the files. In the long listing, directories are marked with a "d" as the first character on the line. The "-l" is called an "option." Many commands have options that can affect what they do. [Note: In case you can't see it, there's a space between "ls" and "-l". And the option is a lower-case letter "L", not the number "1".]
- cd XXX -- Move into to the directory named XXX. That directory becomes your current directory. Often, XXX is the name of a directory that is inside the current directory, but it can also be a "path" to a directory in another location. [Again, there is a space between "cd" and the name of the directory.]
- cd -- When used without a file name, cd moves you back into your main, or "home," directory. Your home directory contains all your files. When you first open a Terminal, the current directory is your home directory.
- mkdir XXX -- Create a new directory in the current directory (or in another location if XXX is a path rather than a simple file name).
- gedit XXX -- Edit a text file named XXX. If the file does not exist, it will be created. gedit is the name of a text editor. You can use it to create and edit Java programs. Note that this command does not complete until you close the editor -- you won't get another command prompt in the Terminal until you do so.
- rm XXX -- Remove (that is, delete) the file named XXX. Be careful with this command! There is no way to restore a file that you have removed. Note that rm XXX will not work if XXX is a directory. For that, you can use rm -r XXX. The "-r" is an option that makes rm remove a directory and everything that it contains; be even more careful with this command!
- cp XXX YYY -- Make a copy of a file named XXX. This command works differently, depending on the nature of YYY. If YYY is an existing directory, then a copy of XXX is placed into YYY. If YYY is an existing file, then YYY is replaced with a copy of XXX, and the previous contents of YYY are lost. (Be careful when using cp!) If YYY is not a directory and doesn't already exist, then a new copy of XXX is created, and the copy is named YYY. (If you want to copy an entire directory named XXX, instead of just a file, use cp -r XXX YYY.)
- mv XXX YYY -- Rename or move XXX to YYY. If YYY is the name of an existing directory then XXX is move into the directory YYY. If YYY does not exist, then XXX is renamed as YYY. If YYY is a file that exists, then YYY is deleted before XXX is renamed to YYY. (Once again, be careful!)
- passwd -- Use this command if you want to change your password. You will be asked to enter your current password, then to enter the new password twice. Your typing will not be echoed as you type the passwords.
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 open a folder, to see what's inside, double-click the folder icon.
- To open a file with the default application for that file, double-click the file icon. A .java file will be opened with gedit. To open a file with a different application, right-click the file icon and select the application from the menu that appears.
- To create a folder, right-click in the file browser window, but not on an icon. Select "Create New Folder" from the pop-up menu. Immediately after creating the folder, you can type in a new name for it.
- To create a new file, right-click and select "Create New Document" / "Empty Document", and enter the name of the file.
- To move a file or folder, just drag the item to its new location, which can be another file browser window or the Desktop. To copy the item, right-click it and use copy-and-paste.
- To delete a file or folder, right-click the icon and select "Move to Trash."
- To rename an item, right-click the icon and select "Rename".
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:
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.
- You can work on the computers in the Math/CS lab, Lansing 310. These computers are always running Linux and do not have to be rebooted; otherwise, they work the same as the computers in Gulick 208 lab.
- You can usually work in Gulick 208 in the evening and on weekends, when no classes are in session.
- You can write, compile, and run Java programs on your own computer, if the JDK (Java Development Kit) has been installed. If you work on your own computer, you will need to copy your work onto the Math/CS Department's file server. One way to do that is with a program that supports "sftp"; see http://math.hws.edu/about_linux/sftp.html.
- You can use VNC on your own computer to work remotely on Math/CS computers. See http://math.hws.edu/about_linux/vnc.html for details. However, VNC can be slow and is not an ideal way to connect.
- If your computer uses Mac OS or Linux, you can use ssh to connect to the Math/CS lab computers and work on them remotely. See http://math.hws.edu/about_linux/ssh.html for information.