Question: I need assistance with the following Java code In the language Lisp, each of the four basic arithmetic operators appears before an arbitrary number of

I need assistance with the following Java code

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 (+) returns 0.

(- a b c ...) returns a - b - c - ..., and (- a) returns -a. The minus operators must have at least one operand.

(* a b c ...) returns the product of all the operands, and (*) returns 1.

(/ a b c ...) returns a / b / c ..., and (/ a) returns 1 / a. The divide operator 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) (*) (- 2 3 1)))

This expression evaluated successively as follows:

(+ (- 6) (* 2 3 4) (/ 3 1 -2)) (+ -6 24 -1.5) 16.5

Design and implement an algorithm that uses a stack to evaluate a legal Lisp expression composed of the four basic operators and integer values. Write a program that reads such expressions and demonstrates your algorithm.

Templates of source codes are given in LispToken.javaI need assistance with the following Java code In the language Lisp, and LispExpressionEvaluator.javaeach of the four basic arithmetic operators appears before an arbitrary number, where you need to implement all methods which in the body of method has a comment that says "needs to be implemented".

import java.util.Scanner;

/**

This class evaluates a simple arithmetic Lisp expression.

@author

*/

public class LispExpressionEvaluator

{

/** Evaluates a Lisp expression.

The algorithm:

Scan the tokens in the string.

If you see "(", push the next operator onto the stack.

If you see an operand, push it onto the stack.

If you see ")",

Pop operands and push them onto a second stack

until you find an operator.

Apply the operator to the operands on the second stack.

Push the result on the stack.

If you run out of tokens, the value on the top of the stack is

the value of the expression.

@param lispExp A string that is a valid lisp expression.

@return A double that is the value of the expression. */

public static double evaluate(String lispExp)

{

StackInterface expressionStack = new LinkedStack();

StackInterface secondStack = new LinkedStack();

boolean nextIsOperator = false;

Scanner lispExpScanner = new Scanner(lispExp);

// needs to be implemented

return 0.0;

} // end evaluate

public static void main (String args[])

{

Double result;

String test1 = "(+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 1)))";

result = evaluate(test1);

System.out.println("Expression " + test1 + " evaluates to " + result);

String test2 = "(+ (- 632) (* 21 3 4) (/ (+ 32) (*) (- 21 3 1)))";

result = evaluate(test2);

System.out.println("Expression " + test2 + " evaluates to " + result);

String test3 = "(+ (/ 2) (* 2) (/ (+ 1) (+) (- 2 1 )))";

result = evaluate(test3);

System.out.println("Expression " + test3 + " evaluates to " + result);

} // end main

} // end LispExpressionEvaluator

/*

Expression (+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 1))) evaluates to 16.5

Expression (+ (- 632) (* 21 3 4) (/ (+ 32) (*) (- 21 3 1))) evaluates to -378.11764705882354

Expression (+ (/ 2) (* 2) (/ (+ 1) (+) (- 2 1 ))) evaluates to Infinity

*/

/**

This class represents either an operand or an operator

for an arithmetic expression in Lisp.

@author

*/

public class LispToken

{

private Character operator;

private Double operand;

private boolean isOperator;

/** Constructors for objects of class LispToken. */

public LispToken(Character anOperator)

{

operator = anOperator;

isOperator = true;

operand = 0.0;

} // end constructor

public LispToken(Double value)

{

operand = value;

isOperator = false;

operator = ' ';

} // end constructor

/** Applies this operator to two given operand values.

@param value1 The value of the first operand.

@param value2 The value of the second operand.

@return The real result of the operation. */

public Double applyOperator(Double value1, Double value2)

{

// needs to be implemented

return 0.0;

} // end applyOperator

/** Gets the identity value of this operator.

For example, x + 0 = x, so 0 is the identity for +

and will be the value associated with the expression (+).

@return The identity value of the operator. */

public Double getIdentity()

{

// needs to be implemented

return 0.0;

} // endGetIdentity

/** Detects whether this operator returns a value when it has no operands.

@return True if the operator returns a value when it has no operands,

or false if not. */

public boolean takesZeroOperands()

{

// needs to be implemented

return false;

} // end takesZeroOperands

/** Gets the value of this operand.

@return The real value of the operand. */

public Double getValue()

{

return operand;

} // end getValue

/** Returns true if the object is an operator.

@return True is this object is an operator. */

public boolean isOperator()

{

return isOperator;

} // end isOperator

// Returns String version of either operator or operand

public String toString()

{

// needs to be implemented

return "";

} // end toString

} // end LispToken

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!