In-Class Lab

Reading another person's code is an important element of learning to be a good programmer. (The other key element is writing lots of code!) The goal of this in-class exercise is to practice reading and critiquing another person's code.

Part 1 - Retrieve the code

    1. Download the client API for accessing Google Translate here: http://code.google.com/p/google-api-translate-java/
      • If you click on the "Source" tab, it will show how you can easily use svn to grab all of the source code: svn checkout http://google-api-translate-java.googlecode.com/svn/trunk/ google-api-translate-java-read-only
    2. Compile the code
      • Import into Eclipse/another IDE if desired.
      • Make sure you are using JDK 1.6.
      • Make sure the junit jar file is in your classpath.
    3. Try running some of the tests and the TranslatorFrame.

Part 2 - Review the code

Read through the code until you feel you understand it.

Part 2 Questions:

    1. Describe any difficulties you had downloading, compiling, and running the code.
    2. Do you understand the code?
    3. Has the developer tested the code appropriately?
    4. Is the code documented appropriately?
    5. Do you think static methods and variables are used appropriately?
    6. Describe an element of the program that you feel is well designed.
    7. Why do you think the developers replaced the translate method with the execute methods in the most recent version of the code? Do you think this was a good idea?

Part 3 - Use and modify/extend the code

Part 3.1

Implement a simple Driver program that will take as input a word, a from language, and a to language and will translate the word from the from language to the to language using the API. Print the result at the command line. Your program will be run as follows:

java Driver -p word -f fromlang -t tolang

Sample output: finger in English translates to prst in Croatian.

Assume the classpath is set appropriately.

Part 3.2

You are now going to modify or extend the code to support a method that will accept as input a word, a from language, and an array of to languages and will return a HashMap of the results where the key is the language and the value is the translated string. The signature of your method will look as follows:

public static HashMap<String, String> execute(final String text, final Language from, final Language[] to) throws Exception {

You are free to use any approach you like to providing this functionality, including modifying existing methods, modifying existing classes, or writing new classes/methods. You should, however, be able to justify your new design.

Part 3 Questions:

    1. Describe any challenges you faced in using the code for part 3.1.
    2. Describe the design of your solution for part 3.2. Did you write new methods/classes or modify existing ones, for example?
    3. How easy was it to modify/extend the existing design?

All required questions can be found in the following document: https://docs.google.com/document/pub?id=1VTUXGWdJL_8xleUhzeLoJ-aG2CdXSuBduYia93_CqAs