Question

can you please explain how to complete all methods in java ? thanks /* Note:   Do...

can you please explain how to complete all methods in java ?
thanks

/*
Note:   Do not add any additional methods, attributes.
        Do not modify the given part of the program.
        Run your program against the provided Homework2Driver.java for requirements.
*/

/*
Hint:   This Queue implementation will always dequeue from the first element of
        the array i.e, elements[0]. Therefore, remember to shift all elements
        toward front of the queue after each dequeue.
*/

public class QueueArray<T> {
    public static int CAPACITY = 100;
    private final T[] elements;
    private int rearIndex = -1;
   
    public QueueArray() {
    }

    public QueueArray(int size) {
    }
   
    public T dequeue() {

    }
   
    public void enqueue(T info) {

    }
   
    public boolean isEmpty() {
       
    }
   
    public boolean isFull() {
       
    }
   
    public int size() {
       
    }
}

Homework Answers

Answer #1

public class QueueArray<T> {
public static int CAPACITY = 100;
private final T[] elements;
private int rearIndex = -1;

public QueueArray() {
       elements = (T[])new Object[CAPACITY];
}

public QueueArray(int size) {
       CAPACITY =size;
elements = (T[])new Object[CAPACITY];
}

public T dequeue() {
if(isEmpty())return null;
else
{
T t = elements[0];
int i=0;
while(i< rearIndex )//moving front
{
elements[i]=elements[i+1];
i++;
}
rearIndex -=1;//decreasing rear index
return t;
}
}

public void enqueue(T info) {
if(!isFull()){
elements[ rearIndex +1]=info;
rearIndex +=1;
}
}

public boolean isEmpty() {
if( rearIndex ==-1)return true;
return false;

}

public boolean isFull() {
if( rearIndex +1 == CAPACITY)return true;
return false;
}

public int size() {
return rearIndex +1;
}
}

Know the answer?
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
Similar Questions
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class QueueLinkedList<T> { private Node front; private Node rear; private int size; public QueueLinkedList() { } public void enqueue(T info) { } public T dequeue() { } public boolean isEmpty() } public int size() { } } Run this program ( Homework2Driver.java ) to test. Comment out...
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the...
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the Queue class implementation and you need to write a method that takes a Queue and sorts it using the selection sort idea. The implementation of the Queue class for long data type is given in the lecture slides (the code skeleton including the implementation of the Queue class is provided below for your convenience). You should go over the Queue and use enqueue(), dequeue(),...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initializes an empty queue.    */    public StringQueue() { //TODO    }    /**    * Returns true if this queue...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class StackLinkedList<T> { private Node topPointer; private int size; public StackLinkedList() { } public void push(T info) { } public T pop() { } public T top() { } public boolean isEmpty() { } public int size() { } } Run this program ( Homework2Driver.java ) to...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class Node<T> { private T info; private Node nextLink; public Node(T info) { } public void setInfo(T info) { } public void setNextLink(Node nextNode) { } public T getInfo() { } public Node getNextLink() { } } Run this program ( Homework2Driver.java ) to test. Comment out...
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT