Question

Objectives During this lab, students will learn how to 1. Implement a queue using arrays Instructions For this lab you will be using your arrayQueueu algorithms that you completed for Homework 10. You are to implement a class called arrayQueue, along with a driver class arrayDriver and the customer exception class that will contain any exceptions that you may need to throw (i.e. emptyQueueException). This will be three seperate java files, and the package name will be PA5arrayQueue [please ensure that when you create your new project in NetBeans that you call it PA5arrayQueue that will name the package correctly] Here is the UML again for your reference ArrayQueue size int CAPACITY int top int bottom int data [] E + ArrayQueue () + dequeue() E enqueue (input E): void + isEmpty ) boolean t peek): E Deliverables Your three Java source files

0 0
Add a comment Improve this question Transcribed image text
Answer #1
package PA5arrayQueue;

import java.lang.reflect.Array;

import static java.lang.System.arraycopy;

class ArrayQueue<E> {
    private int size, top, bottom;
    private E[] data;
    private int CAPACITY;

    ArrayQueue(Class<E> aClass, int capacity) {
        CAPACITY = capacity;
        data = (E[]) Array.newInstance(aClass, capacity);
    }

    void enqueue(E element) throws ArrayQueueFullException {
        if (size == CAPACITY) {
            throw new ArrayQueueFullException("Queue is full");
        }
        data[size] = element;
        size++;
    }

    E dequeue() throws ArrayQueueEmptyException {
        if (isEmpty()) {
            throw new ArrayQueueEmptyException("Queue is empty");
        }
        E firstElement = data[0];
        moveElementsOneStepForward();
        size--;
        return firstElement;
    }

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

    E peak() throws Exception {
        if (isEmpty()) {
            throw new Exception();
        }
        return data[0];
    }

    private void moveElementsOneStepForward() {
        arraycopy(data, 1, data, 0, size - 1);
    }
}

----------------------------------------------------------------------------------------------------

package PA5arrayQueue;

class ArrayQueueEmptyException extends Exception {

    public ArrayQueueEmptyException(String message) {
        super(message);
    }

}

class ArrayQueueFullException extends Exception {

    public ArrayQueueFullException(String message) {
        super(message);
    }

}

----------------------------------------------------------------------------------------------------

package PA5arrayQueue;

public class ArrayDriver {

    public static void main(String[] args) throws Exception {
        ArrayQueue<Integer> arrayQueue = new ArrayQueue<>(Integer.class,5);
        System.out.println(String.valueOf(arrayQueue.isEmpty()));
        arrayQueue.enqueue(1);
        arrayQueue.enqueue(2);
        arrayQueue.dequeue();
        System.out.println(String.valueOf(arrayQueue.peak()));
        System.out.println(String.valueOf(arrayQueue.dequeue()));
    }

}
Add a comment
Know the answer?
Add Answer to:
Objectives During this lab, students will learn how to 1. Implement a queue using arrays Instructions...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

  • Problem 3 (20 points) Implement a queue abstract data type using the UnorderedList class. Problems 2-4...

    Problem 3 (20 points) Implement a queue abstract data type using the UnorderedList class. Problems 2-4 ask you to implement three abstract data types using the UnorderedList class described in chapter 3 in your book instead of the book's implementation using the Python list. # Problem 3 class Queue: def _init_(self): raise NotImplementedError def isEmpty(self): raise NotImplementedError def enqueue(self, item): raise NotImplementedError def dequeue(self): raise NotImplementedError def size(self): raise NotImplementedError

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

    (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT