Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stacks and Queues in java.... ** you will need to create 2 classes using 2 Inter

ID: 674559 • Letter: S

Question

Stacks and Queues in java....

** you will need to create 2 classes using 2 Interfaces, the 2 Interfaces are: Queue and Stack.

1) Create a class named FirstQueue that implements Queue and uses a character array.

2) Create a class named LastQueue that implements Queue and uses a character array.

3) Create a class named Median that implements Stack and uses a character array.

4) Read a String from file reserve.txt into an attribute.

5). Load it with one character at a time into FirstQueue

6) Move each character from FirstQueue into Median and then, when FirstQueue is empty, move the each character from Median into LastQueue.

7) Add them one at a time into a solution attribute of type StringBuilder.

8) Print out the original String and the StringBuilder. They should be reversed.

9) If the file reserve.txt is not empty go to step 4.

10) Fill the file reserve.txt with the following:

FGHI

ABATATOTHAPLANECARTEL

y+z

CAT

Explanation / Answer

import java.io.*;
import java.util.*;

class FirstQueue{
   char[] ch;  
   int curr;
   int prev;
   public FirstQueue(int n){
       ch = new char[n];
       curr = 0;
       prev = 0;
   }  
   void push(char c){
       ch[curr] = c;
       curr++;
   }
   char pop(){
       prev += 1;
       return ch[prev];
   }
   boolean empty(){
       if (prev == curr) return true;
       return false;
   }
}

class Median{
   char[] ch;
   int curr;
   public Median(int n){
       ch = new int[n];
   }
   void push(char c){
       ch[curr] = c;
       curr++;
   }
   char pop(){
       curr--;
       return ch[curr];
   }
   boolean empty(){
       if (curr == 0) return true;
       return false;
   }
}

class main{
   public static void main(String[] args){
       BufferedReader br = null;
       try {
           String line;
           br = new BufferedReader(new FileReader("reverse.txt"));
           while ((line = br.readLine()) != null) {
               FirstQueue fq = new FirstQueue(line.length());
               Median md = new Median(line.length());
               for (int i = 0; i < line.length(); i++){
                   fq.push(line.charAt(i));
               }
               while (fq.empty() == false){
                   md.push(fq.pop());
               }
               while (md.empty() == false){
                   fq.push(md.pop());
               }
               String s;
               while (fq.empty() == false){
                   s += fq.pop();
               }
               System.out.println(s);
           }
       }
       catch (IOException e) {
           e.printStackTrace();
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote