CS 12 Spring 2004 -- Lab 1


A note on Academic Honesty

For assignments in this class, you are allowed to discuss the problem, and your thoughts on the solution, with others. You are not allowed to exchange, in any way, code or anything that resembles code. If in doubt about whether a certain kind of interaction is allowed, ask me.


Logging into romulus from SMudd 014

When sitting at one of the machines during lab time (or, for that matter, at any one of the public Windows systems maintained by the Computer Center), login will be a simple matter. The instructions presented in this section will work this week, but will be superceded by new instructions next week. Note that you need to use your College username and password (that is, the same one you use to retrieve your email.) Follow the steps below to login:

  1. Login to the Windows machine using your College email username and password.
  2. Select Start -> All Programs -> Courses -> VNC Client.
  3. You will be presented with a window that will ask for a server to which to connect. Enter romulus and press return.
  4. You will see a new window that contains a complete graphical interface to romulus, the Linux system on which you will do your work for labs and projects. You will see a login screen. Enter your username and password.

Once you've logged in, you can skip down to the section below entitled Your Assignment.


Logging into romulus from elsewhere

For this course, I will assume that you will use romulus.amherst.edu (a Linux server) to do your work. While you are welcome to do your work on another system (perhaps your own desktop/laptop machine), your code must compile, run, and be submitted on romulus.

You will be able to login and interact graphically with romulus from just about any machine with a high-speed connection. This section will desribe how to establish a VNC connection (which provides the graphical interaction) that is tunneled through an SSH connection (which provides encryption of your communication). The use of SSH is a good habit considering that the Amherst College network, as well as many other networks connected to the Internet, are open and rather insecure. If you are connecting from outside of the Amherst College network, you must use SSH tunneling to establish a VNC connection. I recommend that you use SSH tunneling under all circumstances.

The following steps will allow you to establish a VNC connection using SSH tunneling:

  1. Connect to romulus using an SSH client configured to establish the tunnel: The choice of SSH client and the description of how to use it depends on the type of system that you are using. Below are instructions for Mac OS X, Linux/UNIX, and Windows systems. If you have another type of system (such as Mac OS 9), contact me and I will attempt to point you a web page or a person that can help you.

  2. Open a VNC connection: Again, the software used in system dependant, and I describe how to use software for each of the systems described above:

  3. Login to romulus: Now that you are graphically connected, simply use the login window. A windowing environment will appear.

Now that you're graphically interacting with romulus, move on to the next section (Your Assignment).


Your Assignment

Now that you are connected to romulus, you will obtain a Java file, 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: There should be, along the bottom of your screen, a set of icons. Among these should be an icon that looks like a computer screen with a little paw-print in the lower right corner. Click that, and you should get a new terminal window. Within that, a shell -- the command line program with which you will interact -- will start and you will be given a prompt.

  2. Run a setup script: Just once, you need to run a script that will perform a bit of setup for your account. Enter the following command at the shell prompt:

    source ~sfkaplan/public/cs12/setup
  3. Make a new directory: Create a subdirectory within your home directory that will store the files that you use for this lab, like so:

    mkdir lab-1
  4. Change into the new directory: Change your current working directory to be the one that you just created, like this:

    cd lab-1
  5. Copy a Java code file: At the prompt, enter the following command copy a Java source code file from my directory into your own:

    cp ~sfkaplan/public/cs12/PrintGrid.java .

    Don't forget the trailing dot (.) -- It tells the shell that you want the file copied into the current directory.

  6. Start XEmacs: We will use a powerful program known as XEmacs to edit Java source code. To start this program, simply type the following command at your shell prompt:

    xemacs &

    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.

  7. Open the file for editing: In your XEmacs window, select File -> Open..., and you will see, along the bottom of the window, a prompt that looks like this:

    Find file: ~/lab-1/

    XEmacs is asking you for a full pathname to the file that you want to open, and it's also filling in your current directory as the start to the pathname. In this case, you do want to open a file in your current directory, so just add to the line to make it read:

    Find file: ~/lab-1/PrintGrid.java
  8. Wrap lines and add color: XEmacs will open the file that you copied in its window. We want to make XEmacs automatically wrap around lines that are longer than 72 characters, and we want it to use color to highlight the code itself. In XEmacs, you can use the Alt key like a shift key; however, XEmacs 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, XEmacs will show you that you've begun a command with M-x -- many XEmacs commands begin this way.

    The (not at all intuitive) command to get XEmacs to automatically wrap lines is:

    M-x auto-fill

    The (equally opaque) command for color-coding your text is:

    M-x font-lock-mode
  9. Edit the file: You will find, in the file, a comment that begins "YOUR TASK". It describes what you need to do to complete this small program -- specifically, implement a pair of nested loops to produce some output.

    As you edit the program, notice some of the code that is already there. Note how input is performed, and how one method can call another. Also note that the code is well commented, consistently formatted, and uses a consistent and descriptive naming scheme for methods and variables. You should write your code in a similar fashion!

  10. Compile the file: At your shell prompt, use the following command to compile your code:

    javac PrintGrid.java

    If there are errors in your code, the compiler will tell you where in the file that they were found. You must go back to your XEmacs window and try to correct the errors, and then compile again.

  11. Run the program: To run what you've created, enter the following at the shell prompt:

    java PrintGrid

    The program may not execute correctly if there are errors in your code. If that is the case, you must edit the code to correct the errors, recompile the new code, and then run the program again. Be sure to remember to recompile after you edit the code.

As an aside, note that XEmacs is an exceedingly flexible and powerful program (although it does not have the most user-friendly interface). You would be well served to take the time to open an XEmacs window and select Help -> Tutorials -> English (or whatever language suits you best!). This will open a lengthy tutorial on XEmacs use that will show you a large number of capabilities that make XEmacs so well suited to editing programs and other text.


A second project

Copy the file ~sfkaplan/public/cs12/PascalsTriangle.java into your directory. Open the file, and you will find a method named makeTriangle() that has no body. You must fill in the body of this method.

Specifically, this method should create a two-dimensional array of integers that hold Pascal's Triangle. (It will be presented during the lab if you're not familiar with its structure.) A pointer to that array should be returned to the caller (the main() method) which will in turn call on printTriangle(), which is a method that is already written to emit the triangle.


A third (and final) project

Create a program with a class named Fib that calculates a given Fibonacci number, where the nth Fibonacci number is defined as:

Fib(n) = Fib(n-2) + Fib(n-1)

Your program should obtain n from the user using the User class at runtime, and must rely on a recursive method named calcFib to perform the calculation. The result should be emitted to the user, and the program should then terminate.

When you write this method, try mapping out how the activation stack would change over time as this doubly recursive method proceeded. It might, some day, resemble an exam question (hint hint).


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:

cs12-submit lab-1 PrintGrid.java PascalsTriangle.java Fib.java

The first argument to the cs12-submit program is the name of the assignment, which in this case is lab-1. The second and third arguments are the names of the files to submit, which here are PrintGrid.java, PascalsTriangle.java, and Fib.java.

Be sure to check the output of the cs12-submit program carefully to be sure that it did not indicate any errors. If you want to be doubly sure that your assignment was received, you can do the following:

(romulus)> cs12-check-submission lab-1

This program will indicate to you whether or not you have successfully submitted work for lab-1. Note that if you submit more than once, the most recent submission will clobber any previous submissions. Thus, if you want to correct something about your work, be sure to submit your entire set of files each time.

This lab is due on Sunday, February 8th at 11:59 pm!

Scott F. Kaplan
Last modified: Mon Feb 2 10:46:30 EST 2004