Question

In: Computer Science

Add The following methods to the LinkedQueue class and create a test driver for each to...

Add The following methods to the LinkedQueue class and create a test driver for each to show they work correctly. In order to practice your linked list coding skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously defined public methods of the class. a. String toString () creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class and for testing and debugging applications tha

t use the class. Assue each queued element already provides its own reasonable toString method. b. void remove (int count) removes the front count elements from the queue; throws QueueUnderflowException if less than count elements are in the queue. c. boolean swapStart () returns false if less than two elements are in the queue, otherwise reverse the order of the front two elements in the queue and returns true. d. boolean swapEnds() returns false if there are less than two elements in the queue, otherwise swaps the first and last elements of the queue and returns true.

LinkedQueue.java:

package ch04.queues;

import support.LLNode;

public class LinkedQueue<T> implements QueueInterface<T> {

    protected LLNode<T> front;

    protected LLNode<T> rear;

    protected int numElements = 0;

    public LinkedQueue(){

        front = null; rear = null;

    }

    public void enqueue(T element){

        LLNode<T> newNode = new LLNode<T>(element);

        if (rear == null)

            front = newNode;

        else

            rear.setLink(newNode);

        rear = newNode;

        numElements++;

    }

    public T dequeue(){

        if (isEmpty())

            throw new QueueUnderflowException("Dequeue attempted on empty queue.");

        else{

            T element;

            element = front.getInfo();

            front = front.getLink();

            if (front == null)

                rear = null;

            numElements--;

            return element;

        }

    }

}

LLNode.java:

//----------------------------------------------------------------------------

// LLNode.java by Dale/Joyce/Weems Chapter 2

//

// Implements <T> nodes for a Linked List.

//----------------------------------------------------------------------------

package support;

public class LLNode<T>{

    private LLNode<T> link;

    private T info;

    

    public LLNode(T info)

    {

        this.info = info;

        link = null;

    }

    

    public void setInfo(T info){

        // Sets info of this LLNode.

        this.info = info;

    }

    

    public T getInfo()

    // Returns info of this LLONode.

    {

        return info;

    }

    

    public void setLink(LLNode<T> link)

    // Sets link of this LLNode.

    {

        this.link = link;

    }

    public LLNode<T> getLink()

    // Returns link of this LLNode.

    {

        return link;

    }

}

QueueInterface.java

package ch04.queues;

public interface QueueInterface<T> {

    

    void enqueue(T element) throws QueueOverflowException1;

    T dequeue() throws QueueUnderflowException;

    boolean isFull();

    boolean isEmpty();

    int size();

}

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU



1. QueueInterface.java

package ch04.queues;

public interface QueueInterface<T> {

   void remove(int count) throws QueueUnderflowException;

   boolean swapStart();

   boolean swapEnds();

}

2. LLNode.java

package support;

public class LLNode<T> {

   private T element;
   private LLNode<T> next;

   public LLNode(T element) {
       this.element = element;
   }

   public void setNext(LLNode<T> node) {
       this.next = node;

   }

   public T getElement() {
       return element;
   }
  
  

   /**
   * @param element the element to set
   */
   public void setElement(T element) {
       this.element = element;
   }

   public LLNode<T> getNext() {
       return next;
   }

}

3. LinkedQueue.java

package ch04.queues;

import support.LLNode;

public class LinkedQueue<T> implements QueueInterface<T> {

   protected LLNode<T> front; // reference to the front of this queue
   protected LLNode<T> rear; // reference to the rear of this queue
   protected int numElements = 0; // number of elements in this queue

   public LinkedQueue() {
       front = null;
       rear = null;
   }

   public void enqueue(T element) {
       LLNode<T> node = new LLNode<T>(element);

       if (isEmpty())
           front = node;
       else
           rear.setNext(node);
       rear = node;
       numElements++;
   }

   /**
   * The remove method removes the front count elements from the queue
   *
   * @param count
   * @throws QueueUnderflowException
   */
   @Override
   public void remove(int count) throws QueueUnderflowException {
       for (int i = 1; i <= count; i++) {
           dequeue();
       }
   }

   /**
   * The swapStart method returns false if less than two elements are in the
   * queue, otherwise reverses the order of the front two elements in the
   * queue and returns true.
   *
   * @return
   * @throws QueueUnderflowException
   */
   public boolean swapStart() {
       if (numElements < 2)
           return false;
       else {
           // swap
           T firstElement = front.getElement();
           T lastElement = front.getNext().getElement();
           front.setElement(lastElement);
           front.getNext().setElement(firstElement);
           return true;
       }

   }

   /**
   * The swapEnds method returns false if there are less than two elements in
   * the queue, otherwise swaps the first and last elements of the queue and
   * returns true
   *
   * @return
   */
   public boolean swapEnds() {
       if (numElements < 2)
           return false;
       else {
           T firstElement = front.getElement();
           T lastElement = rear.getElement();
           front.setElement(lastElement);
           rear.setElement(firstElement);
           return true;

       }
   }

   public T dequeue() throws QueueUnderflowException {
       if (isEmpty())
           throw new QueueUnderflowException("Queue Underflow!");

       T result = front.getElement();
       front = front.getNext();
       numElements--;

       if (isEmpty())
           rear = null;
       return result;
   }

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

   public String toString() {
       String result = "";
       LLNode<T> current = front;

       while (current != null) {
           result = result + (current.getElement()).toString() + " ";
           current = current.getNext();
       }

       return result;
   }
}

4. QueueUnderflowException.java

package ch04.queues;

@SuppressWarnings("serial")
public class QueueUnderflowException extends Exception {
   private String message;
   public QueueUnderflowException(String message) {
       this.message=message;
       System.out.println(this.message);
   }
}

5. LinkedQueueDriver.java

package ch04.queues;

public class LinkedQueueDriver {

   public static void main(String[] args) {
       try {
           LinkedQueue<Integer> linkedQueue = new LinkedQueue<Integer>();
           linkedQueue.enqueue(10);
           linkedQueue.enqueue(20);
           linkedQueue.enqueue(30);
           linkedQueue.enqueue(40);
           linkedQueue.enqueue(50);
           System.out.println("Queue");
           System.out.println(linkedQueue);

           System.out.println("after swapStart");
           linkedQueue.swapStart();
           System.out.println(linkedQueue);
           System.out.println("after swapEnds");
           linkedQueue.swapEnds();
           System.out.println(linkedQueue);
           System.out.println("after remove 2 count");
           linkedQueue.remove(2);
           System.out.println(linkedQueue);
           linkedQueue.remove(3);
           System.out.println("after remove 3 count");
           System.out.println(linkedQueue);
           linkedQueue.remove(1);
       } catch (QueueUnderflowException e) {
           e.printStackTrace();
       }

   }

}


Related Solutions

28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack 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 ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Add the following method below to the CardDeck class, and create a test driver to show...
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly. int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck. Complete in Java programming language. // Models a deck of cards. Includes shuffling and dealing. //---------------------------------------------------------------------- package Homework4; import java.util.Random; import java.util.Iterator; import javax.swing.ImageIcon; public class CardDeck { public static final int NUMCARDS = 52; protected ABList<Card> deck; protected Iterator<Card> deal; public CardDeck() { deck...
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
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....
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT