|
Overview:
|
mkdir ~/cs170/hw10 cp ~cs170002/share/hw10/*.java ~/cs170/hw10 cd ~/cs170/hw10 |
|
|
Example:
String[] A = new String[ ... (some array size that you specify) ]; int top = -1; |
Result:
![]() |
The variable top contains the value −1 to indicate that there is no items inside the array A
(the first element in the array has index 0 and the value -1 is smaller than 0.)
|
Example: push("Hello");
|
|
Example: pop();
|
Use the provided file Stack.java to do Part 1 of the assignment.
|
|
|
I have included 2 test programs to help you debug the Stack.java program in a piecemeal fashion.
The correct output of TestStack1.java is:
======== correct output is: =========== 3 - 2 + 1 s.size() = 3 1 + 2 - 3 |
The correct output of TestStack2.java is:
javac TestStack2.java java TestStack2 ======== correct output is: =========== 3 s.size() = 1 3 + 2 s.size() = 3 3 + 2 - 1 s.size() = 5 1 - 2 + 3 |
|
>> javac RecPolish.java >> java RecPolish 1 2 + Next item = 1 Stack = 1 ---------------------------------------------- Next item = 2 Stack = 1 2 ---------------------------------------------- Next item = + Stack = 3.0 ---------------------------------------------- 3.0 |
Note:
|
And:
>> java hw9 2 3 4 + x Next item = 2 Stack = 2 ---------------------------------------------- Next item = 3 Stack = 2 3 ---------------------------------------------- Next item = 4 Stack = 2 3 4 ---------------------------------------------- Next item = + Stack = 2 7.0 ---------------------------------------------- Next item = x Stack = 14.0 ---------------------------------------------- 14.0 |
If you like, you can examine the RevPolish.java code and see how a reverse polish expression is evaluated using your Stack object (it will be very educational).
|
|
|
Use the file PalinSentence.java for part 2 of this homework.
1. Create a Stack with size equal to the half of the size of the input String array (in this example: 6/2 = 3) 2. Push the first half of the words in the String array onto the stack. You will have a stack with the following content: +--------+ | de | <---- top +--------+ | bc | +--------+ | ab | +--------+ 3. Now compare the second half of the sentence (highlighted in red): "ab bc de de bc ab" with the first half of the sentence that is stored in the stack - one word at a time: s.pop() ---> de matches 1st word in second half of sentence s.pop() ---> bc matches 2nd word in second half of sentence s.pop() ---> ab matches 3rd word in second half of sentence If you find one mismatch return false immediately If you match all words stored in the stack, the sentence is "palindromic" and you return true Note: make sure to skip the middle word in the matching process when the sentence has an odd number of words. Note: the number of words in the sentence is the length of the array (You should know how to get the length of an array) Note: the number of items in the stack is returned by the size() method in the Stack class. |
|
|
cd ~/cs170/hw10 /home/cs170002/turnin-hw Stack.java hw10 /home/cs170002/turnin-hw PalinSentence.java hw10a |