Question

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 / ArraryList classes ) Method Members: public void enqueue(int value): This should add the value into the queue. public int dequeue(): This should remove and return the int which was added first. public boolean isEmpty(): Returns true if the queue is empty, false otherwise Driver code: QueueInt testQ = new QueueInt(); testQ. Enqueue(5); testQ. Enqueue(50); testQ. Enqueue(6); System.out.println(test.dequeue()); // it should print 5

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

Code

QueueInt.java


import java.util.NoSuchElementException;


public class QueueInt
{
protected int Queue[] ;
protected int front, rear, len;
static int size=5;
public QueueInt()
{
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to Enqueue an element to the queue */
public void Enqueue(int i)
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
/* Function to dequeue front element from the queue */
public int dequeue()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
{
len-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
}
main.java


public class main {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
QueueInt testQ = new QueueInt();
testQ. Enqueue(5);
testQ. Enqueue(50);
testQ. Enqueue(6);
System.out.println(testQ.dequeue());
}
  
}
output

Queue Integer-NetBeans IDE 8.0.1 Eile Edit View Navigete Source Refgctor Run Debug Profile Team Iools Window Help Search (Ctr

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
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...
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
  • 5. Stack (12 pts) Build a Stack class for a st The Stack should operat and...

    5. Stack (12 pts) Build a Stack class for a st The Stack should operat and methods also listed below ack of doubles that is compatible with the driver code below e in a LIFO (last in, first out) fashion and implement the variables a. Data Members: Declare the data item(s) you will need to manage a stack of doubles. Method Members: public void push (double input): This should add the double input to your stack public double pop ():...

  • 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...

  • 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....

  • 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....

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • 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...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

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