I need to remove the declaredstack from the code and replace it with the system
ID: 3615002 • Letter: I
Question
I need to remove the declaredstack from the code and replace it with the system stack throughuse of recursion. There should be no stack used directly by thecode, only indirectly through recursion. we do not need to changethe code in any other way-- simply take out the stack and replaceit with recursive calls to the method (making sure that they willstop at some point) that perform the task.
thanks,
/**
* This class takes a string as input and reverses the orderof the
* sentences using stacks and queues
*/
import java.util.Scanner;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ReverseSentences
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Declare and allocate scanner
Stack<Queue<String>> sq =new Stack(); //Declares and allocates a stack ofqueues
Queue<String> q = newLinkedList(); //Declares and allocates a queueof strings using a linked list
System.out.print("Enter text to bereversed: "); //Prompts user toenter input
Scanner in = newScanner(input.nextLine()); //Reads text from console
while (in.hasNext()) { //loop through all words of text
String nextToken = in.next(); //loads next word intostring variable
q.offer(nextToken); //enqueues word onto queue
if (nextToken.contains(".") ||nextToken.contains("?") || nextToken.contains("!")) { //checks every word for ".", "?", or "!"
sq.push(q); // push queue onto stack
q = new LinkedList(); //allocatesa new queue
}
}
//print thereversed text
System.out.print("Reversedtext: ");
while (!sq.isEmpty()) { //if stack is notempty, pop queue off stack
q = sq.pop();
while (q.peek()!=null) { //while queue q is notempty, dequeue each entry and print
System.out.print(q.poll() + " ");
}
}
}
}
I need to remove the declaredstack from the code and replace it with the system stack throughuse of recursion. There should be no stack used directly by thecode, only indirectly through recursion. we do not need to changethe code in any other way-- simply take out the stack and replaceit with recursive calls to the method (making sure that they willstop at some point) that perform the task.
thanks,
/**
* This class takes a string as input and reverses the orderof the
* sentences using stacks and queues
*/
import java.util.Scanner;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ReverseSentences
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Declare and allocate scanner
Stack<Queue<String>> sq =new Stack(); //Declares and allocates a stack ofqueues
Queue<String> q = newLinkedList(); //Declares and allocates a queueof strings using a linked list
System.out.print("Enter text to bereversed: "); //Prompts user toenter input
Scanner in = newScanner(input.nextLine()); //Reads text from console
while (in.hasNext()) { //loop through all words of text
String nextToken = in.next(); //loads next word intostring variable
q.offer(nextToken); //enqueues word onto queue
if (nextToken.contains(".") ||nextToken.contains("?") || nextToken.contains("!")) { //checks every word for ".", "?", or "!"
sq.push(q); // push queue onto stack
q = new LinkedList(); //allocatesa new queue
}
}
//print thereversed text
System.out.print("Reversedtext: ");
while (!sq.isEmpty()) { //if stack is notempty, pop queue off stack
q = sq.pop();
while (q.peek()!=null) { //while queue q is notempty, dequeue each entry and print
System.out.print(q.poll() + " ");
}
}
}
}
Explanation / Answer
please rate - thanks better import java.util.Scanner; public class ReverseSentences { public static void main(String[] args) { Scanner input = newScanner(System.in); //Declare and allocate scanner String []a=new String[10]; int i=0,j; System.out.print("Enter textto be reversed:"); //Prompts user to enterinput Scanner in = newScanner(input.nextLine()); //Reads text from console a[i]=""; while (in.hasNext()){ //loop through all words of text String nextToken =in.next(); a[i]+=nextToken; a[i]+=""; //loads next word into stringvariable and adds a blank to them if(nextToken.contains(".") || nextToken.contains("?") ||nextToken.contains("!")) { //checks every wordfor ".", "?", or"!" a[++i]=""; //go to and initialize next word } } //print the reversed text System.out.print("Reversedtext: "); reverse(a,i-1,i-1); for(j=0;jRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.