COP 4331 (5862/99S)
Assignment 1:  Expr application

Update:  Note on JDK 1.2 on PCs

The Unix command expr treats its command line argument as an expression to be evaluated and prints the resulting value.  The Unix man page includes the following description.

The expr utility will evaluate the expression and write the result to standard output.  Arguments are taken as an expression.  Terms of the expression must be separated by blanks.  Characters special to the shell must be escaped
The Unix command supports a variety of operators with several precedence levels.  This application will support only the operators +, -, *, /, and parentheses for grouping.  Expressions that do not contain parentheses will be evaluated strictly from left to right.  Operands must be integer constants and may optionally have a minus sign.

Note that when expressions with the above syntax are typed on the command line, *, (, and ), must be typed with escapes as \*, \(, and \), but the program sees only the single operand character.  If you are using a development environment like Visual J++, you do not use the escapes since the arguments are passed directly to the program and are not seen by the command interpreter.

In addition, if the program is run with no parameters, it is to print the message shown in the example below, but with your name rather than mine.

Here are some simple examples.  The '$' at the beginning of some lines is the prompt symbol.

$ expr 1 + 2
3
$ expr 1 + 2 \* 3
9
$ expr 1 + \( 2 \* 3 \)
7
$ expr
Programmed by Roy Levow
The equivalent of the first command for your program would look like
$java Expr 1 + 2
3
Observe that the Java interpreter takes the values following the class name and places them in an array of strings that is passed as the argument value for main.  You just use those values, you do not have to read anything within your program.

The assignment

Code a Java application program using a class named Expr to operate on expressions as described above.
 

Design hints

Observe that This alternation of items simplifies the design since you know what kind of item must come next.

Use a recursive function to do the actual evaluation of the expression.  When you come to a left paren, call the function recursively and return the value of the subexpression when you come to a right paren or the end of the expression.  The main function will print the returned value.  Note that there is a slight technical problem here because a program designed this way will operate correctly even if some right parens at the end are omitted.
 

Error handling

The  program must run correctly on correct data.  Behavior of the program on erroneous data is not defined and no special error handling is required.
 

Note on JDK 1.2 on PCs

JDK 1.2 on PCs interprets '*' on the command line as the wild-card for all files and replaces it with a list of all file names in the arguments array passed to main().  This will prevent you from properly performing multiplication as instructed.  As a "work-around", provide the alternative multiplication symbol 'x'.  That is, if the input symbol is either '*' or 'x', perform multiplication.  Other alternatives are to use a double backslash, as in \\*, or to surround the asterisk with quotes, as in "*".  This will allow testing on your PC but still allow me to use the standard tests on the Unix host.

What to submit

  1. A printed copy of the Expr.java source file for your program
  2. Printed output showing thorough testing of the program from the command line so that both the command and the corresponding output are shown
  3. Submit the Expr.java file with hwroy
  4. Place the Expr.class file in your directory ~/public_html/cop4331/expr  Make sure the permissions on the directories and the file are correctly set as described on the course home page.


Last Update:  11 Feb1999 by  R.Levow