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

in this programming, I should have 4 classes, but I dont know how to do with obj

ID: 3607164 • Letter: I

Question

in this programming, I should have 4 classes, but I dont know how to do with object class, and when run the code, it has problme that keep repeating.

here is the code of each classes:

public class QueueElement {
int item;
QueueElement next;
public QueueElement()
{
}
public QueueElement(int d)
{
item=d;
}
public void setData(int d)
{
item=d;
}
public void setPtrToNextElement(QueueElement q)
{
next=q;
}
public QueueElement returnPtrToNextElement()
{
return next;
}
}

public static void main(String[] args) {
Scanner StdIn = new Scanner(System.in);
Queue q = new Queue();
int item = 0;
boolean status = q.isEmpty();
if (status == true) {
System.out.print("Queue is empty ");
} else {
System.out.print("Queue is not empty");
}
while (item != -999) {
System.out.println("Please Enter element:");
item = StdIn.nextInt();
if (item != 0) {
q.enqueue(item);
} else if (!q.isEmpty()) {
System.out.print(q.dequeue() + " ");
}
}
System.out.println("Number of elements in queue: " + q.size());

System.out.println("Queue: " + q.toString());
}
}

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Queue {

private int N; // number of elements on queue
private QueueElement head; // beginning of queue
private QueueElement tail; // end of queue

public Queue() {
N = 0;
head = null;
tail = null;
}

//return true if this queue is empty; false otherwise
public boolean isEmpty() {
return head == null;
}

//return the number of items in this queue
public int size() {
return N;
}

//Adds the item to this queue.
public void enqueue(Object element) {
QueueElement oldlast = tail;
tail = new QueueElement();
tail.item = (int) element;

//tail.data = (int)element;
tail.next = null;
if (isEmpty()) {
head = tail;
} else {
oldlast.next = tail;
}
N++;
assert check();
}

//Removes and returns the item on this queue that was least recently added.
public Object dequeue() {
if (isEmpty()) {
throw new NoSuchElementException("Queue underflow");
}
int item = head.item;
head = head.next;
N--;
if (isEmpty()) {
tail = null; // to avoid loitering
}
assert check();
return item;
}

//an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator {

private QueueElement current = head;

public boolean hasNext() {
return current != null;
}

public void remove() {
throw new UnsupportedOperationException();
}

public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int item = current.item;
current = current.next;
return item;
}
}

public Iterator iterator() {
return new ListIterator();
}

public String toString() {
StringBuilder s = new StringBuilder();
for (Object listIter = iterator().next(); iterator().hasNext();) {
s.append(iterator().next()).append(" ");
}
return s.toString();
}

// check internal invariants
private boolean check() {
if (N < 0) {
return false;
} else if (N == 0) {
if (head != null) {
return false;
}
if (tail != null) {
return false;
}
} else if (N == 1) {
if (head == null || tail == null) {
return false;
}
if (head != tail) {
return false;
}
if (head.next != null) {
return false;
}
} else {
if (head == null || tail == null) {
return false;
}
if (head == tail) {
return false;
}
if (head.next == null) {
return false;
}
if (tail.next != null) {
return false;
}

//check internal consistency of instance variable N
int numberOfNodes = 0;
for (QueueElement x = head; x != null && numberOfNodes <= N; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N) {
return false;
}

//check internal consistency of instance variable tail
QueueElement tailNode = head;
while (tailNode.next != null) {
tailNode = tailNode.next;
}
if (tail != tailNode) {
return false;
}
}

return true;
}
}

here is the result:

Queue is empty Please Enter element 12 Please Enter element 12 Please Enter element 12 Please Enter element

Explanation / Answer

import java.util.*;

class Node
{
protected int data;
protected Node link;

public Node()
{
link = null;
data = 0;
}   

public Node(int d,Node n)
{
data = d;
link = n;
}   
public void setLink(Node n)
{
link = n;
}   

public void setData(int d)
{
data = d;
}   

public Node getLink()
{
return link;
}   

public int getData()
{
return data;
}
}

class linkedQueue
{
protected Node front, rear;
public int size;

public linkedQueue()
{
front = null;
rear = null;
size = 0;
}   
  
public boolean isEmpty()
{
return front == null;
}   
public int getSize()
{
return size;
}   
public void insert(int data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}   
public int remove()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();   
if (front == null)
rear = null;
size-- ;   
return ptr.getData();
}   
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}   
public void display()
{
System.out.print(" Queue = ");
if (size == 0)
{
System.out.print("Empty ");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();   
}
}

public class LinkedQueueImplement
{   
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);   
linkedQueue lq = new linkedQueue();
System.out.println("Linked Queue Test ");
char ch;   
do
{
System.out.println(" Queue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
lq.insert( scan.nextInt() );
break;
case 2 :
try
{
System.out.println("Removed Element = "+ lq.remove());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}   
break;
case 3 :
try
{
System.out.println("Peek Element = "+ lq.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+ lq.isEmpty());
break;

case 5 :
System.out.println("Size = "+ lq.getSize());
break;  

default :
System.out.println("Wrong Entry ");
break;
}   
/* display queue */   
lq.display();

System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);   
} while (ch == 'Y'|| ch == 'y');   
}
}