Question

Step 4 Develop a class with only a main method in it: public class QueueDemo {...

Step 4

Develop a class with only a main method in it:

public class QueueDemo {

public static void main(String[ ] args) {

/*   Inside of this main method do the following:

Create a reference to a QueueInterface<String> called myQueue and have it refer to a new object of the ImprovedQueue<String> class

    

     Call the enqueue method on myQueue passing the String value of “A”

Call the enqueue method on myQueue passing the String value of “B”

Call the enqueue method on myQueue passing the String value of “C”

Call the enqueue method on myQueue passing the String value of “D”

Create a String variable called discard and sets it value to the value that is returned from a call to dequeue on myQueue

Set the value of discard to the value that is returned from a call to dequeue on myQueue

Create a loop that continues as long as the queue is not empty and inside this loop create a variable called front that is set each time through the loop to the value that is returned from a call to dequeue on myQueue and then the value of front is printed each time through the loop

*/  

}

}

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

main() logic :

  • Create a reference to QueueInterface<String> call it myQueue
  • store a new ImprovedQueue<String> object in myQueue.
  • Insert "A", "B" , "C" , "D" into myQueue using enqueue() method.
  • call dequeue() and store the returned element inside a String variable discard.
  • Loop until myQueue.isEmpty() returns true
    • 1.) call dequeue and store the returned value in front
    • 2.) print front

implementation:

public class QueueDemo{
   public static void main(String[] args){
       QueueInterface<String> myQueue;
       myQueue = new ImprovedQueue<String>();
       myQueue.enqueue("A");
       myQueue.enqueue("B");
       myQueue.enqueue("C");
       myQueue.enqueue("D");
       String discard = myQueue.dequeue();
       while(!myQueue.isEmpty()){
           String front = myQueue.dequeue();
           System.out.println(front);
       }
   }
}

the complete program:

  • I think I answered all the 4 steps in your previous posts. So here is the complete implementation:

interface QueueInterface<T>{

public boolean isEmpty();

public T dequeue();

public void enqueue(T element);

}

class QueueNode<T> {
   private T info;
   private QueueNode<T> link;
   public QueueNode(T i){
       this.info = i;
       this.link = null;
   }
   public void setInfo(T i){
       this.info = i;
   }
   public T getInfo(){
       return this.info;
   }
   public void setLink(QueueNode<T> q){
       this.link = q;
   }
   public QueueNode<T> getLink(){
       return this.link;
   }

}

class ImprovedQueue<T> implements QueueInterface<T>{
   private QueueNode<T> front;
   public ImprovedQueue(){
       this.front = null;
   }
   public boolean isEmpty(){
       if(this.front==null)
           return true;
       else
           return false;
   }
   public T dequeue(){
       if(isEmpty())
           return null;
       QueueNode<T> temp = front;
       front = front.getLink();
       temp.setLink(null);
       return temp.getInfo();
   }
   public void enqueue(T element){
       QueueNode<T> newNode = new QueueNode<T>(element);
       if(isEmpty())
           this.front = newNode;
       else{
           QueueNode<T> temp = front;
           while(temp.getLink()!=null)
               temp = temp.getLink();
           temp.setLink(newNode);
       }
   }

}

public class QueueDemo{
   public static void main(String[] args){
       QueueInterface<String> myQueue;
       myQueue = new ImprovedQueue<String>();
       myQueue.enqueue("A");
       myQueue.enqueue("B");
       myQueue.enqueue("C");
       myQueue.enqueue("D");
       String discard = myQueue.dequeue();
       while(!myQueue.isEmpty()){
           String front = myQueue.dequeue();
           System.out.println(front);
       }
   }
}

output:

IB C D

Add a comment
Know the answer?
Add Answer to:
Step 4 Develop a class with only a main method in it: public class QueueDemo {...
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
  • use intellij idea main java Step 4 Develop a class with only a main method in...

    use intellij idea main java Step 4 Develop a class with only a main method in it: public class QueueDemot public static void main(String[] args) { Inside of this main method do the following: Create a reference to a Queue Interface<String> called myQueue and have it refer to a new object of the ImprovedQueue<String> class Call the enqueue method on myQueue passing the String value of "A" Call the enqueue method on myQueue passing the String value of "B" Call...

  • i was able to make sense on the others but this two i need help Name:...

    i was able to make sense on the others but this two i need help Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                            Access modifier: public Parameters:...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

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

  • Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty...

    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 Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...

  • what is wrong with my code. what should I do with this kind of error 61...

    what is wrong with my code. what should I do with this kind of error 61 QueueDemo java - Files - RaProjects/one- A Queue Demoava Queueinterface Java inport java.util.LinkedList; import java.util.Queue public class QueueDemo public static void main(String[] args) { 10 11 12 13 Queue String myQueue new LinkedlistString();//Create a reference to a queue Interface myqueue.add("A");//Call the enqueue method on myQueue passing the string value of "A" myQueue.add("B");//call the enqueue method on nyQueue passing the String value of "3" myQueue.add("C");//Call...

  • use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier:...

    use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...

  • Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables...

    Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                           Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...

  • using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public...

    using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public void enqueue(T data) { list.add(data); } public T dequeue() { return list.remove(0); } public T peek() { return list.get(0); } public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } } class MyQueueTest { public static void main(String[] args) { MyQueue<Integer> queue = new MyQueue<Integer>(); queue.enqueue(3); queue.enqueue(2); queue.enqueue(7); queue.enqueue(1); while (!queue.isEmpty()) { System.out.println(queue.dequeue()); } } } please solve the following:...

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