Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; public class PoD { public static void main( String [] args ) { Scanner in = new Scanner( System.in ); Stack computerTasks =

image text in transcribed

image text in transcribed

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 stack

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

Explain the causes of indiscipline.

Answered: 1 week ago

Question

=+What is the nature of their impact?

Answered: 1 week ago

Question

=+Is it possible to operate union-free?

Answered: 1 week ago

Question

=+impact member states and MNEs?

Answered: 1 week ago