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: 3685726 • 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(){
    }
  
  
}

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 null.
*/
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)

*/