Question

In: Computer Science

Without running the program, what happens if you run StackofStringsApp with “to be or not to...

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();
}
}

Solutions

Expert Solution

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


Related Solutions

Suppose you run the central bank in an open economy. What happens to the following variables...
Suppose you run the central bank in an open economy. What happens to the following variables of interest in response to the below events (analyze each event separately)? I) The president cuts government spending to reduce the budget deficit II) The president restricts the import of Chinese goods Use the standard open economy IS-LM model (not the Fleming-Mundell model). Also, assume direct effects of shifts are larger than indirect effects. a) IS – Direct Effect (increase / decrease / indeterminate...
In a closed economy, what happens in the short run as a result of an increase...
In a closed economy, what happens in the short run as a result of an increase in taxes? both investment and output increase. money demand and output increase. the change in the interest rate increases money demand and the change in output decreases money demand. investment decreases and output increases. the change in the interest rate decreases money demand and the change in output increases money demand. Which of the following would decrease real wages in the medium run? expansionary...
Explain what happens to the output gap, unemployment, and inflation in the short run if there...
Explain what happens to the output gap, unemployment, and inflation in the short run if there is a permanent income tax cut implemented by the federal government. You do not need to draw any graphs, but thinking about the short run model will help. Be sure to explain which part of IS/MP/PC is affected and why. You may start with an economy at full employment and inflation at the central bank target. Also, discuss what the central bank is likely...
Explain what happens to the output gap, unemployment, and inflation in the short run if there...
Explain what happens to the output gap, unemployment, and inflation in the short run if there is a permanent income tax cut implemented by the federal government. You do not need to draw any graphs, but thinking about the short run model will help. Be sure to explain which part of IS/MP/PC is affected and why. You may start with an economy at full employment and inflation at the central bank target. Also, discuss what the central bank is likely...
What happens in the long run for the monopolistic competition firm and for the oligopoly firm?...
What happens in the long run for the monopolistic competition firm and for the oligopoly firm? Which of the two are better for the consumer and why?
in paython You are going to make an advanced ATM program When you run the program,...
in paython You are going to make an advanced ATM program When you run the program, it will ask you if you want to: Either log in or make a new user Exit Once I have logged in, I will have 3 balances, credit, checking, and savings. From here it will ask which account to use. Once the account is picked they can deposit, withdraw, check balance, or log out. Each time the user performs one of these actions, it...
1. Using the AS-AD model diagram, illustrate what happens to the LONG-RUN and SHORT-RUN equilibrium level...
1. Using the AS-AD model diagram, illustrate what happens to the LONG-RUN and SHORT-RUN equilibrium level of aggregate output and inflation, when the economy is hit by a negative (adverse) demand shock and there is NO POLICY response. Suppose the economy is at a long-run equilibrium before it is hit by the negative demand shock. Make sure you properly label all the axes and curves. Will the negative demand shock more likely lead to an expansion or recession in the...
Using the Phillips curve diagram and in words, illustrate what happens to the short-run/long-run Phillips curve...
Using the Phillips curve diagram and in words, illustrate what happens to the short-run/long-run Phillips curve (inflation and unemployment) when the economy faces a “tight” labor market (actual unemployment rate is below the natural rate). Make sure you properly label all the axes and curves. Hint: Differentiate between short-run and long-run and think how this affects the inflation expectations.
1. According to the IS–LM model, what happens in the short run to the interest rate,...
1. According to the IS–LM model, what happens in the short run to the interest rate, income, consumption, and investment under the following circumstances? Be sure your answer includes an appropriate graph. a. The central bank decreases the money supply. Effect of the shock: Interest rate: Income: Consumption: Investment: b. The government increases government purchases and taxes. The increase in taxes is twice as big as the increase in government purchases. Effect of the shock: Interest rate: Income: Consumption: Investment:
Draw what happens in the economy in the short run when interest rates fall.
Draw what happens in the economy in the short run when interest rates fall. a. What components of GDP change—how and why? b. How does aggregate demand and/or supply change? What type of gap is the economy in? c. What is the impact on price, real GDP and unemployment?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT