Computer Science 210 Lab 1: Java Programming with "Class"
Due: September 8, 1997

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. Please bring a diskette to thelab session on Wednesday.

Part 1 - Complete the Java Tutorial

The document Writing CodeWarrior Java Applications contains a complete tutorial for starting CodeWarrior on the Macs, and writing and running a Java program. Please complete this tutorial before going on to Part 2.

When you finish this tutorial, notice that there is a folder called Courses -> CS210(Tucker) on the Mac desktop, which has several items inside it (see below):

The Drop Box folder is the place where you may submit completed Java programs during the semester. This cana be done simply by dragging your program file to this folder. To ensure your identity, please affix your name to the program file before submitting it. For instance, if I have just finished writing and testing the program myfirst.java, I would rename it myfirst.atucker.java before dragging it to the Drop Box.

The structure.zip file is a precompiled library of all the classes used in your text Java Structures. We'll not use that for the time being, but it will be useful later in the semester. The source folder contains the Java source code for all these classes, and the examples folder contains all the example Java programs in your text (which use these classes).

Part 2 - Yet Another Pig Latin Program (YAPLAP)

The following program appears on page 15 of your text. An electronic copy is also in the source folder noted above, for your use.

import structure.*;

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, which are described in your text more fully on pages 14-15.

The program exhibits several Pascal-like ideas in Java form -- 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. Add a copy of this program file, called atinlay.java, from the examples folder into your project
  2. Add copies of the the files Association.java and Assert.java, also from the source folder
  3. Remove the file myfirst.java from your project
  4. Rename the Edit->TrivialApplication Settings...->Java Target->Main Class to atinlay
  5. Rename the Edit->TrivialApplication Settings...->Java Target->Parameters to hop on pop

The last two steps should result in the following appearance of the TrivialApplication Settings window:

The project window should now look like this:

Now Make and Run this program. The output should appear in the Java Output window just as it is shown on page 15 of your text.

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.

Modify this 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.

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.

Note also that the end of input is signaled by typing command-D on the keyboard. This will cause the eof condition to become true, so that it can be used to control the input loop.

Lab 1 Deliverables:

Now submit your revised Java program from Part 3 of this lab by dragging it to the CS210/Tucker -> Drop Box folder. Don't forget to rename your program file by affixing your name to it. For example, if I were submitting it, the program file would be called atinlay.atucker.java, not just atinlay.java.

Also, hand in a hard copy listing of your program with your name on it, along with the answers to the following questions.

  1. Answer question 1.1 on page 20 of your text.
  2. Answer question 1.2 on page 21.
  3. In its present form, the atinlay.java program displays the pig latin translation for only those input words that appear in the dictionary dict. What additional code would be required to modify the program so that it displays in the Java Output window one word forevery word typed as input -- either its translation or the original word (if it does not appear in dict)?
  4. (Optional) If you have time, add the capability discussed in question 3 to the program you are submitting.