Computer Science 210 Lab 1: Java Programming with "Class"
Due: Jan 30, 2001

Objectives and Overview: This lab has two goals: to become familiar with writing and running Java programs using the CodeWarrior system on a Mac PowerPC, and to explore the use of classes to help solve problems.

Warning: Be sure to read the lab carefully. Much of it is simple and a review, but there are a number of details which will be new and essential to successful completion of the lab. This also applys to the tutorial on writing Code Warrior applications.

Part 1 - Complete the Java Tutorial

The document Writing CodeWarrior Java Applications contains a complete tutorial for starting CodeWarrior on the Macs, and creating a new project from scratch. Please complete this tutorial before going on to Part 2. Even if you have taken 101 within the last year you might find this helpful as we are using a new version of code warrior this term.

A great deal of the material in the course will be accessed through a special folder. This will be slightly different than what you used in 101 (for now anyway). The folder is accessed through the chooser (use the Apple menu and select the chooser). Once in the chooser select "AppleShare." This will change your options in the right half of the window. Click on the button "Server IP Address". This will activate a new window. Type in "Collaboration" and hit "Connect." From there you can log into the folder using your email account name and password. You will each have your own folder within this folder and you can use that area to save your work at the end of every session. Please note that the best way to handle this is to work from the desktop. E.g. copy things from your folder to the desktop, work off the desktop version, then when you are done, recopy back to your folder. Please be careful to log off when you are done with your session. The easiest way to do this (after you have saved your work) is to drag the Collaboration folder to the trash.

The course folder has several items inside it (see below):

The User folder is where the individual folders are for you to save your work. You should have access to your folder and no others in this area.

Instead of a drop box, this term you will create a personal drop box inside your own folder. Just make a folder called "Drop Box" and when you are done with your assignments, copy them into that folder. To ensure your identity (since I'll then copy your assignments to my own desktop), please affix your name to the project folder before submitting it. For instance, if I have just finished writing and testing the program in folder Lab1, I would rename it Lab1.echown before dragging it to my Drop Box. A program is not done until it is in your drop box!

The Course Material folder will be critical throughout the term. You will spend a lot of time copying documents back and forth between the desk top and the course folder. Make sure when you are working on a program that you are working on a desktop copy. Always remember to copy that version back to your personal folder at the end of your session.

Part 2 - Yet Another Pig Latin Program (YAPLAP)

The following program is ridiculous and made up, but demonstrates some useful Java concepts. An electronic copy is also in the Course Material folder noted above, for your use. There are several other files there which will also be required for this lab.

public class atinlay {
// a pig latin translator for 9  words
    public static void main (String args[]) {
        // build and fill out an array of 9 translations
        Association dict[] = new Association[9];
        dict[0] = new Association("a","a");
        dict[1] = new Association("bad","adbay");
        dict[2] = new Association("had","adhay");
        dict[3] = new Association("dad","adday");
        dict[4] = new Association("day","ayday");
        dict[5] = new Association("hop","ophay");
        dict[6] = new Association("on","on");
        dict[7] = new Association("pop","oppay");
        dict[8] = new Association("sad","adsay");
        
        for (int argn = 0; argn < args.length; argn++) {
            // for each argument
            for (int dictn = 0; dictn < dict.length; dictn++) {
              // check each dictionary entry
              if (dict[dictn].key().equals(args[argn]))
                 System.out.println(dict[dictn].value());
            }
        }
    }
} 

This program translates a series of strings (words) that appear as run-time arguments into their "pig Latin" form, provided that they can be found in the dictionary dict. This dictionary is an array of so-called Associations. Associations are your first example of a data structure. You can think of an Association as being a pair with a key and a value. Each entry in the dictionary is an association with the original word (the key) and its pig-latin equivelent (the value). The keys and values can be extracted with the key() and value() methods.

The program demonstrates a number of common Java constructs -- assignment, arrays, for loops, if statements, and output statements. To exercise this program, follow the steps below (starting from the project Lab1 which you created in Part 1.

  1. Update the atinlay.java program in your project to be the same as the one above. There are two ways to do this. You can open the file from the Course Material folder and cut and paste the appropriate parts into your file. Or you can remove the version in your project and add the version in Course Materials (after you have copied it into your folder). Some things to be careful about: 1) The version created with your project might have the statement "package Lab1" in it (if you didn't delete the package name while creating the project). Either all of your files must have this statement or none of them should have it (I'd reccommend the latter). Packages define groups of files which only work together. 2) Please keep (and update) the comments at the top of the original version. Your program should always have this basic information and you should fill in the appropriate description, author, etc.
  2. Copy the rest of the files from Course Materials, except for WordFreq.java (Association.java, Assert.java and ReadStream.java) into your project folder.
  3. Add all of those files to your project with the Add Files menu command
  4. Rename the Edit->Lab1Debug Settings...-> Java Target->Parameters to hop on pop

The last step should result in the following appearance of the Lab1Debug Settings window:

When you run the program the parameters you just put in will be automatically loaded into the array called args. This is a useful way of passing information to programs without user input, it is also likely to be the last time we do this all term. After you put in the parameters and save it you are ready to go. The project window should now look like this:

Now Make and Run this program. The output should appear in the Java Output window.

Part 3 - Modify the Program for Stream Input

One of the handicaps of this program is that it takes its input from a short list of arguments, rather than as a stream of input strings typed by the user at run time. The program could be modified to take its input from the input stream, by using methods from the ReadStream class (see Migrating from Pascal to Java, section 6). There you should find that the readString method can be used to read a single word as a character string.

One of the key goals of this course is for you to learn how to function autonomously as a programmer. While it is great to have professors and T.A.s available in lab, this won't be the case in the rest of your life. A common trick that programmers use when learning new languages or techniques is to look at other programs which do similar things. In this case I have provided the WordFreq.java file as an aid to help you figure out how to read information from files. Most of this file can be ignored, but it contains a basic reading loop that will be useful throughout this course. In this case it reads until the user types in a q or a Q. You should be able to use the same technique in your program.

Modify your program so that the user can type a series of input words in the System.in window and the program will translate each word typed to its pig Latin equivalent, if it is in the dictionary. Words not in the dictionary would not be translated.

You might find that the WordFreq.java program (also in the course materials folder) provides some extremely helpful hints as to how to proceed with this portion of the assignment. It reads in a list of words and tracks how frequently they occur.

This exercise gives you practice with the Java stream input facilities used in the example program in Part 1. It should also give you some experience with arrays and Strings (see Migrating from Pascal to Java, sections 2 and 5). You should freely borrow from the input conventions in that example program to suit the needs of this problem.

Lab 1 Deliverables:

Now submit your revised Java program from Part 3 of this lab by dragging the project folder to your personal Drop Box. Don't forget to rename your project folder by affixing your name to it. For example, if I were submitting it, the project folder would be called Lab1.echown, not just Lab1.

Also, hand in a hard copy listing of your program with your name on it.

(Optional) If you have time, add the capability of converting any word to its pig latin equivelent.