Java Reference
In-Depth Information
6.
Show the contents of the stack as you trace the algorithm checkBalance , given in Segment 5.8, for each of the fol-
lowing expressions:
a. a { b [ c * ( d + e )] - f }
b. { a ( b * c ) / [ d + e ] / f ) - g }
c. a { b [ c - d ] e ] ) f
7.
Using the algorithm convertToPostfix , given in Segment 5.16, convert each of the following infix expressions
to postfix expressions:
a. a * b / ( c - d )
b. ( a - b * c ) / ( d * e * f + g )
c. a / b * ( c + ( d - e ))
d. ( a ^ b * c - d ) ^ e + f ^ g ^ h
8.
Using the algorithm evaluatePostfix , given in Segment 5.18, evaluate each of the following postfix expres-
sions. Assume that a = 2, b = 3, c = 4, d = 5, and e = 6.
a. a b + c * d -
b. a b * c a - / d e * +
c. a c - b ^ d +
9.
What infix expressions are represented by the postfix expressions given in the previous exercise?
10.
Show the contents of the two stacks as you trace the algorithm evaluateInfix , given in Segment 5.21, to evalu-
ate each of the following infix expressions. Assume that a = 2, b = 3, c = 4, d = 5, e = 6, and f = 7.
a. ( a + b ) / ( c - d ) - 5
b. ( d * f + 1 )* e / ( a ^ b - b * c + 1) - 72
c. ( a ^ c - f ) ^ a - a ^ b ^ a
11.
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read
it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a pal-
indrome. So is A man, a plan, a canal: Panama . Describe how you could use a stack to test whether a string is a
palindrome.
12.
Suppose that you read a binary string—that is, a string of 0s and 1s—one character at a time. Describe how you
could use a stack but no arithmetic to see whether the number of 0s is equal to the number of 1s. When these
counts are not equal, state how you could tell which character—0 or 1—occurs most frequently and by how much
its count exceeds the other's.
13.
Write Java code that displays all the objects in a stack in the order in which they were pushed onto it. After all the
objects are displayed, the stack should have the same contents as when you started.
14.
Using the class java.util.Stack , define a class OurStack that implements the interface StackInterface , as
given in Listing 5-1.
15.
Use the class OurStack from the previous exercise in a program that demonstrates the class BalanceChecker , as
given in Listing 5-2.
 
Search WWH ::




Custom Search