CS 117 Lab 1: Introduction to Linux, Java, and the CS Lab Environment


The objectives of today's lab:

  1. Familiarize yourself with the CS lab environment
  2. Familiarize yourself with the Linux operating system
  3. Familiarize yourself with the Java programming environment

Introduction to the CS Lab Environment

In this class, you will be using the machines in CMC 306 for your programming assignments, projects, and labs.. These machines run both Windows and Linux. Most or all of you have used Windows already, but most likely you are not familiar with Linux. We'll be learning more about Linux in today's lab.

Windows and Linux are examples of operating systems. An operating system is the set of programs that tells the computer what to do when. It handles things like determining which program or process gets to use the computer at any given time, how data is stored in files, and so on.

In this class, you will be programming using the Java programming language in the Linux operating system. In particular, you will be using the following programs:

In the next part of today's lab, we will be covering the basics of the Linux operating system. You will learn some basic commands that you will need to use over and over throughout the course of the term.

Introduction to Linux

By now, you have logged in to the lab machines and started Mozilla. Take a few minutes to familiarize yourself with the computing environment. You've already found the web browser; see if you can also find examples of the following programs:

Now we will learn some of the basic Linux commands. You will be using these commands throughout the term. Before we dive in, though, we have to open up a terminal window. A terminal window, or xterm, is where you type in the commands to interact with the computer: create and delete files, compile and run programs, get help, etc. To open a terminal window, do either of the following:

Before the end of today's lab, both you and your lab partner should try logging in to your lab computer.

Note: Both Linux and Java are case-sensitive, so pay attention to what you are typing. Upper case letters vs. lower case letters are different.

Directories

Once you have the terminal window up, type pwd and then hit the Enter key. pwd stands for "print working directory"; it lists the name of the directory that you are currently in. Linux organizes everything according to directories (some other operating systems call these folders). Some of these directories are created by the system, but directories can also be created by the user. Each user has his or her own "home directory", where all the files created by that user are placed. This is a bit different from Windows, because on Windows a user can put a file anywhere and there is no concept of a "home directory" (although the "My Documents" folder is close). When you type pwd now, it should show that you are in your home directory. Make a mental note of what your home directory's full name is.

Now type ls (this is a lowercase "L" and not the number "1") and then hit the Enter key. ls is the command for "list files and directories". It will list all the files and directories that are currently in your home directory. There should be nothing listed (yet).

Let's create some directories. A good idea is to store groups of things in separate directories. Some people organize their home directories to contain directories named "classes", "programs", "data", "docs", and so on. You might want to create separate directories for each lab or homework assignment for this class. Directories can also have subdirectories; so, for instance, a "classes" directory can have subdirectories "fall2004", "winter2005", "spring2005", and each of these subdirectories can have directories for each class during that term.

Note: While in Windows it's perfectly acceptable to use spaces in your filenames and directory names, in Linux you want to avoid doing this. (You can do it, but it's a bit of a headache to deal with.) So, what happens if you have a file or directory name that's more than one word long, like "financial docs"? You can either use an underscore in between the two words ("financial_docs"), or use capitalization to delineate the words ("financialDocs" or "financialDocs"). Either one is acceptable, though the second approach will be consistent with the Java programs that we write.

Type mkdir labs in the terminal window and press Enter. mkdir stands for "make directory". Now if you type the command ls, you should see that directory listed in your home directory. It has been created.

Now let's create a subdirectory. Type cd labs and press Enter. cd stands for "change directory". Enter the commands ls and pwd and make a note of what you see. You are now in the "labs" directory that you just created. Now use mkdir to create another directory here, to store the files for this lab that you will create. Name the new directory "lab1".

If you ever want to remove a directory, use the command rmdir. For example, to remove the directory "foo", type:

rmdir foo

Try creating and deleting some directories now.

Shortcuts: At any time, to return to your home directory, you can just type cd, with no directory name afterwards. To go up to the parent directory of whatever directory you're in, type cd ..

Creating and editing files

Go back into the "lab1" directory that you created. We will now create some files to put in this directory. To create files, we will use the program NEdit. NEdit is a text editor that is very similar to Windows Notepad or other programs you may have used; in fact, it has similar menus and, in most cases, identical "shortcut" keys (such as [CTRL]s to save a file). Start NEdit by doing one of the following:

A blank window should appear. Write a short poem in this window, and save it in the "lab1" directory as "poem.txt". Then exit out of NEdit.

Did the poem save in the correct spot? To check, list the contents of the "lab1" directory. You should now see "poem.txt" listed. To view the contents of the file, type more poem.txt. The more command is useful for checking the contents of a file without having to open a text editor.

To remove a file, use the command rm. For example, to remove the file "foo.txt", type:

rm foo.txt

Note that rm will permanently remove a file, so use with care! There is no "recycle bin" or "trash can" in Linux.

Try creating and deleting some more files.

Relocating files and directories

Sometimes, we'd like to move and copy files around. For example, maybe you're afraid that you'll accidentally delete your beautiful poem, so you want to create a backup copy. Or, maybe you made a note to yourself to remind you of a meeting later, and you want to move this to your home directory (where you'll see it) rather than keeping it in your "lab1" directory (where you'll most likely miss it). Fortunately, Linux has commands for these too:

Practice moving, copying, creating, and deleting more files now.

Getting help

The instructor, prefector, and lab assistants are always a great source for assistance. As it turns out, though, there is also help built-in to Linux! You can get help on any Linux command or program by using the man command. man is short for manual. If you type in man followed by the name of any Linux command or program, you will get a help page for that program or command. Look at the man pages for all of the commands that we've used in today's lab. Is there a man page for Mozilla? For NEdit? Note: To exit out of man, type the letter q.

Notes, tips, and tricks

This page lists all of the commands that we've used in today's lab. Feel free to use it as a reference.

Introduction to Java

Now that you're familiar with the lab environment and with Linux, it's (finally!) time to dive into Java. Let's start by looking at a really simple Java program.

A really simple Java program

Click on the file Simple.java and save it in your "lab1" directory. Open up the file in NEdit. The parts of the program are labeled. Take a couple of minutes to look at the program; see if you can figure out what it does.

Before we can run it, we need to convert this text into bytecode (a format that Java can read and interpret). We do this by compiling the program (running a special program that converts text into bytecode). Java's compiler program is named javac. To compile this program, type:

javac Simple.java

The program should compile without returning any messages. (Any messages you see are errors). If you do get errors, get help from from the instructor or the prefector.

Once you've compiled the program successfully, do an ls. You should see a new file in the directory, named Simple.class. This is the "executable" (bytecode) file that you will run. To run the program, type:

java Simple

Notice that you do not include the .class extension when you run a Java program.

EXERCISE 1: Modify the program so that it prints out "Welcome to CS117, [your name and your partner's name]!".

In general, to compile a Java program type javac [filename], and to run a Java program type java [classname], where [classname] is the name of the class file, minus the .class extension.

A diversion: What a Java program looks like

Most Java programs look pretty similar. They all have the following format:

Simple.java does not contain any data, but it does contain one method. That method is called main. main is a special method in Java; it is always the one that the computer looks for to see how to run your program.

EXERCISE 2: What action does the main method in Simple.java perform?

Now download the file Simple2.java into your lab1 directory. Simple2.java is very similar to Simple1.java; the difference is that Simple2.java uses data. The line that starts with String is our data line. This line is an example of a variable declaration, which is a fancy way of saying "here's some data that we will use; it's a String (of characters), its name is 'message', and its value is 'Are we having fun yet?."

EXERCISE 3: Modify the message that the program writes out to the screen in Simple2.java by changing the value of the variable.

Output in Java

The programs we've seen so far contain "output" statements, because they each write something to the screen. There are several ways to do output in Java; we're just going to look at one today.

The output of these programs is what we call "console output", because data is printed out to the console (another name for the terminal or xterm). In order to do console output, we use one of Java's many built-in classes. (One of the nice things about Java is that, not only can we write our own classes, but we can use other classes in our programs, either ones we write or ones that Java provides.) This class is the System class. System contains attributes and methods for doing various things, such as writing out data and errors to the console, exiting a program, etc. The System class contains an object named out, which knows how to write to the console. Finally, println() is a method (action) associated with the out object. So, System.out.println("...") is how we indicate that we want to write out something to the console.

Java input

Just as there are several ways to output data in Java, there are also several ways to input data to Java. We'll look at one today and one later on in the term.

Save Simple3.java to your lab1 directory. Compile and run the program. What do you see this time?

Now look at the program in NEdit. You should notice several differences between this program and the previous program. First, we are "importing" all of the classes in the java.util package. (A "package" in Java is a set of related classes that are pre-written for us.) The java.util package contains "utility" classes that are useful to many types of programs, such as a random number generator and classes that deal with groupings of items. The class that we're using here is the Scanner class, which handles getting input from the user in various ways (console, file, etc.). Second, we have two more variables that we've declared. These variables will store whatever the user types in to the console window (in this case, your name and your partner's name). Third, we have a statement that contains the word new. Statements that contain the word new are used to create objects. Here, we're creating an object from the Scanner class. In most cases, we need to create an object before we can use it; we create a Scanner object, name it "input", and use this object to read in data from the console (that the user has typed in). (We'll talk about this more in class, so hang in there.)

Now make the following changes to the code:

  1. Change the variable named name2 to age, and change its type from String to int.
  2. Replace lines 27 and 28 with the following lines:
    System.out.println("Enter your age: ");
    age = input.nextInt();
  3. Change the output message to
    "Your name is "+name1+" and you are "+age+" years old. Welcome!"

Compile and run the program.

EXERCISE 4: What happens if you enter a number when prompted for your name and a name when prompted for your age? If you get a message, what did the message say? In words, explain what you might do as a programmer to prevent a user from entering incorrect input. (I'm not looking for an exact answer, just a general idea as to the approach you'd take.)

More exercises

Submit your answers to these exercises, and the ones above, via hsp. (Note: you can submit entire directories using hsp, so it may be easiest to submit your entire "lab1" directory.) Answers to the questions should be in a text file named "lab1.txt". You should work on and submit the answers together with your partner. Only one of you should submit your lab with hsp, but make sure that you indicate in the lab1.txt both of your names.

  1. Modify Simple3.java again so that it asks for the names of you and your partner and both of your ages, and displays the information on separate lines. Save this program as Simple4.java.
  2. Go back to Simple.java, and replace System.out.println(...) with System.out.print(...). What is the difference between the println() and print() methods?
  3. Modify Simple4.java by doing the following:
    1. Add your age and your partner's age and write this out to the console. (HINT: The addition operator in Java is "+".)
    2. Subtract your age from your partner's age and write this out to the console. (HINT: The subtraction operator in Java is "-".)
    3. You've no doubt noticed that sometimes we use input.next() to get input from the user, and sometimes we use input.nextInt(). Based on what you observed, what is the difference between input.next() and input.nextInt()?

Instructions for using hsp are here.


Authored originally by Amy Csizmar Dalal. Modified by Dave Musicant.