Question

In: Computer Science

Please provide the missng code(parts) for the given code in java. ASAP. import java.util.Stack; class StackQueue<T>...

Please provide the missng code(parts) for the given code in java. ASAP.
import java.util.Stack;

class StackQueue<T> implements Queue<T>{
        private Stack<T> inStack;
        private Stack<T> outStack;
        private int size;

        public StackQueue(){
                inStack = new Stack<T> ();
                outStack = PART(a);
                size = 0;
        }

        public int size(){
                return size;
        }

        public boolean isEmpty(){
                return size==0;
        }

        public T first(){
                if (size == 0) return null;;
                if (outStack.empty())
                        while (!inStack.empty())
                                outStack.push(inStack.pop());
                return PART(b); //return the element at the top of the outStack without removing it from the stack
        }

        public void enqueue(T x){
                PART(c); //push x to inStack 
                size++;
        }

        public T dequeue(){
                if (size == 0) return PART(d);
                if (outStack.empty())
                        while (!inStack.empty())
                                outStack.push(inStack.pop());
                size--;
                return PART(e); //Removes and returns the element at the top of the outStack
        }

        public String toString(){
                Stack<T> temp = new Stack<>();
                String ans = "StackQueue: ";
                if (isEmpty()) return ans;;
                if (outStack.empty())
                        while (!inStack.empty())
                                outStack.push(inStack.pop());

                while (!outStack.empty()){
                        T x = outStack.pop();
                        ans += (x + " -> ");
                        temp.push(x);
                }

                while (!inStack.empty())
                        outStack.push(inStack.pop());
                while (!outStack.empty()){
                        T x = outStack.pop();
                        ans += (x + " -> ");
                        temp.push(x);
                }

                while (!temp.empty())
                        outStack.push(temp.pop());

                return ans;
        }

        public static void main(String[] args){
                StackQueue<Integer> q = new StackQueue<>();
                q.enqueue(0);
                System.out.println("first element: " + q.first());
                q.enqueue(1);
                q.enqueue(2);
                q.dequeue();
                System.out.println(q.toString());
                System.out.println("first element: " + q.first());
                q.enqueue(3);
                q.enqueue(4);
                q.enqueue(5);
                System.out.println(q.toString());
                q.dequeue();
                System.out.println(q.toString());
                System.out.println("first element: " + q.first());
        }

}

/**
 * Interface for a queue: a collection of elements that are inserted
 * and removed according to the first-in first-out principle.
 */
interface Queue<T> {

  /**
   * Inserts an element at the rear of the queue.
   * @param e  the element to be inserted
   */
  void enqueue(T e);

  /**
   * Removes and returns the first element of the queue.
   * @return element removed (or null if empty)
   */
  T dequeue();

  /**
   * Returns the number of elements in the queue.
   * @return number of elements in the queue
   */
  int size();

  /**
   * Tests whether the queue is empty.
   * @return true if the queue is empty, false otherwise
   */
  boolean isEmpty();


  /**
   * Returns, but does not remove, the first element of the queue.
   * @return the first element of the queue (or null if empty)
   */
  T first();

}

Solutions

Expert Solution

/**
 * Interface for a queue: a collection of elements that are inserted
 * and removed according to the first-in first-out principle.
 */
import java.util.*;
interface Queue<T> {

    /**
     * Inserts an element at the rear of the queue.
     * @param e  the element to be inserted
     */
    void enqueue(T e);
  
    /**
     * Removes and returns the first element of the queue.
     * @return element removed (or null if empty)
     */
    T dequeue();
  
    /**
     * Returns the number of elements in the queue.
     * @return number of elements in the queue
     */
    int size();
  
    /**
     * Tests whether the queue is empty.
     * @return true if the queue is empty, false otherwise
     */
    boolean isEmpty();
  
  
    /**
     * Returns, but does not remove, the first element of the queue.
     * @return the first element of the queue (or null if empty)
     */
    T first();
  
  }
  class StackQueue<T> implements Queue<T>{
    private Stack<T> inStack;
    private Stack<T> outStack;
    private int size;

    public StackQueue(){
            inStack = new Stack<T> ();
            outStack = inStack; //Set outStack as inStack at the time of construction
            size = 0;
    }

    public int size(){
            return size;
    }

    public boolean isEmpty(){
            return size==0;
    }

    public T first(){
            if (size == 0) return null;// if size is 0 then stack is empty
            if (outStack.empty())
                    while (!inStack.empty())
                            outStack.push(inStack.pop());
            return outStack.peek(); //PART(b)  return the element at the top of the outStack without removing it from the stack
        //peek()function returns the top element without deleting it
        }

    public void enqueue(T x){
           inStack.push(x); //PART(c) push x to inStack 
           //Inserting at the end of stack using push() function
            size++;
    }

    public T dequeue(){
            if (size == 0) return null;//PART(d)
            if (outStack.empty())
                    while (!inStack.empty())
                            outStack.push(inStack.pop());
            size--;
            return outStack.pop(); //PART(e) Removes and returns the element at the top of the outStack
    }

    public String toString(){
            Stack<T> temp = new Stack<>();
            String ans = "StackQueue: ";
            if (isEmpty()) return ans;;
            if (outStack.empty())
                    while (!inStack.empty())
                            outStack.push(inStack.pop());

            while (!outStack.empty()){
                    T x = outStack.pop();
                    ans += (x + " -> ");
                    temp.push(x);
            }

            while (!inStack.empty())
                    outStack.push(inStack.pop());
            while (!outStack.empty()){
                    T x = outStack.pop();
                    ans += (x + " -> ");
                    temp.push(x);
            }

            while (!temp.empty())
                    outStack.push(temp.pop());

            return ans;
    }

    public static void main(String[] args){
            StackQueue<Integer> q = new StackQueue<>();
            q.enqueue(0);
            System.out.println("first element: " + q.first());
            q.enqueue(1);
            q.enqueue(2);
            q.dequeue();
            System.out.println(q.toString());
            System.out.println("first element: " + q.first());
            q.enqueue(3);
            q.enqueue(4);
            q.enqueue(5);
            System.out.println(q.toString());
            q.dequeue();
            System.out.println(q.toString());
            System.out.println("first element: " + q.first());
    }

}

Sample Output

Missing parts

outStack = inStack; //part a

return outStack.peek(); //PART(b)  return the element at the top of the outStack without removing it from the stack

        //peek()function returns the top element without deleting it

inStack.push(x); //PART(c) push x to inStack

           //Inserting at the end of stack using push() function

if (size == 0) return null;//PART(d)

return outStack.pop(); //PART(e) Removes and returns the element at the top of the outStack


Related Solutions

Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT