CS 3723 Programming Languages
Spring 2001 -- Fully Parenthesized Arithmetic Expressions
Due 2 March 2001

For this assignment, you are to modify a Java program so that it outputs fully parenthesized arithmetic expressions. The input is an arithmetic expression, with no parentheses, with some parentheses, or even with extra parentheses.

This assignment is not hard, because you will be making simple modifications to a program that takes the same input and produces fully parenthesized prefix notation, as in Lisp. You do not need to know much about java, nor do you need to fully understand the program you are modifying.

Here is the Java program you are to modify. It converts an arithmetic expression to fully parenthesized prefix notation (as in Lisp):

Carry out this assignment in the following steps:
  1. Copy the three Java source files into a separate directory (just using copy and paste from your browser).

  2. If you rename these files, you will have to change all the names inside the files also, since a Java source file must have the same name as the public class it defines. (The simplest way is to just not change the names.)

  3. Make (very simple) changes to the file Prefix.java so that it puts the operator in between the operands, rather than at the start of the operands. Leave everything else alone. (Note that in working with Java strings, the + operator is concatenation.)

  4. Compile the java source files with the three lines:
              pandora% javac Prefix.java
              pandora% javac PrefixScan.java
              pandora% javac PrefixTest.java
    
    (Actually, just the command javac PrefixTest.java will force the compiler to compile all three files.)

  5. Test the program with the command:
              pandora% java PrefixTest
    
    This invokes the Java Virtual Machine, and puts the class PrefixTest.class into execution. This class makes use of the other two classes.

  6. Here is some sample source, and the expected output. (Input characters are in boldface.)
              pandora% java PrefixTest 
              2+3+4+5#
              (((2 + 3) + 4) + 5) #
              pandora% java PrefixTest 
              (((2 + 3) + 4) + 5) #
              (((2 + 3) + 4) + 5) #
              pandora% java PrefixTest
              2^3^4^5#
              (2 ^ (3 ^ (4 ^ 5))) #
              pandora% java PrefixTest
              2+3*4^5*6+7#
              ((2 + ((3 * (4 ^ 5)) * 6)) + 7) #
              pandora% java PrefixTest
              ((3^2-4*1*2)^(1/2)-3)/(2*1)#
              (((((3 ^ 2) - ((4 * 1) * 2)) ^ (1 / 2)) - 3) / (2 * 1)) #
              pandora% 
    
  7. Turn in a partial listing of Prefix.java highlighting the changes you made (or just indicate the changes you made by hand), along with a listing of tests as above.


Revision date: 2001-02-26. (Please use ISO 8601, the International Standard.)