Computer Science 136
Data Structures and Advanced Programming

Williams College
Fall 2005


Lab 1: Java, Unix, and the Silver Dollar Game
Due: Monday, September 19, 2005 at 9:00 AM

Before Lab

You should come to lab with a proposed design for your Coinstrip class that you will write for the "Lab Program" section of this assignment. These designs may be handwritten or typed. They will be checked at the start of lab and will constitute a small part of the grade for this week's lab.

Getting Started

During lab today, you will learn how to use the Java environment from the Unix command line on the CSLab Mac systems and get some practice writing Java code.

  1. Go through the Unix tutorial handout. This will teach you how to log in and out of the machines, use basic Unix commands, and edit files with Emacs.
  2. Log in. Start X11, the X Window System, by clicking on the rectangular while icon with an X on it in the dock at the bottom of your desktop. This will bring up an "xterm" command window.
  3. To set up your account to run the correct version of Java, you should enter the command
    source /Network/Servers/cortland.cs.williams.edu/Volumes/Courses/cs136/bin/136
    
    in the command window each time you log in. Rather than type this command every time, you can make it happen automatically by adding it to the file .local_bashrc in your home directory.
  4. Identify the function of and experiment with these Unix Commands:
     
    ls         cd         cp         mv         rm         mkdir      pwd
    man        chmod      cat        more       grep       head       tail
    ln         find       rmdir      wc         diff       tar
    

    Identify the function of and experiment with these Emacs Commands:

     
    C-x C-s    C-x C-c    C-x C-f    C-x C-w    C-g        C-a        C-e        
    C-d        C-_        C-v        M-v        C-s        C-r        M-%
    

    Learn these commands - you will use them often. Hints can be found in the Unix and Emacs web pages on the course website.

  5. Make a directory in your Unix account for CS 136 work (perhaps "136" or "cs136" might be reasonable). Use the chmod command to restrict access to this directory so only you can read the files. Ask a classmate, TA, or instructor to verify that he or she cannot see the contents of this directory.

    Make a subdirectory lab1 in this new directory for files related to this lab.

Warm-up Exercises

  1. Write, compile, and run a Java program under Unix that prints the first ten odd numbers. Don't just use 10 println statements - use a loop. Call it Odd.java. To submit your program, once it is working, you can type "turnin -c 136 Odd.java" at the Unix prompt.
  2. What is printed by the following program? (try it out if you're not sure.)

    public class Example {
        protected int num;
    
        public Example(int initial) {
            num = initial;
        }
    
        public int getNum() {
            num = num + 1;
            return num;
        }
    
        public static void main(String args[]) {
            Example first = new Example(10);
            Example second = new Example(20);
            System.out.println(first.getNum());
            System.out.println(second.getNum());
        }
    }
    
    What would be printed if we changed the declaration of num to be
        protected static int num;
    
    instead? What does this tell you about static instance variables? Explain briefly, writing your answer in a file lab1.txt.

Lab Program

This week, we will write the Silver Dollar Game at the end of Chapter 3. You should come to lab with a written design of the program.

For your design, you should first decide on an internal representation of the coin strip. Make sure your representation supports all of the operations necessary, i.e., testing for a legal move, printing the board, testing for a win, moving pieces easily, etc. You should think about alternative designs and be able to justify your decisions. You may read ahead a little to Vectors if you like, but the lab can be implemented with arrays.

Once you have decided on a representation, write down the set of operations supported by your data structure. In other words, what are the public methods of CoinStrip, and what do they do?

The document you bring to lab should include both a description of the representation and the operations on it. This initial design will constitute a fraction of your grade on the lab assignment.

Write your program in a file named CoinStrip.java. When you have finished the program, turn in CoinStrip.java using the turnin utility-- type turnin -c 136 CoinStrip.java.

Finally, answer the thought questions at the end of the lab. Put your answers in the file lab1.txt and submit that file as well.

As in all labs, you will be graded on design, documentation, style, and correctness. Be sure to document your program with appropriate comments (use Javadoc!), including a general description at the top of the file, a description of each method with pre- and post-conditions where appropriate. Also use comments and descriptive variable names to clarify sections of the code which may not be clear to someone trying to understand it.