Question

In: Computer Science

JAVA public class StringQueue {    //You may NOT add any more fields to this class....

JAVA

public class StringQueue {
   //You may NOT add any more fields to this class.
   private Stack<String> stack1;
   private Stack<String> stack2;

   /**
   * Initializes an empty queue.
   */
   public StringQueue() {
       // TODO
       throw new RuntimeException("Not Implemented");
   }

   /**
   * Returns true if this queue is empty.
   *
   * @return {@code true} if this queue is empty; {@code false} otherwise
   */
   public boolean isEmpty() {
       // TODO
       throw new RuntimeException("Not Implemented");
   }

   /**
   * Returns the number of items in this queue.
   *
   * @return the number of items in this queue
   */
   public int size() {
       // TODO
       throw new RuntimeException("Not Implemented");
   }


   /**
   * Adds the item to this queue.
   *
   * @param item the item to add
   */
   public void enqueue(String item) {
       // TODO
       throw new RuntimeException("Not Implemented");
   }

   /**
   * Removes and returns the item on this queue that was least recently added.
   *
   * @return the item on this queue that was least recently added
   * @throws NoSuchElementException if the queue is empty
   */
   public String dequeue() throws NoSuchElementException {
       // TODO
       throw new RuntimeException("Not Implemented");
   }

Solutions

Expert Solution

StringQueue.java :

import java.util.*;  //import this line to use Stack in code.
public class StringQueue {
   //You may NOT add any more fields to this class.
   private Stack<String> stack1;
   private Stack<String> stack2;

   /**
   * Initializes an empty queue.
   */
   public StringQueue() {
       // TODO
       //Initializing both stacks
       stack1 = new Stack<String>();
       stack2 = new Stack<String>();
   }

   /**
   * Returns true if this queue is empty.
   *
   * @return {@code true} if this queue is empty; {@code false} otherwise
   */
   public boolean isEmpty() {
       // TODO
       /*
       if stack1 is empty then queue is empty
       */
       if(stack1.isEmpty()) {
           return true;
       }
       return false;
   }

   /**
   * Returns the number of items in this queue.
   *
   * @return the number of items in this queue
   */
   public int size() {
       // TODO
       /*
       stack1 size is the total size of queue
       get size of stack1 using sixe() method (Stack built in method to get size of stack)
       */
       return stack1.size();
   }


   /**
   * Adds the item to this queue.
   *
   * @param item the item to add
   */
   public void enqueue(String item) {
       // TODO
       
       /*
       For enqueue we are going to remove all element from stack1 and push all removed elements
       in stack2 then push ne item to stack1 and again push back removed element in stack1.
       */
        // Move all elements from stack1 to stack2  
        while (!stack1.isEmpty()) 
        {  
            stack2.push(stack1.pop());  
        }  
  
        // Push item into Stack stack1  
        stack1.push(item);  
  
        // Push everything back to stack1  
        while (!stack2.isEmpty())  
        {  
            stack1.push(stack2.pop());  
        }
   }

   /**
   * Removes and returns the item on this queue that was least recently added.
   *
   * @return the item on this queue that was least recently added
   * @throws NoSuchElementException if the queue is empty
   */
   public String dequeue() throws NoSuchElementException {
       // TODO
        // if first stack1 is empty emplies that Queue is empty
        //check it first if yes then return null
        if (stack1.isEmpty())  
        {  
            return null;  
        }  
  
        // Return top of stack1 (top of stack1 is the front element for Queue) 
        String x = stack1.peek();  //peek() is used to get top element from stack.
        stack1.pop();  
        return x;
   }
}

Driver Program to show working of StringQueue class :

Main.java :

import java.util.*;  //import this line to use Stack in code.
public class StringQueue {
   //You may NOT add any more fields to this class.
   private Stack<String> stack1;
   private Stack<String> stack2;

   /**
   * Initializes an empty queue.
   */
   public StringQueue() {
       // TODO
       //Initializing both stacks
       stack1 = new Stack<String>();
       stack2 = new Stack<String>();
   }

   /**
   * Returns true if this queue is empty.
   *
   * @return {@code true} if this queue is empty; {@code false} otherwise
   */
   public boolean isEmpty() {
       // TODO
       /*
       if stack1 is empty then queue is empty
       */
       if(stack1.isEmpty()) {
           return true;
       }
       return false;
   }

   /**
   * Returns the number of items in this queue.
   *
   * @return the number of items in this queue
   */
   public int size() {
       // TODO
       /*
       stack1 size is the total size of queue
       get size of stack1 using sixe() method (Stack built in method to get size of stack)
       */
       return stack1.size();
   }


   /**
   * Adds the item to this queue.
   *
   * @param item the item to add
   */
   public void enqueue(String item) {
       // TODO
       
       /*
       For enqueue we are going to remove all element from stack1 and push all removed elements
       in stack2 then push ne item to stack1 and again push back removed element in stack1.
       */
        // Move all elements from stack1 to stack2  
        while (!stack1.isEmpty()) 
        {  
            stack2.push(stack1.pop());  
        }  
  
        // Push item into Stack stack1  
        stack1.push(item);  
  
        // Push everything back to stack1  
        while (!stack2.isEmpty())  
        {  
            stack1.push(stack2.pop());  
        }
   }

   /**
   * Removes and returns the item on this queue that was least recently added.
   *
   * @return the item on this queue that was least recently added
   * @throws NoSuchElementException if the queue is empty
   */
   public String dequeue() throws NoSuchElementException {
       // TODO
        // if first stack1 is empty emplies that Queue is empty
        //check it first if yes then return null
        if (stack1.isEmpty())  
        {  
            return null;  
        }  
  
        // Return top of stack1 (top of stack1 is the front element for Queue) 
        String x = stack1.peek();  //peek() is used to get top element from stack.
        stack1.pop();  
        return x;
   }
}

Sample Output :

Please refer to the Screenshot of the code below to understand indentation of the code :

Screenshot of StringQueue class

Screenshot of Main class (Driver class) :


Related Solutions

Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
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!
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
In java, write a class that tests the following code. Add screen shots as well. public...
In java, write a class that tests the following code. Add screen shots as well. public class DoubleStack<E> {    private E[] elements;    private int top1, top2;    private static int DEFAULT_SIZE = 10;       public DoubleStack() {        elements = (E[])new Object[DEFAULT_SIZE];        top1 = 0;        top2 = elements.length - 1;    }       public boolean isFull(int stack) {        if(top2 == top1 + 1)            return true;   ...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add new instance variables or modify existing ones. private LinkedNode<T> head; private LinkedNode<T> tail; private int size; Q1: Write a method called "public void addFirst(T data)" that does the following: * Adds the element to the front of the deque. * Must be O(1) * @param data the data to add to the front of the deque * @throws java.lang.IllegalArgumentException if data is null Q2:...
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;   ...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
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 +=...
public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT