Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* * To change this license header, choose License Headers in Project Properties

ID: 3685746 • Letter: #

Question

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.queue;

/**
*
* @author zumrut
*/
public class Main {
public static void main(String[] args) {
Main mainClass = new Main();
Queue list = new Queue();

list.add("Book");
list.add("Lappy");
System.out.println("Queue Length : "+list.size());

System.out.println("Peek or Element in Queue : "+list.element());
System.out.println("List Of Elements in Queue : ");
list.iterate();
  
System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Queue Length : "+list.size());
System.out.println("Peek or Element in Queue : "+list.element());

list.add("Computer");
System.out.println("Length : "+list.size());
System.out.println("List Of Elements in Queue : ");
list.iterate();

System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Length : "+list.size());

System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Length : "+list.size());

System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Length : "+list.size());
}
}

package com.queue;

public class Node {

private String name = "";
private Node next;

public Node(String name) {
this.name = name;
this.next = null;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return " Name : "+this.name;
}
}

package com.queue;

public class Queue {


private int size = 0;
private Node root = null;

  
/*
* It has to return the size of the Queue
*
* @return size
*/
public int size() {
return size;
}

/*
* The method should add an element to the tail of the queue
*
* @Param - String, New element to the root.
*/
public void add(String item) {


}

/*
* The method should remove and return the head of the queue
*
* @Return - NoSuchElementException, if the queue is empty
* - String,if the queue contains elements
*/
public String remove() {

}

/*
* The method should return the head of the queue
*
* @Return - NoSuchElementException, if the queue is empty
* - String,if the queue contains elements
*/
public String element() {
}

/*
* is the Queue empty?
*
*/
public boolean isEmpty() { return root == null; }
  
/**
* Start with the head and traverse till you reach null.
*/
public void iterate(){
}
  
  
}

Lab 08.pdf fi Chegg Study l Guided Solu x https://blackboardalbany.edu/bbcswebdav/pid2220677-dt-content-rid-10511516-1/courses/2163-ICSI-310-1678/Lab%2008.pdf NEW ? > > E H: Delta Book a flight [] Shop Agent Lister T YouTube co Lab 08.pdf 1. Given a class 'Node' and 'Queue', that contains the below information diagram. Node Queue name: String next: Node size: int root: Node +Node(name: String) +setName(name: String): void +getNameString +setlextnext: Node): void +getNext: Node +addlitem: String): void +remove): String + size): nt +iterate): void + elementO: Sring +isEmpty0: boolean Implement add(String), remove() and element()methods in the Quue class which is provided to you, The methods should work as: add(String item) It has to take a 'new item (String)' and add it to the tail of a queue, if there are no nodes, then the 'new item (String) should the first node It has to remove and return the 'first Item' from the Queue, if there are no items then return 'NoSuchElementException' remove0 element() It has to return the 'first Item' from the Queue, if there are no items then return 'NoSuchElementException' Size) It should return the size of the Queue. Iterate() It should traverse the queue from head to tail.

Explanation / Answer

############ Main.java ################

public class Main {
  
    public static void main(String[] args) {
      
       Queue list = new Queue();

       list.add("Book");
       list.add("Lappy");
       System.out.println("Queue Length : "+list.size());

       System.out.println("Peek or Element in Queue : "+list.element());
       System.out.println("List Of Elements in Queue : ");
       list.iterate();
            
       System.out.println("Remove element from Queue : "+list.remove());
       System.out.println("Queue Length : "+list.size());
       System.out.println("Peek or Element in Queue : "+list.element());

       list.add("Computer");
       System.out.println("Length : "+list.size());
       System.out.println("List Of Elements in Queue : ");
       list.iterate();

       System.out.println("Remove element from Queue : "+list.remove());
       System.out.println("Length : "+list.size());

       System.out.println("Remove element from Queue : "+list.remove());
       System.out.println("Length : "+list.size());

       System.out.println("Remove element from Queue : "+list.remove());
       System.out.println("Length : "+list.size());
    }
}


########## Node.java #########

public class Node {

       private String name = "";
       private Node next;
  
       public Node(String name) {
           this.name = name;
           this.next = null;
       }
  
       public Node getNext() {
           return next;
       }

       public void setNext(Node next) {
           this.next = next;
       }

       public String getName() {
           return name;
       }

       public void setName(String name) {
           this.name = name;
       }
  
       public String toString() {
           return " Name : "+this.name;
       }
   }


######### Queue.java ############

import java.util.NoSuchElementException;

public class Queue {


    private int size = 0;
    private Node root = null;


    /*
     * It has to return the size of the Queue
     *
     * @return size
     */
    public int size() {
        return size;
    }

    /*
     * The method should add an element to the tail of the queue
     *
     * @Param - String, New element to the root.
     */
    public void add(String item) {

       Node newNode = new Node(item);
      
       if(root == null)
           root = newNode;
       else{
           Node temp = root;
           while(temp.getNext() != null)
               temp = temp.getNext();
           temp.setNext(newNode);
       }
      
       size++;
    }

    /*
     * The method should remove and return the head of the queue
     *
     * @Return - NoSuchElementException, if the queue is empty
     *            - String,if the queue contains elements
     */
    public String remove() {
      
       if(root == null)
           throw new NoSuchElementException();
      
       Node temp = root;
       root = root.getNext();
       size--;
       return temp.getName();
    }

    /*
     * The method should return the head of the queue
     *
     * @Return - NoSuchElementException, if the queue is empty
     *            - String,if the queue contains elements
     */
    public String element() {
       if(root == null)
           throw new NoSuchElementException();
      
       return root.getName();
    }

     /*
      * is the Queue empty?
      *
      */
    public boolean isEmpty() { return root == null; }

    /**
     * Start with the head and traverse till you reach nul   l.
     */
    public void iterate(){
      
       Node temp = root;
      
       while(temp != null){
           System.out.println(temp.toString());
           temp = temp.getNext();
       }
    }
}

/*

Output:

Queue Length : 2
Peek or Element in Queue : Book
List Of Elements in Queue :
Name : Book
Name : Lappy
Remove element from Queue : Book
Queue Length : 1
Peek or Element in Queue : Lappy
Length : 2
List Of Elements in Queue :
Name : Lappy
Name : Computer
Remove element from Queue : Lappy
Length : 1
Remove element from Queue : Computer
Length : 0
Exception in thread "main" java.util.NoSuchElementException
   at firstWeek.Queue.remove(Queue.java:51)
   at firstWeek.Main.main(Main.java:32)

*/