Download (RTF format) that prints in three pages.

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CScD-300 [Fall 2008] Assignment 4:
Arithmetic Expression Translation:
Infix Notation Calculator
100 points — Due Friday, 21 November 2008
Full Documentation Required*
* NOTE: if you properly document this assignment using javadoc, you will not have to fully document
your last assignment (minimal documentation will still be required).
In this program, you will be implementing (based either on the PowerPoint slides from when Carrano was
the text, on the PowerPoint slides and program examples from Koffman, or on the Wikipedia extract, as
discussed in lecture) a program to translate a parenthesized infix expression into a postfix expression. It
will be an expression containing only numbers for the non-operator entries. In addition, you will also
evaluate the expression and report the value.
You will also be making a minor amplification to Sahni's class in ArrayStack.java.
Asg4Driver.java — File available on the course website
You are being given the main to test your program. It accepts Strings from System.in until an empty
string is entered. Each string is translated into a postfix expression through the class "InToPost", which
has a static method "translate" that is expecting a String. It returns a character string that is the postfix
expression, or a null reference on failure. The postfix expression string is used to do a stack-oriented
evaluation of the expression, generating a single value. You will report both the postfix expression and
the value returned on its evaluation. Input will end with the entry of an empty string or an end-of-file
condition (that is, Scanner.hasNext() is false).
ArrayStack.java — File available on the course website
This file uses one of the utility classes that Sahni developed for his textbook. Rather than bringing in the
entire "utilities" package for that one class, you will be writing your own public static Object []
changeLength1D(Object [] a, int newLength) as appropriate for its use in ArrayStack.java
(i.e., simply changing the size of a bare array of Objects). The class is actually included in the
ArrayStack.java file, presently in stub form. You are free either to develop it yourself (actually an
exercise appropriate to Principles of Programming II) or to bring across code from Sahni's version and get
it working — noting that it must work without referencing Sahni's "utilities" package. Sahni’s
ArrayStack is a stack of Objects, so you’ll be doing a lot of type casting.
InToPost.java — You develop this entirely on your own
This class contains "static public String translate(String infix)" — which converts the
String in "infix " into a white-space delimited String with the postfix expression that is return by the
method. This method must correctly handle not only the left-associative operators +, -, *, and /, but also
the right-associative operator ^ (which has the highest precedence). You can generate this either by
translating the pseudocode in the earlier (Carrano) PowerPoint slide set, or by amplifying the later code
that accompanied the Koffman text (but does not include the ^ operator). If translate finds an error in
the String infix, it reports the error and returns a null string. Note that the stack used in this part of
the exercise will have Objects from the either the class Character or the class String.
EvaluatePost.java — You develop this entirely on your own
This class contains “static public double evaluate(String postfix)” — which receives a
Printed 2017/ May/3 at 06:55
Page 1
Printed
Page 2
white-space delimited postfix expression of numbers (readable as doubles) and operators and returns the
value. If it encounters an error it reports the error. If there are insufficient operands for the operators,
report that error and return the constant defined in the Double wrapper class “Double.NaN”. If there are
insufficient operators for the operands, report what is on the stack and return the last value popped. Note
that the stack used in this part of the exercise will have Objects from the class Double.
To Turn In: This assignment must be submitted in working order by midnight Friday, 21 Nov. Submit a
jar file with all Java source files — Asg4Driver.java [unmodified], ArrayStack.java [completed],
InToPost.java, and EvaluatePost.java. Use the standard naming convention for your JAR file.
Asg4Driver.java
import java.util.Scanner;
/**
* Program to exercise the classes InToPost and EvaluatePost
* @author Timothy Rolfe
*/
public class Asg4Driver
{
public static void main ( String[] args )
{
Scanner console = new Scanner ( System.in );
String textInput,
postfix;
double result;
System.out.print (
"Enter white-space-delimited arithmetic expressions. " +
"Empty line to terminate.\n\n" +
"First expression:
");
if ( args.length > 0 )
{ textInput = args[0]; System.out.println(textInput); }
else
textInput = console.nextLine();
while ( textInput.length() > 0 )
{
postfix = InToPost.translate( textInput );
if ( postfix == null )
System.out.println("Problem converting infix string " +
textInput);
else
{
System.out.println("InToPost.translate returns \"" +
postfix + "\"");
result = EvaluatePost.evaluate(postfix);
System.out.println("EvaluatePost.evaluate returns " + result);
}
System.out.print ("\nNext expression:
textInput = console.nextLine();
}
}
}
Printed 2017/ May/3 at 06:55
");