CS239 Advanced
Programming
|
 |
Software Requirements Specification
Programming Assignment 5
Expression Evaluator
Final Program Due by April 9 (Wednesday).
Hardcopy due on April 10 at the beginning of class.
Introduction
Purpose: This application will provide a tool to
evaluate an expression in the Latte language. The
application must read the expression, decide if it can be
evaluated according to the syntax of the language and if so produce a result. You must use a recursive
solution to this problem.
Deadlines
- Submit: Wednesday, April 9 10pm.
- Hardcopy submitted April 10, at the beginning of class.
- You will not need to keep a timesheet for this assignment.
Background
Programming languages like natural languages have a grammar associated
with them that can describe the language rules. For example, in
Java, one can define a declaration statement as a primitive type
name followed by a variable or a class name followed by a variable. A
variable can be a single letter or a single letter followed by one or
more letters, digits, or special symbols. Special symbols include
- and _ only, etc. Some grammars are by their nature recursive.
Think about English. A noun phrase consists of a noun or an
adjective followed by a noun phrase. big, red fox for example is
an adjective, big, followed by a noun phrase which is an adjective red
followed by a noun phrase which is fox. The base case is the
single noun and the refinement involves stripping off each adjective
until we reach the single noun.
In this assignment, you will take a grammar and parse it to discover if
the grammar is legal in the language and then evaluate it to achieve
its result.
Existing program components
Your application will be written from scratch. You may use
tools found in the standard Java libraries. You must have a
Driver and an ExpressionEvaluator class. You may have others.
Input
Your application will read a series of expressions entered at the
keyboard. The application will prompt the user for this input.
Execution ends when a return statement (no data is entered) is
issued from the keyboard.
Output
- The application will open with the single line "Welcome to the Latte Evaluation Calculator".
- Output one blank line.
- Prompt with the statement: "Enter an expression. Press
<enter> to quit: " The cursor should advance to the next
line for the input of the expression.
- Output a blank line before displaying the result.
- If the expression can be evaluated, output the expression
followed by " = " followed by the result. All results should be expressed as integers
If the expression cannot be evaluated, output the expression
followed by " = Bad Result".
- Follow the output with a blank line and repeat from step 3 until the user presses enter (no input).
- Output the final line, "Thank you for using the Latte Evaluation Calculator"
Valid Expressions
This picture shows the grammar of Latte expressions.
Examples of valid expressions are
3 A 2 M 7
( 3 S 2 ) D 1
7 S 2 A 5 M ( 3 R 2 )
7S2A5M(3R2)
Whitespace should be ignored. A valid expression can only contain
the operators A, S, M, D, and R, the digits 0 - 9, and the parentheses
(). Case is important. a is not the same as A.
A |
Add (like +) |
S |
Subtract (like -) |
M |
Multiply (like *) |
D |
Divide (like /) |
R |
Remainder (like %) |
Error Handling
If an invalid expression is entered, you will output the message BAD RESULT in place of the actual result.
See output specs above.
Hints and Suggestions
- Work with the grammar until you are comfortable with it.
- Understand what constitutes valid vs invalid expressions.
- Then carefully consider which methods you need to parse the
expression and to evaluate it. You cannot evaluate the expression
until you first determine if it is valid.
- Be sure that you understand your base case. It will be
easier to refine the hard case if you know where you should be heading.
- Code the methods to determine validity first. It will be
easier to recognize good or bad expressions than it will be to evaluate
them.
- Test the validation thoroughly, then code the actual evaluation methods.
- Design your solution, then test your design BEFORE you code it.
- If you get stuck in the coding, use debug statements to see what
is happening. Is your base case resolving properly? Is your
refinement really refining?
- Don't wait until the last minute to code.
Timesheet
You may choose to keep a timesheet for yourself, but it is not a requirement of this assignment.
Honor Code
This work must conform to the JMU Honor Code and the
specific
requirements
of this class. You may have an opportunity to work with your team in
class on this assignment but only in the confines of the classroom.
Any other help must come from the professor or the TAs for
this
class. See the collaboration
policy for further information.
Grading
- You will receive 80% of the grade by successfully
submitting the program (which includes passing all submit tests) on
time. You must adhere to the design requirements listed above.
- Programs submitted with submit errors will be docked 20 points (out of the 80 points).
- The remainder will be based on your conformance to the
Style and other requirements of the assignment. Review the Style Guide
and the program checklist before your final submission.
- All grades will be based on 100 points.
- You may submit any number of times. The only one I will
count is the one that corresponds to the hardcopy report that you turn
in.
- Successfully submitted programs that are late will be
graded, then a 10 point penalty assessed for each day late.
Saturday will not be considered a late day. Late
penalties
apply to programs submitted on time but not turned in on time also.
It
is expected that the hardcopy will be turned in on the deadline or
within one class period of a successful submit run.
- No programs will be accepted after one calendar week past
the due date of the assignment.