Introduction to Computer Science I

Lab 1


Logging into the server

For labs and projects, all of your work will be done on the college's UNIX servers, remus and romulus. For the remainder of these instructions, I will assume that you will use romulus, although both machines are equivalent and you may use either. These machines will provide a new kind of computing environment for you, and they will have all of the software necessary for programming in Java.

You must first be able to login to romulus. When sitting at one of the machines during lab (or, for that matter, at any one of the college's public Windows machines), logging in will be a simple matter. Note that you need to use your College username and password (that is, the same one you use to retrieve your email or login to the CMS pages.) Follow the steps below to login:

  1. Login to the public Windows machine using your College email username and password.
  2. Select Start -> All Programs -> Course-related -> VNC Viewer.
  3. You will be presented with a window that will ask for a server to which to connect. Enter either romulus and press theReturn key.
  4. You will see a new window that contains a complete graphical interface to the server. You will see a login screen. Once again, enter your username and password, this time logging into the UNIX server. Given a few moments, you will, within the VNC Client window, see a graphical environment with various icons; you are now logged in.

A bit of setup

Now that you are connected to romulus, you will obtain a pre-written file with Java code, make some changes to it, compile it, and run it. This exercise will introduce you to many of the tools you will use for this course. Follow these steps:

  1. Open a shell: At the top of your screen, there is a set of menus. Click on Applications and a menu will drop down From that menu, move to Accessories, which will reveal a sub-menu. In that sub-menu, click on the Terminal option near the bottom. You will see a window, labeled Terminal appear with a shell -- a program that accepts commands that you type, providing a command-line interface.

  2. Run a setup program: Just once (only once!), you need run a small program that will perform a bit of setup on your account for CS 11. Again, note that once you issue the command below, you never need to do so again. Here's the command:

    source ~sfkaplan/public/cs11/setup

    Note that once you've entered the command, you will simply get the command prompt again. That's a good thing. It is common, for UNIX commands, for no news to be good news. Many commands will only show output if something has gone wrong.

  3. Make a new directory: Right now, your shell is in your home directory---something like a My Documents folder on the UNIX server's hard disks for your user account. Within that home directory, you need to create a subdirectory---a folder within your home directory---that will store the files that you use for each lab. For this lab, you should make a subdirectory named lab-1. You can do that by using the mkdir (short for fmake directory) command, like so:

    mkdir lab-1
  4. Change into the new directory: For the files that you copy and create to be placed into your newly created lab-1 subdirectory, you need to make your shell move into that subdirectory. You do that by using the cd (change directory) command, like this:

    cd lab-1

Working with Java code

Now that you've done a little preliminary setup, you can obtain some Java code and make it run.

  1. Copy a Java code file: I have written a simple program that you can copy from my directory space into your lab-1 subdirectory. To do so, you need to use the cp (copy) command. In your shell, enter the following command:

    cp ~sfkaplan/public/cs11/Hello.java .

    First, yes, that is a tilde(~) character before my username (sfkaplan). Second, don't forget the trailing space and then the period (.) -- It's not a typo, and it really needs to be there. If you forget it, you will see an error messsage.

    What did I just type? The cp is the UNIX command for copying files. The ~sfkaplan/public/cs11/Hello.java is the name of a file. More specifically, ~sfkaplan is my home directory. public is a subdirectory within my home directory, and then cs11 is a subdirectory within public. Hello.java is the actual file with Java code in it. The trailing period is the UNIX shorthand for "the shell's current subdirectory", which here is your lab-1 subdirectory. So, this command directs the system to copy the Hello.java file from one of my subdiretories into your current subdirectory.

  2. Check that the file is there: Use the ls command (that's a lower-case L, not the numeral one to see a listing of the files and subdirectories in your current directory:

    ls -l

    You should see the file Hello.java listed. The -l part of this line tells the ls program that you want not just the file names, but more information on when the file was modified, etc.

  3. Look at the Java code: Examine the Java code that you just copied. Look through it, and make sure that it makes some sense to you. Use the following command to print the Java code to the screen:

    cat Hello.java
  4. Compile the file: Translate this Java code into something that the machine can understand by using the javac program:

    javac Hello.java

    This program compiles your code; once again, for this command, no news is good news. When it is done, you should list the files in your lab-1 directory again, and you will see a new file named Hello.class. This file contains the ready-to-run program that the CPU can process.

  5. Run the program: To carry out the steps written in that Java code file, enter the following at the shell prompt:

    java Hello

    Notice that you just give the name of the program, Hello, and not the .java or .class extensions. You should see a couple of friendly little lines of output printed to the screen.


Modifying the program

It is good to compile and run a program, but to do your own programming, you need to be able to write or change a Java code file. Next, you will make some changes to the Hello.java file, and see the consequences of those changes.

  1. Start Emacs: We will use a text-editor named Emacs to edit Java source code. First, note that a text-editor is somewhat like a word processor, but not really. You can write in both, but the latter allows you to control formatting for placing written work on a printed page. A text-editor is only concerned with editing plain text on screen. Emacs is a powerful, flexible tool for programming, but it is not terribly user-friendly, and it will take some time before you are comfortable with it. However, it is worth your effort to get used to it, as it is terribly helping for writing Java programs. To start Emacs, simply type the following command at your shell prompt:

    emacs &

    Note that for UNIX shells, placing an ampersand (&) at the end of a command causes that command to be executed in the background. That is, the shell won't wait for the program to finish before giving you a new prompt. You will see the Emacs window appear, and you will also be able to issue more commands to the shell. You will therefore be able to edit your Java code in the Emacs window while compiling and running your program in the terminal window.

  2. Open the file for editing: In your Emacs window, type control-x and then control-f -- that is, hold the control key down as you would the shift key, and then type x and then f. In the future, we will write this key sequence as C-x C-f. Once you type that sequence, you will see, along the bottom of the window, a prompt that looks like this:

    Find file: ~/lab-1/

    Emacs is asking you for the name of the file you want to open (where it is also showing you that your current subdirectory is the lab-1 directory within your home directory (represend). To open our Java code file within the lab-1 subdirectory, add the filename to the line to make it read:

    Find file: ~/lab-1/Hello.java

    Press Return, and you will see the file appear in the Emacs window.

  3. Add color: We want to make Emacs use color to highlight the code itself, making the code easier to read. In Emacs, you can use the alt key like another control key; however, Emacs refers to this key as the meta key. Thus, if I indicate that you should enter M-x as part of a command, you should hold down the alt key and press the x key. When you do so, on the bottom of its window, Emacs will show you that you've begun a command with M-x -- many Emacs commands begin this way.

    The (not at all intuitive) command to get Emacs to highlight your code in color is:

    M-x font-lock-mode

    That is, you will type the M-x, and then type out the command font-lock-mode. Your code should then become color-coded. This color-coding can help you recognize when you've made a mistake, forgetting a parenthesis, a quote, an important keyword, etc., since doing so will change how the text is colored.

  4. Try some other, useful Emacs commands: Here are some Emacs capabilities that you probably want to know about:

    A note about indentation: It will be very helpful, when writing Java code, to indent each line to a particular length. The pattern for this indentation is something we will discuss in the upcoming days. However, it is useful to know that Emacs knows how to indent Java code all by itself. With each line of code that you write or modify, just press the Tab key while the cursor is on that line. Emacs will intelligently indent that line to its appropriate depth. Note that this capability not only makes your code better organized and easier to read, but it also can help you find errors. If you forget a closing parenthesis, a bracket, a quote, or a semicolon, your line will be indented to some odd-looking depth, tipping you off that some previous line (likely the immediately preceeding one) contains an error.

  5. Edit the file: Maneuver yourself in the file until you are at the beginning of the first line that says System.out.println. This command is one that you use to make Java print some text to the screen when you program runs. Change the text that currently appears between the double-quotation marks, altering the progranm to print something different. Also, add another System.out.println to print something new. Be sure to include the semicolon at the end.

    Once you've changed the file, save the changes (see the Emacs command listing, above).

  6. Re-compile and re-run: Every time that you edit the source code, you must save your changes and compile the code again. So, click on your terminal window, and issue the javac command again to your shell, as you did before. Then, issue the java command as before. You should see your modified output appear.


Writing your own program -- Trying out type-casting

Now you're going to create a new program from scratch. Follow these steps to create a new Java code file, write some code that works with some numeric variables, compile it, and run it:

  1. Create the file: Click on your Emacs window again. To create and open a new file, type the Emacs command C-x C-f. Once again you will see the prompt at the bottom that asks you for the name of a file within your lab-1 subdirectory. This time, complete the line so that it reads:

    ~/lab-1/Numbers.java

    Press Return and you will get a new, blank space within your Emacs window in which to type some new stuff.

  2. Enter the magic boilerplate: Recall that every Java code file contains some standard, odd-looking text that is part of every file. Enter the following text for this new program (notice the indentation!):

      public class Numbers {
    
          public static void main (String[] args) {
    
          } // main
    
      } // class Numbers
    	
  3. Write some Java instructions: Now that you have the boilerplate entered, move the cursor to the blank line that follows public static void main... This is where Java assumes your program will begin when you run it.

    For this program, I want you to experiment with different ways of using type casting. Below are a few beginning lines of code that you should add to your program. These create a few numeric variables---some of them int, some of them double---and then perform some basic arithmetic on them. The System.out.println lines then show the results of that arithmetic so that you can see what the program did:

              int x = 4;
              int y = 3;
    
              double d1 = x / y;
              double d2 = (double)x / (double)y;
              double d3 = (double)(x / y);
    
              System.out.println("No casting:         " + d1);
              System.out.println("Casting each:       " + d2);
              System.out.println("Casting the result: " + d3);
    	
  4. Testing your code: Save your Java code. Then click back over to your shell and compile the program...

    javac Numbers.java

    ...and then run it...

    java Numbers

    What do you see? Are those the results that you expected?

  5. One last modification: Go back and add one more calculation to this program. Specifically, add a line that type-casts x, but not y, and stores the result in a new double variable named d4. Then add a command that prints the contents of d4 to the screen. Save, compile, and run this modification. What happens with d4? Is it what you expected? Can you explain it?

Submitting your work

When your work is complete, you will be able to use a special program to submit your work and to be sure that it was received. For this lab, you will submit your work with the following command at the shell prompt:

cs11-submit lab-1 Hello.java Numbers.java

The first argument to the cs11-submit program is the name of the assignment, which in this case is lab-1. The additional arguments are the name of the file to submit -- that is, your source code.

Be sure to check the output of the cs11-submit program carefully to be sure that it did not indicate any errors. If the program completes without an error message, than it was submitted successfully.


This lab is due on Thursday, February 7th at 11:59 pm

Scott F. H. Kaplan
Last modified: Fri Feb 1 21:59:06 EST 2008