Question: In Java write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain

In Java write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to recognize and evaluate prefix expressions. I need help creating this program with the given algorthms I have from the book.

isPre()

size = length of expression strExp

lastChar = endPre(0, size 1)

if (lastChar >= 0 and lastChar == size-1 {

return true

}

else {

return false

}

evaluatePrefix(strExp)

ch = first character of expression strExp

Delete first character from strExp

if (ch is an identifier) {

return value of the identifier

}

else if (ch is an operator named op) {

operand1 = evaluatePrefix(strExp)

operand2 = evaluatePrefix(strExp)

return operand1 op operand2

}

endPre( in first:integer, in second:integer):integer

if (first < 0 || first > last )

{

return -1

}

ch = character at position first of strExp

if ( ch is an identifier)

{

//index of last character in simple prefix expression

return first;

}

else if(ch is an operator )

{

//find the end of the first prefix expression

firstEnd = endPre (first +1, last)

//If the end of the first expression was found

//If the end of the second prefix expression

if(firstEnd > -1)

{

return endPre(firstEnd +1, last)

}

else

{

return -1

}

else

{

return -1

}

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!