In: Computer Science
Without running the program, what happens if you run
StackofStringsApp with
“to be or not to be that – is - - the question – whether tis nobler
- - -“? Show how you came up with the answer (show your work).
Write this on paper by hand.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package stackofstringsapp;
/******************************************************************************
*
* % more tobe.txt
* to be or not to - be -
*
* % java FixedCapacityStackOfStrings
* to be or not to - be - -
* to be not
*
* Remark: bare-bones implementation. Does not do repeated
* doubling or null out empty array entries to avoid
loitering.
*
******************************************************************************/
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
import csci232library.StackOfStrings;
import csci232library.ListStack;
import java.io.File;
import java.io.FileNotFoundException;
public class StackOfStringsApp {
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(System.in);
int max = 1000;
String item;
StackOfStrings stack = new StackOfStrings(max);
// ListStack stack = new ListStack();
// read the strings from a file
File file = new File(args[0]);
Scanner inpFile = new Scanner(file);
int i = 0;
while(inpFile.hasNext()){
item = inpFile.next();
if (!item.equals("-")) stack.push(item);
else if (stack.isEmpty()) System.out.println("BAD INPUT");
else System.out.print(stack.pop() + " ");
}
// print what's left on the stack
System.out.print("\nLeft on stack: ");
for (String s : stack) {
// for (Object s : stack) {
System.out.print(s + " ");
}
System.out.println();
}
}
Assuming StackOfStrings work like a normal stack (since you haven’t provided it, I can only guess). So in a nutshell, the program works like this:
1. open the file specified by commandline argument
2. looping through each String token (separated by white space/tab/newline characters)
3. if the token is not ‘-‘, pushing to the stack, otherwise if the token is ‘-‘, pops the top element from the stack and display it; if the stack is empty, display ‘Bad Input’. And after finishing reading the file, the system will display the remaining items on the stack.
So let’s simulate the running of this program if the file contained below String.
“to be or not to be that – is - - the question – whether tis nobler - - -“
The first token is “to” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”]
Next token is “be” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”]
Next token is “or” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”]
Next token is “not” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”]
Next token is “to” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”]
Next token is “be” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”]
Next token is “that” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”]
Next token is “–” (em-dash character)-> not equal to “-“ , so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”–“]
Next token is “is” -> not equal to “-“ , so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”–“,”is”]
Next token is “-” -> equal to “-“, so popping top element from the stack -> “is” will be printed
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”–“]
Next token is “-” -> equal to “-“, so popping top element from the stack -> “–” will be printed
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”]
Next token is “the” -> not equal to “-“,so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”]
Next token is “question” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”,”question”]
Next token is “–” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”,”question”,” –“]
Next token is “whether” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”,”question”,”–“,”whether”]
Next token is “tis” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”,”question”,”–“,”whether”,”tis”]
Next token is “nobler” -> not equal to “-“, so pushing to the stack
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”,”question”,”–“ ,”whether”,”tis”,”nobler”]
The next three tokens are “-”, so popping the next top three elements will print “nobler tis whether”
Stack contents (bottom to top): [“to”,”be”,”or”,”not”,”to”,”be”,”that”,”the”,”question” ,”–“]
After exiting the loop reading the file, the remaining elements will be printed from top to bottom
“– question the that be to not or be to”
So the actual output of the program will be
is – nobler tis whether
Left on stack: – question the that be to not or be to