Question
import java.util.Scanner; public class PoD { public static void main( String [] args ) { Scanner in = new Scanner( System.in ); Stack computerTasks =
import java.util.Scanner;
public class PoD {
public static void main( String [] args ) { Scanner in = new Scanner( System.in );
Stack computerTasks = new Stack();
boolean quitProgram = false;
while (!quitProgram) { String task = in.nextLine(); if (task.equals("quit")) { quitProgram = true; //System.out.println("QUIT"); } else { if (task.equals("undo")) { String undoTask = computerTasks.pop(); //System.out.println("UNDO: "+undoTask); } else { computerTasks.push(task); //System.out.println("DO: "+task); } } } System.out.println(computerTasks);
in.close();
System.out.print("END OF OUTPUT"); } }
==================================================
import java.util.NoSuchElementException;
public class Stack {
private Node head;
class Node
{
public String data;
public Node next;
}
// Construct an empty stack:
// ================
/**
* Adds an element to the top of the stack
* @param s String to add
*/
public void push(String s) {
// YOUR CODE HERE
// ================
}
/**
* Removes the element from the top of the stack
* @return the removed String
*/
public String pop() {
// YOUR CODE HERE
// ================
}
public String toString()
{
Node position = head;
String output = "";
while (position != null)
{
output += position.data + " ";
position = position.next;
}
return output;
}
}
Input Input is taken care of for you (in PoD.java). It reads in the tasks to be performed by the word processor. The main program then makes a push call to the stack so that all the issued commands are stored in the stack in the order that they took place. At times, if the user wants to undo their last task, it will read in "undo". At that point, the main program will pop the last command from the stack. This carries on until the user quits the program. Once it reads in "quit" at which point it reads in "quit" and the program stops. Processing Your task is to implement the stack, including the addition of any required attributes/fields, a constructor, as well as push and pop methods. Output Output is taken care of for you (in PoD.java). Once all has been performed, the contents of the computerTasks stack is output (in reverse order) using the toString method already implemented in your stackStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started