Question

Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...

Please explain code 1 and code 2 for each lines

code 1

public class MyQueue {

   public static final int DEFAULT_SIZE = 10;

   private Object data[];

   private int index;

code 2

package test;

import java.util.*;

/* Class Node */
class Node
{
protected Object data;
protected Node link;

/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(Object d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public Object getData()
{
return data;
}
}

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

/* Constructor */
public linkedQueue()
{
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}
/* Function to get the size of the queue */
public int getSize()
{
return size;
}
/* Function to insert an element to the queue */
public void insert(Object data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
/* Function to remove front element from the queue */
public Object remove()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getData();
}
/* Function to check the front element of the queue */
public Object peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}
/* Function to display the status of the queue */
public void display()
{
System.out.print("\nQueue = ");
if (size == 0)
{
System.out.print("Empty\n");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print((String)ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}

/* Class LinkedQueueImplement */
public class LinkedQueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedQueue */   
linkedQueue lq = new linkedQueue();
/* Perform Queue Operations */
System.out.println("Linked Queue Test\n");
  
lq.insert("java");
lq.insert("python");
lq.insert("html");
lq.insert("CSS");
System.out.println("Removed Element = "+ lq.remove());
  
System.out.println("Peek Element = "+ lq.peek());
System.out.println("Empty status = "+ lq.isEmpty());
  
System.out.println("Size = "+ lq.getSize());
  
/* display queue */
lq.display();

  
}
}

   public MyQueue() {
       data = new Object[DEFAULT_SIZE];
   }

   public MyQueue(int size) {
       data = new Object[size];
   }

   public boolean isEmpty() {
       return index == 0;
   }

public boolean isFull()
{
if(index == data.length - 1)
return true;
else
return false;
}
   public void enqueue(Object obj) throws Exception {
       if (isFull()) {
           throw new Exception("Queue is full. Dequeue some objects");
       }
System.out.print("\nEnqueue: "+(String)obj);
       this.data[index] = obj;
       this.index++;
   }

   public Object dequeue() throws Exception {
       if (isEmpty())
           throw new Exception("Queue is empty");
       Object obj = this.data[0];
       for (int i = 0; i < this.index - 1; i++) {
           data[i] = data[i + 1];
       }
System.out.print("\nDequeue: "+data[index]);
       this.index--;
       return obj;

   }
  
Object peek()
{
if(!isEmpty())
return this.data[0];
return null;
}
  
int getSize()
{
return this.index;
}
  
void print()
{
if(isEmpty())
{
System.out.println("\nEmpty Queue!");
return;
}
System.out.print("\nQueue Data: ");
for(int i=0; i<index; i++)
{
System.out.print(" "+this.data[i]);
}
}
  
}


class test
{
//main driver method
public static void main(String[] args) throws Exception
{
MyQueue queue = new MyQueue();
queue.enqueue("java");
System.out.println(queue.dequeue());

queue.enqueue("python");
queue.enqueue("xml");
queue.enqueue("css");
System.out.println(queue.dequeue());

queue.enqueue("lisp");
  
queue.print();
queue.dequeue();
queue.print();
queue.enqueue("jason");
queue.print();

}
}

Homework Answers

Answer #1

code 1

public class MyQueue { //create class MyQueue

public static final int DEFAULT_SIZE = 10; //declare a variable and fix its value to 10

private Object data[]; //create an object array with Object type

private int index; //integer variable declare

code 2

package test;

import java.util.*;

/* Class Node */

class Node

{

protected Object data; //declare variable data of type Object

protected Node link; //declare variable link of type Node class

/* Constructor */

public Node() //default constructor

{

link = null; //assigning values to variables

data = 0;

}

/* Constructor */

public Node(Object d,Node n) //parameterized constructor

{

data = d; //assigning values to variables

link = n;

}

/* Function to set link to next Node */

public void setLink(Node n) //mutator to set link

{

link = n;

}

/* Function to set data to current Node */

public void setData(int d) //mutator to set data

{

data = d;

}

/* Function to get link to next node */

public Node getLink() //Accessor to get link

{

return link;

}

/* Function to get data from current Node */

public Object getData() //Accessor to get data

{

return data;

}

}

/* Class linkedQueue */

class linkedQueue //create class

{

protected Node front, rear; //declare objects of class Node

public int size; //declare size variable

/* Constructor */

public linkedQueue() //default constructor

{

front = null; //initialize variables

rear = null;

size = 0;

}

/* Function to check if queue is empty */

public boolean isEmpty()

{

return front == null; //return either true or false

}

/* Function to get the size of the queue */

public int getSize()

{

return size; //return size

}

/* Function to insert an element to the queue */

public void insert(Object data)

{

Node nptr = new Node(data, null); //create object of class Node and passing values

if (rear == null) //check either its first insertion

{

front = nptr; //assign front and rear to first insertion

rear = nptr;

}

else //otherwise

{

rear.setLink(nptr); //insert at rear

rear = rear.getLink(); //get the link

}

size++ ; //increase the size of queue

}

/* Function to remove front element from the queue */

public Object remove()

{

if (isEmpty() ) //check queue is empty

throw new NoSuchElementException("Underflow Exception");

Node ptr = front; //assign front to ptr

front = ptr.getLink(); //get link of front

if (front == null) // if there is only one element to delete

rear = null; //make rear null

size-- ; //decrease size

return ptr.getData(); //return deleted data

}

/* Function to check the front element of the queue */

public Object peek()

{

if (isEmpty() ) //check queue is empty

throw new NoSuchElementException("Underflow Exception");

return front.getData(); //return front of queue

}

/* Function to display the status of the queue */

public void display()

{

System.out.print("\nQueue = "); //display queue

if (size == 0) //if size is zero

{

System.out.print("Empty\n"); //queue is empty

return ;

}

Node ptr = front; //assign front to ptr

while (ptr != rear.getLink() ) //while link is not null

{

System.out.print((String)ptr.getData()+" "); //display data

ptr = ptr.getLink(); //move to next element

}

System.out.println(); //create a line gap

}

}

/* Class LinkedQueueImplement */

public class LinkedQueueImplement //create a class

{

public static void main(String[] args) //main method

{

Scanner scan = new Scanner(System.in); //Scanner for taking input from user

/* Creating object of class linkedQueue */

linkedQueue lq = new linkedQueue(); // create object

/* Perform Queue Operations */

System.out.println("Linked Queue Test\n"); //display message

lq.insert("java"); //call insert method with string data

lq.insert("python");

lq.insert("html");

lq.insert("CSS");

System.out.println("Removed Element = "+ lq.remove()); //call remove element method from front

System.out.println("Peek Element = "+ lq.peek()); //display front element

System.out.println("Empty status = "+ lq.isEmpty()); //display queue is empty

System.out.println("Size = "+ lq.getSize()); //display size

/* display queue */

lq.display();

}

}

public MyQueue() { //constructor of MyQueue

data = new Object[DEFAULT_SIZE]; //assign value to data

}

public MyQueue(int size) { //parameterized constructor

data = new Object[size]; //assign value to data

}

public boolean isEmpty() { //check queue is empty

return index == 0; //return true or false

}

public boolean isFull() //check queue is full

{

if(index == data.length – 1) //if index is equal to length of data

return true; //return true

else

return false; //return false

}

public void enqueue(Object obj) throws Exception { //insert element in queue

if (isFull()) {

throw new Exception("Queue is full. Dequeue some objects"); //if full then throw exception

}

System.out.print("\nEnqueue: "+(String)obj); //display data

this.data[index] = obj; //assign value

this.index++; // increase index

}

public Object dequeue() throws Exception { //delete element from queue

if (isEmpty())

throw new Exception("Queue is empty"); //throw exception

Object obj = this.data[0]; //remove element from front

for (int i = 0; i < this.index - 1; i++) {

data[i] = data[i + 1]; //shifting elements a position ahead

}

System.out.print("\nDequeue: "+data[index]); //display deleted element

this.index--; //decrease size

return obj;

}

Object peek() //get front of queue

{

if(!isEmpty()) //check queue is not empty

return this.data[0]; //return front element

return null; //return link null

}

int getSize() //get size

{

return this.index; //return size

}

void print() //display queue

{

if(isEmpty()) //check is empty

{

System.out.println("\nEmpty Queue!"); //display queue is empty

return;

}

System.out.print("\nQueue Data: "); //display message

for(int i=0; i<index; i++) //loop to display values

{

System.out.print(" "+this.data[i]); //display data

}

}

}

class test //test class

{

//main driver method

public static void main(String[] args) throws Exception //main method which can throw exception

{

MyQueue queue = new MyQueue(); //create pbject

queue.enqueue("java"); //insert data

System.out.println(queue.dequeue()); //delete data and display it

queue.enqueue("python"); //insert data

queue.enqueue("xml");

queue.enqueue("css");

System.out.println(queue.dequeue()); //delete data and display it

queue.enqueue("lisp"); //insert data

queue.print(); //print queue

queue.dequeue(); //remove element from queue

queue.print(); //print queue

queue.enqueue("jason"); //insert queue

queue.print(); //print queue

}

}

Know the answer?
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
Similar Questions
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL;...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...