Question

Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To...

Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. 
Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except 
the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any 
whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false.

given

NonZeroNumQueue.txt

public class NonZeroNumQueue{
private int[] data;
private int total;
private int front;
private int tail;
public NonZeroNumQueue(){
data=new int[10];
total=0;
front=tail=0;
}
public void enqueue(int num){
//Implement this method here
}
public void dequeue(){
if(!isEmpty()){
total--;
front = (front+1) %data.length;
}
}
public int peek(){
if(isEmpty())
return 0; //0 means the queue is empty
return data[front];
}
public boolean isEmpty(){
return total==0;
}
public boolean isFull(){
return false;
}
public int size(){
return total;
}
}

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

// here is the full code
// output is attached
// plz comment if you need any clarification
// hit like if you liked it

// CODE
import java.io.*;

public class NonZeroNumQueue{
   private int[] data;
   private int total;
   private int front;
   private int tail;
  
    public NonZeroNumQueue(){
      data=new int[10];
      total=0;
      front=tail=0;
   }
   public void enqueue(int num) throws Exception{
       //Implement this method here
       if(num == 0) { // if number is zero throw exception
           throw new Exception("Exception : Trying to insert zero to the queue");
       } else{ // if it is not zero then add it to the queue
           if(!isFull()) { // if queue is not full
               data[tail] = num; // add number at the end of the queue
               tail = (tail+1) % data.length; // update tail pointer
               total++; // increment the number of elements in the queue
           }
       }
   }
   public void dequeue(){
      if(!isEmpty()){
         total--;
         front = (front+1) %data.length;
      }
   }
   public int peek(){
      if(isEmpty())
         return 0; //0 means the queue is empty
      return data[front];
   }
   public boolean isEmpty(){
      return total==0;
   }
   public boolean isFull(){
      return false;
   }
   public int size(){
      return total;
   }
}

// Driver class to test the above Queue
// You can remove this class, if you want to
// This class is added to test above Queue
public class Chegg218 {
    public static void main(String... args) {
        NonZeroNumQueue queue = new NonZeroNumQueue();
       
        // add elements
        try {
            queue.enqueue(2);
            System.out.println("2 is added");
            queue.enqueue(3);
            System.out.println("3 is added");
            queue.enqueue(1);
            System.out.println("1 is added");
            queue.enqueue(5);
            System.out.println("5 is added");
            queue.enqueue(0); // exception Occurred
            System.out.println("0 is added");
            queue.enqueue(23);
            System.out.println("23 is added");
        } catch(Exception e) { // if any exception is throwed, it will be catched here
            System.out.println(e); // print the error message
        }    
    }
}


// OUTPUT

Your Output (stdout) 2 is added 3 is added 1 is added 5 is added java.lang. Exception: Exception Trying to insert zero to the

Add a comment
Know the answer?
Add Answer to:
Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To...
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
  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

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

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

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

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

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

  • use intellij idea main java wp the professor. Please make sure to only implement what is...

    use intellij idea main java wp the professor. Please make sure to only implement what is asked for. You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue...

  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

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