CSCI 3310
Spring 2008
Small Programming Assignment 1

Assigned: January 11
Due: January 23, 11:59pm

This is just a warmup assignment to remind you how to use Java. If you have trouble using Java, please ask for help. You can use Windows or Unix. You will find the Unix compiler simpler and more reliable, and you will also find it much easier to created automated test scripts in Unix. (Professional software developers rely heavily on automated testing.) See here for how to use Java in Unix.


HTML and CGI

There are a variety of ways to make web pages that respond to requests. One is called the Common Gateway Interface, or CGI. The user fills in a form on a web page, and submits the form to a server. The server runs a program that receives information on the form and produces a new page for the browser to display.

This page does not explain how CGI works, except one aspect of that. Between the user input and the program that responds to the request, strings are converted into a form that avoids certain special characters. The following conversions are done.

  1. Each space is replaced by +.
  2. A special character (including, for example, +) is replaced by a three-character sequence %XX, where XX is the hexadecimal code for the character. For example, ':' is replaced by %3A.

The program that processes the request needs to undo those conversions. This assignment is to write a function that a CGI application can use for that purpose.


The assignment

Write a Java class Unmangle containing a static Java function called unmangle that takes a string as a parameter, and returns a new string that holds the string that you get by undoing the above conversions. For example, unmangle("a+b%3A") should return "a b:".


Getting started

You will want to be able to convert a string of two hexadecimal digits to an integer (a character code). Here is a function that does such a conversion. You can use this as a helper for unmangle. To convert an integer n to a character, use (char) n. For example, (char) 48 = '0', since 48 is the code for character '0'.

  //*********************************************************
  //                     hexToInt                           *
  //*********************************************************
  // digs is a string of two hexadecimal digits.            *
  // Return the integer that they represent.  For           *
  // example, hexToInt("2A") represents 2*16 + 10 = 42.     *
  //*********************************************************/

  private static int hexDigitToInt(char dig)
  {
    if(dig >= '0' && dig <= '9') return (int) dig - '0';
    if(dig >= 'A' && dig <= 'Z') return (int) dig - 'A' + 10;
    if(dig >= 'a' && dig <= 'z') return (int) dig - 'a' + 10;
    return 0;
  }

  private static int hexToInt(String digs)
  {
    return 16*hexDigitToInt(digs.charAt(0)) 
           + hexDigitToInt(digs.charAt(1));
  }

Submitting your work

Put your class in a file called Unmangle.java. Be sure to use exactly that name, not unmangle.java or UNMANGLE.JAVA. Call the class Unmangle, and make it a public class. Call your function unmangle, not something else. Make unmangle public and static. Be sure to include the hexToInt function as well, if you use it. If there is a main method, for testing, then make a copy of your program, remove main from the copy, and submit that. So there should be no main method in your submission.

To submit, log into a Unix machine in the lab. Put your file somewhere in your home directory. Change to the directory that contains your file. For example, if it is in directory 3310, then do command

  cd 3310
Then submit using command
  ~karl/3310/bin/submit S1 Unmangle.java
You should get a confirmation that the file was successfully submitted. (Be sure to include S1. It is the name of the assignment.)