Write a Java implementation for the following problem. You can make use of which
ID: 3791979 • Letter: W
Question
Write a Java implementation for the following problem. You can make use of whichever of the ADTs Stack, Queue, and Deque that you need. Assume that these ADTs are already implemented, compiled and available in files Stack.class, Queue.class, Deque.class. Input is read as a sequence of strings from the terminal (System.in). The program continues until the special string * is found on the input. Input data is stored until one of the special strings +, -, 0 is encountered. These special strings cause the stored data to be printed forwards and removed, printed backwards and removed, or removed without printing. For example, if the input is: q w e r + z x c v 0 t y u i p - a s d 0 q w e * the output would be: q w e r p i u y tExplanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<String> tokens = new ArrayList<String>();
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
String input = "";
//Continue until "*" is entered
while(!input.equals("*")){
System.out.println("Enter a input String");
// get their input as a String
input = scanner.next();
//If + is enter Print all Strings in ArrayList from Beginning and then remove each one
if(input.equals("+")){
for (int i = 0; i < tokens.size(); i++) {
System.out.print(tokens.get(i));
}
tokens.clear();
System.out.println();
}
//If - is enter Print all Strings in ArrayList from last to first and then remove each one
else if(input.equals("-")){
for (int i = tokens.size()-1; i >=0; i--) {
System.out.print(tokens.get(i));
}
tokens.clear();
System.out.println();
}
//If 0 is enter remove each one without printing
else if(input.equals("0")){
tokens.clear();
}
else{
tokens.add(input);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.