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.
Important: When using Linux in Gulick 208, you should always restart the computer, so that it will reboot into Windows. You can do this by selecting "Shutdown" from the "System" menu and clicking on "Restart".
The computers in Gulick 208 are ordinarily running Windows. To use Linux on one of these computers, you must first restart the computer. You can do this using the small "power" menu in the bottom-right corner of the login screen. During the restart process, there is a point where you will have the option of selecting Windows or "Ubuntu". You want to select "Ubuntu" (which refers to Ubuntu 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 select "Ubuntu", 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 Gnome Desktop environment. It will take a little longer to log in the first time you do it, as the environment is set up.
Take some time to look around. Check out some of the many entries in the "Applications" menu at the top left of the screen. Try out some of the games (briefly! -- you can play the games on your own time).
Note that you can drag an entry from the "Applications" menu onto the bar that runs across the top of the screen. This will add an icon for the application to the bar, and you can click that icon to start the application instead of using the menu. You will be using the "Terminal" application regularly in this course. Find "Terminal" in the "Applications/Accessories" submenu, and drag it onto the bar at the top of the screen.
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, open a Terminal, either by selecting the "Terminal" command from the "Applications/Accessories" menu or by clicking an icon that you have added to the bar at the top of the screen, as described above. 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. (If you don't see a prompt when you expect one, press return. Sometimes the computer prints messages to the terminal that can obscure the prompt.)
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 should 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 gedit 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 the editor 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 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 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
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, you can select "Home Folder" from the "Places" menu. (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 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 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. To do this, you will need a Terminal window. Open one, or go to one that you already have open. In that window, you must be working in the directory that 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!
The default preference settings in gedit are not acceptable for a program editor. For improved editing, you should make some changes to the preferences. In any gedit window, select the "Preferences" command from the "File" 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.
You might also want to try a different text editor, jedit, which is designed as a programmer's editor and is written entirely in Java.
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 2090 students. You want to have 3 glasses of root beer available for each student. A keg of root beer costs $89.95 and serves 205 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 = 205; costPerKeg = 89.95;
You can name your program anything you want.
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 folder into
a homework folder that I have created for you. Your homework folder
is in the folder named /classes/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.
Ask for help if you need it!
To be really comfortable using the command line, whether in a Terminal or inside gedit, 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:
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.