LispExpressionEvaluator.java - CSC220 Programming Project#2...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/ ******************************************************************************** **** * * CSC220 Programming Project#2 * * * * Specification: * * Taken from Project 7, Chapter 5, Page 178 * I have modified specification and requirements of this project * * Ref: (see chap. 10) * * In the language Lisp, each of the four basic arithmetic operators appears * before an arbitrary number of operands, which are separated by spaces. * The resulting expression is enclosed in parentheses. The operators behave * as follows: * * (+ a b c ...) returns the sum of all the operands, and (+ a) returns a. * * (- a b c ...) returns a - b - c - ..., and (- a) returns -a. * * (* a b c ...) returns the product of all the operands, and (* a) returns a. * * (/ a b c ...) returns a / b / c / ..., and (/ a) returns 1/a. * * Note: + * - / must have at least one operand * * You can form larger arithmetic expressions by combining these basic * expressions using a fully parenthesized prefix notation. * For example, the following is a valid Lisp expression: * * (+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)) (+ 1)) * * This expression is evaluated successively as follows: * * (+ (- 6) (* 2 3 4) (/ 3 1 -2) (+ 1)) * (+ -6 24 -1.5 1) * 17.5 * * Requirements: * * - Design and implement an algorithm that uses Java API stacks to evaluate a * valid Lisp expression composed of the four basic operators and integer values. * - Valid tokens in an expression are '(',')','+','-','*','/',and positive integers (>=0) * - Display result as floating point number with at 2 decimal places * - Negative number is not a valid "input" operand, e.g. (+ -2 3) * However, you may create a negative number using parentheses, e.g. (+ (-2)3) * - There may be any number of blank spaces, >= 0, in between tokens * Thus, the following expressions are valid: * (+ (-6)3) * (/(+20 30)) * * - Must use Java API Stack class in this project. * Ref: * - Must throw LispExpressionEvaluatorException to indicate errors * - Must not add new or modify existing data fields * - Must implement these methods : * * public LispExpressionEvaluator()
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
* public LispExpressionEvaluator(String inputExpression) * public void reset(String inputExpression) * public double evaluate() * private void evaluateCurrentOperation() * * - You may add new private methods * ******************************************************************************** *****/ package PJ2; import java.util.*; public class LispExpressionEvaluator { // Current input Lisp expression private String inputExpr; // Main expression stack & current operation stack
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern