MIT 6.80s:
Interactive Programming on the Web

Programming Project: A Pig Latin Translator

In this project, you will write a java applet. The applet will translate text typed by the user into Pig Latin. We have supplied you with the Graphical User Interface (GUI) that you'll need to interact with your applet under Netscape or appletviewer. Your job is to write the interactive control loop that forms the heart of the PigLatin class.

Project Outline

  • Introduction
  • A Pig Latin Translator
  • Creating a PigLatin Class
  • Compiling Your Code
  • The Translator Class
  • Run
  • Evaluating Your Applet
  • A PigLatin Translator

    To begin, you need to know something about the environment you'll be working in. You can start by viewing the url

    http://web.mit.edu/6.096/www/psets/PigLatin/
    This shows you the GUI you will be interacting with. You can type to the input TextField. When you hit return (to tell the GUI that you are done typing), it will echo back the text that you have typed. This applet is running using a precompiled PigLatin class file. We have not given you the corresponding .java file: writing it is your job.

    Creating a PigLatin Class

    In order to create the source code for the PigLatin class, you'll need a file called PigLatin.java. (It must have this name because the Translator class we've given you assumes that that is what it is called. In other words, the name PigLatin, and some of its behavior -- like the kind of information it needs when it is created -- is hard wired into the Translator class.) So pull out your editor (remember emacs?) and open a file called PigLatin.java in this directory (********). As always, if you are not sure, ASK.

    Now, what goes into this file? Depending on how adventurous you are, you can do more or less of this yourself. (We have actually supplied you with a template file in *********. You can load it directly, and not try to write the file yourself at all, or you can write it yourself from scratch, or something in between. For example, you can go back and forth between the template and your file, trying to write as much as you can but looking at the template for ideas and to cross-check.) In then next paragraphs, we will lead you through the construction of the text in the template file.

    The first thing that you need is to say what this file is all about. You are using it to define a kind of object, or class. The name of the class is PigLatin. In java, the way you declare something is by first saying what type of thing it is, then giving its name. (Remember int i?) Oh, and this particular class delaration needs to be followed by the (currently magic) incantation extends Thread. This will be true for every interactive control loop you write, and by later this week you should understand why.

    Advanced: If you want to get fancy, you can even say how widely available you want to make this class: public. This is only important if you are going to be worrying about hiding some things from other objects or code-writers (e.g., using packages), though.

    After the declaration of the class comes the body. What does the body get wrapped in?

    Now, you need a constructor method. This is some code that tells java how to make a PigLatin object. Part of what is hard-wired into the Translator class is the fact that each PigLatin object gets created with a particular Translator object (its GUI). So the PigLatin constructor method will need to say

              PigLatin ( Translator gui ) {
    
              }
    

    This says exactly what the preceding paragraph does: each PigLatin object is created with its own Translator object, which, inside this constructor method, is called by the name gui.

    Advanced note: constructor methods violate the type-of-thing name-of-thing rule. Since they return a thing whose type is the same as the name of the constructor (e.g., in this case, PigLatin), they save space by only saying the type/name once.

    Now, you need to fill in the body of the constructor method. It needs to do two things: save away the Translator for later, and call its own start method.

    To do the first, you'll need a place to store the Translator. (Hint: you will need to declare something, with a type and a name. Where does this declaration go? What is it that you are declaring?) Use the thing that you have declared, together with the constructor method's parameter (Translator gui), to store away the Translator for later.

    To do the second, you need to call the method named start with no arguments. The method belongs to the object you are creating: the object whose code you are writing. To invoke a method, write the name of the object whose method it is, followed by a . and the name of the method, then the argument list in parentheses:

    object.method( arg1, ...argn );

    Advanced note: Although you are calling a start method, you will notice that you are not going to define one for PigLatin. PigLatin extends, or inherits from, a class called Thread. It is actually Thread, not PigLatin, that implements the start method, and it is this start method that you're calling. Tricky, huh?

    The last part of the class definition is a method called run, also with no arguments (and this time returning nothing, i.e., with return type void). This method is automatically called once the PigLatin object is started. It is where the interactive control loop goes. To declare this method, remember the type-of-thing name-of-thing rule, and also remember that a method has to have a parameter list (enclosed in what?) and a body (enclosed in what?), even if they are empty. One piece of magic: the run method must be declared public.

    At this point, your PigLatin.java file should look like the template that we have given you, except for the comments. If you haven't done so already, take a look and see. If you find that there are differences, try to explain them. If you can't, ask someone to take a look. Or, go on to compile your code (in the next section) and see what happens.

    Compiling Your Code

    If your class is defined properly, you should be able to compile your class using javac. Try it. Don't be surprised if you get some error messages. Read through the error messages and see if you can figure out what they say. Each one should identify the file and line number that caused the problem. (Hopefully the only file listed is your PigLatin.class file!) Try looking at that line in your file, too. ****does emacs give them goto-line, or do they need to use M-x goto-line******* See if your partner can understand what the problem is. If neither of you can figure it out, ask us.

    It is, of course, possible that your file won't compile, or that it will do something other that what we've told you it would. In this case, you'll need to learn about debugging. (Everyone has to, sooner or later....) For the moment, ask us if this happens. (Later, we'll talk about debugging techniques....)

    Once your PigLatin.java file compiles, it will create a PigLatin.class file. This file contains the java byte code that actually runs under appletviewer and Netscape. You can run this code using either of those programs on Translator.html. (Type appletviewer Translator.html& to the shell or use Netscape to open the url *************.) Not much will happen, though: the Translator code is waiting for PigLatin to do something. You can tell that it's working if the Translator applet runs but doesn't do anything.

    Contrast this with what happens if you don't have a PigLatin.class file: the applet doesn't load properly at all. You can see this in appletviewer because it will pop up a window with the error message "start: applet not initialized" in the bottom left-hand corner. More error messages will print out to the shell that you ran the appletviewer from. In Netscape, there will be an error message at the bottom of the browser window, but to see the whole message you need to select Show Java Console from the Options menu. Try deleting your PigLatin.class file (by typing rm PigLatin.class to the shell) and then viewing Translator.html. (In Netscape, you'll probably have to hold down the shift key while pressing the Reload button to get this effect. In appletviewer, you can just select Reload from the Applet menu. Quite a difference! To get back your PigLatin.class file, what do you need to do?

    Project Outline

  • Introduction
  • A Pig Latin Translator
  • Creating a PigLatin Class
  • The Translator Class
  • Run
  • Evaluating Your Applet
  • The Translator Class

    Before going any further, it is time to take a break and look at some code that you are not going to be writing. (Using other people's code is an important part of object-oriented programming and modern software practice.) Your PigLatin object will have its own Translator GUI, and the meat of the run method will rely on this object.

    Rather than looking directly at the code, we suggest that you look at the documentation for the code. This can be found in **********/Translator.html. This file contains a detailed listing of the functionality provided by the Translator class. You should read the comments here, which describe Translator.java's interface. In particular, pay attention to the methods getNextWord and printWord. For each of these methods, make sure that you can answer these questions:

    Test your answers to these questions by adding a call to putWord to the body of your PigLatin run method. Where does this statement go? How do you call this method? (Hint: whose method is it, anyway?) What argument(s) do you need to give it? You can create a new String constant by putting it in double quotes: e.g., ìSalutations!î (Extra credit for identifying the source of the quotation, and its relevance to this programming project.) How do you end the statement?

    Try compiling your PigLatin file again, and once it compiles, try looking at Translator.html again. (Don't forget to (shift) reload!) What does it do this time?

    Run

    Now that you know about Translator and its getNextWord and putWord methods, it's time to build the real PigLatin run method. Unlike the simple method that you've just written -- the one that should print ìSalutations!î or some other String, and then stop -- the real run method will be an interactive control loop. That is, it needs to run (approximately) forever (or at least until the applet stops). To do this, it needs to be wrapped in an infinite loop. Replace the call to printWord with the infinite loop constructor (or stick the infinite loop after the greeting message, if you prefer).

    Now, what goes inside the infinite loop. Actually, you could try putting the greeting message there and then compling and reloading. In Netscape, you can stop bad things by going Back (use the button); in appletviewer, try selecting Quit from the Applet menu. You probably don't want to leave the greeting there....

    What you do want in the interactive loop is something which gets input as well as giving output. Remember that the point of this is to translate the words that you type to the Translator's input area into Pig Latin and then display the Igpay Atinlay back in the GUI. So, inside the loop:

    Remember: object.method( arg1, ...argn );

    Of course, this omits the important step of translating it into Pig Latin, but we'll get to that. For the moment, make sure that this code works. After you compile and reload it, Translator.html should give you an echo: whatever you type to it should be typed back in the output area.

    About that translation....You need to know a few things:

    Don't worry about getting the translation perfect -- start simple and work up. Try just moving the first letter to the end and adding ìayî. Be careful, though: what happens if you call getNextWord repeatedly in the same pass through the loop? Try acting it out with your partner. How can you avoid multiple calls?

    Remember that almost no oneÌs code works the first time. You'll probably have a wide range of problems, from bad java syntax (which the compiler should catch) to logical errors. Ask us for lots of help learning how to debug. One really basic tip: Put lots of print statements in your program. You can do this using this gui and printWord, or you can use Netscape's Java Console (**????****appletviewer) and print using a bit of magic: System.out.println( String ); (or System.out.print( String ); to print without the newline at the end.)

    Evaluating Your Applet

    So now you should have a working java applet. How well does it work? If you think that it handles some simple pig latin. (Try typing in pig latin, for example.) What happens when you type what happens? Or snowman? Or once upon a time, or This little PIGGY.... or Look! Do you see my JavaWords? ? Does your translator handle consonant clusters, capitalization, punctuation, y-as-a-vowel, ....? Where does it work? Where does it break? Make a list of things that your applet doesn't handle properly.

    Pick one of these extensions, and try to write code to handle it. You may want to use the utilities we've provided in cs101.util.StringUtils. Note that these are static (class) methods: they belong to the StringUtils class object. So, for example, you would call the firstVowelPos method by writing StringUtils.firstVowelPos(ìLynnAndreaî); You will probably also need to use an if statement, which takes the form:

    if ( booleanCondition ) {
        ifStatements
    } else {
        elseStatements;
    }
    

    Punctuation is a relatively advanced task. If you want to tackle it, you will probably need to look at java.util.StringTokenize as well as cs101.util.StringUtils.nonWordChars. To handle punctuation, you'll probably need to modify Translator.java as well as PigLatin.java.

    If you have extra time, you can always work on Ubby Dubby or some Spanish Pig Latin :o)

    When you're all done, don't forget to put a pointer to your completed applet -- or the applet itself -- on your web home page!

    -----

    ==< Onwards Or Back To: ==>

    o  A PigLatin Demonstration

    o  Some Java Documentation

    o  The 6.80s Home Page

    -----

    This page last updated by Lynn Andrea Stein on 6/20/96

    Copyright © 1996 Massachusetts Institute of Technology.
    $ID$