2 + 3
as
2 3 +
To evaluate an expression in postfix notation in a calculator, we can use a Stack. When a number is entered, it is pushed onto the stack. When an operator is entered, the last two elements of the stack are popped, the operation is performed between the two items, and the result is pushed back onto the stack. When the calculation is complete, we peek at the top of the stack to see the answer.
Draw the state of the stack at each step of the calculation using our ArrayStack implementation, and show the methods you would call, when given the following input
1, 2, 3, +, +, 7, 9, +, *
public boolean removeThird(E t)
for our ArrayList that will remove
the third instance of an element in the list when there are at least three of that element in the list.
If the remove is successful, return true, otherwise return false.
public int lastIndexOf(E t)
for our ArrayList that will
return the last found location of the element t
. If the element is not found in the
ArrayList, return -1.
public boolean funky(int[] nums)
that will return true if each
element in the array is either at least twice as much as the sum of the elements before it, or a multiple
of 7. For example [1, 2, 8, 23, 21, 120] would return true, while [3, 8, 25, 44, 200]
would return false because 44 is invalid.
For example, the push
method of the Stack can add
the element to the first Queue. The pop
method is more complicated, where you must remove
elements of the Queue to find the
most recently added element instead of the front element. This is where you can use the second
Queue to help you out so you don't lose all these elements... but I will leave the details of this to you.
Write a class QStack<E>
that will have two ArrayQueues as data members. Implement
all the methods of the Stack interface using calls to your two ArrayQueue data members.
Write a method isFinished()
that will return true when the current page is on the last
page of the book.
Write
a method read()
that will return the string "Call me Ishmael." and advance the current
page by one, unless the current page is the last page, where it will output "The End." and not advance the page.
This method
should make use of the isFinished()
method you wrote above.
protected
visibility modifier instead of
public
or private
?