Home
  Contact
  Short Vita
  Research
  Teaching
  LSDIS Lab
  Links

Additional requirements for graduate students

In addition to implementing all of the features of TinyJava as described in the class handout, you will have to implement handling of one-dimensional arrays, for loops, and increment and decrement operators.

  1. You must introduce the necessary modifications to the existing TinyJava grammar in order to incorporate declarations of array variables (you will have to add the for, null and new keywords, as well as the special symbols ++, --, [ and ]). Arrays may use only int or float as the base type. You do not have to handle arrays of boolean, String or other class types.
  2. You will have to introduce changes to the fragment of the TinyJava grammar dealing with expressions to allow increment and decrement expressions, the access of array elements and represent the null expression. Also, you will have to modify the field and local variable initialization of array types. The only possible initalization should be to null. Furthermore, it should be possible to compare an expression (if equal or not equal) to null. Change the Statement rules to add the handling of the for statement.

Remember that arrays in Java are passed by reference. It should be possible to supply an array actual argument in place of a suitable formal parameter.

As an illustration, please examine the the following simpe sorting program:

/*
 * a sample sorting program
 */
public class Sort
{
    static void print( int[] b, int sz )
    {
        int i = 0;

        SimpleIO.printString( "[ " );
        for( i = 0; i < sz; i++ ) {
            SimpleIO.printInt( b[i] );
            if( i < sz - 1 )
                SimpleIO.printString( ", " );
        }
        SimpleIO.printString( " ]\n" );
        return;
    }

    static void sort( int[] c, int sz )
    {
        int i = 0;
        int j = 0;
        int temp = 0;;

        while ( i < sz - 1 ) {
            j = i + 1;
            while ( j < sz ) {
                if( c[i] > c[j] ) {
                    temp = c[i];
                    c[i] = c[j];
                    c[j] = temp;
                }
                j = j + 1;
            }
            i = i + 1;
        }
        return;
    }

    public static void main( String[] args )
    {
        int[] a = null;
        int   i = 0;

        // create the array
        a = new int[10];

        // initialize the values
        a[0] = 10;
        a[1] = 1;
        a[2] = 6;
        a[3] = 2;
        a[4] = 7;
        a[5] = 3;
        a[6] = 8;
        a[7] = 4;
        a[8] = 9;
        a[9] = 5;

        SimpleIO.printString( "Elements to sort:\n" );
        print( a, 10 );

        // sort the array
        sort( a, 10 );

        SimpleIO.printString( "Sorted elements:\n");
        print( a, 10 );
        return;         // return from method (last stmt)
    }
}