Docsity

Lab 8: Format Math Expressions with Visitor Pattern in CS350/SE310 Software Design - Prof., Lab Reports of Computer Science

Toggle sidebar
bg1
CS350/SE310 Software Design/Software Architcture I
Lab 8: Format Mathematical Expressions using Visitor Pattern
Due December 2nd, 2008
Goal:
Practice Visitor Pattern
Requirements
Modify the Mathematical Interpreter in Java (Mij) to “pretty print” the expressions rather than
evaluate them. For example, here is a sample run of the program (user input shown in italics):
> int ( 2*sin(x), x,0,2 *Pi)
int(2 * sin(x), x, 0, 2 * Pi)
> 3*(y-(3)/sin( ( Pi ) ))+(4*z )
3 * (y – 3 / sin(Pi)) + 4 * z
Fulfill the following requirements in formatting the output.
1. A single space before and after each binary operator (e.g. +, -, *, /, ^)
2. No space to the right of an opening parenthesis
3. No space to the left of a closing parenthesis
4. No space between the function name and the opening parenthesis
5. No space before a comma
6. A single space after a comma
7. Reduce the number of parentheses as much as possible
Be careful with parentheses. Consider the following two expressions:
• (3 + 4) * 2
• 3 + (4 * 2)
A naïve approach may format both of these expressions as:
• 3 + 4 * 2
Instructions
Fill in the code for the Formatter class to use a visitor pattern and correctly output the formatted
expressions.

Partial preview of the text

Download Lab 8: Format Math Expressions with Visitor Pattern in CS350/SE310 Software Design - Prof. and more Lab Reports Computer Science in PDF only on Docsity!

CS350/SE310 Software Design/Software Architcture I

Lab 8: Format Mathematical Expressions using Visitor Pattern

Due December 2nd, 2008

Goal:

Practice Visitor Pattern

Requirements

Modify the Mathematical Interpreter in Java (Mij) to “pretty print” the expressions rather than evaluate them. For example, here is a sample run of the program (user input shown in italics):

int ( 2*sin(x), x,0,2 *Pi) int(2 * sin(x), x, 0, 2 * Pi) 3(y-(3)/sin( ( Pi ) ))+(4z ) 3 * (y – 3 / sin(Pi)) + 4 * z

Fulfill the following requirements in formatting the output.

  1. A single space before and after each binary operator (e.g. +, -, *, /, ^)
  2. No space to the right of an opening parenthesis
  3. No space to the left of a closing parenthesis
  4. No space between the function name and the opening parenthesis
  5. No space before a comma
  6. A single space after a comma
  7. Reduce the number of parentheses as much as possible

Be careful with parentheses. Consider the following two expressions:

  • (3 + 4) * 2
  • 3 + (4 * 2) A naïve approach may format both of these expressions as:
  • 3 + 4 * 2

Instructions

Fill in the code for the Formatter class to use a visitor pattern and correctly output the formatted expressions.