Question

Your assignment is to create and test a class for a queue of objects. You may...

Your assignment is to create and test a class for a queue of objects. You may use any object class of your choice as the data for the queue.   The instances of the class should have at least one field that distinguishes each instance from other instances of the class (key property, also called a key field).

You should complete the software to implement and test your queue and submit the software along with a project report.

Your queue class should maintain a head pointer to the first element in the queue, a tail pointer to the last elementin the queue, and the size of the queue as queue properties.  

We will add elements to the tail of the queue and remove elements from the head of the queue. The head pointer points to the spot from which elements are deleted from the queue, and the tail pointer points to the end of the queue where elements are added to the queue.

Note that elements move though the queue in the opposite direction of the pointers from one element to the next. Head points to the first element in the queue. Head.next points to the element that will become the head when the head is removed (de-queue method) – the pointers point back in the queue.

You should have methods to:

  • instantiate a null queue
  • enqueue – add an element to the queue
  • dequeue – remove an element from the queue
  • size – return the size of the queue
  • a boolean isEmpty method

You will need to create a class of nodes for the data in the queue, which we call a queueElement.

Each queueElement should have data and a pointer to the next queueElement.

We need:

  • A null constructor (default)
  • An initializing constructor (instantiates a queueElement from a data element)

and methods to :

  • set the data in a queue element
  • return the data in a queue element
  • set the pointer to the next element
  • return the pointer to the next element

This assignment requires four classes – Queue, QueueElement, the data object class, and a class for your test project.   Each class should be a separate file in your NetBeans project, all in the same source directory.

Please with explanation, and a clearly explanation/proper comments on when im switching classes, huge part of my grade thanks !!

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

ANSWER:

import java.util.*;

class QueueElement{
   int value;
   QueueElement next;
   public QueueElement(int n){
       value = n;
       next = null;
   }
   void setData(int n){
       value = n;
   }
   int getData(){
       return value;
   }
   void setNext(QueueElement temp){
       next = temp;
   }
   QueueElement getNext(){
       return next;
   }
}

class Queue{
   QueueElement front;
   QueueElement rear;
   public Queue(){
       front = rear = null;
   }
   void enqueue(int n){
       QueueElement temp = new QueueElement(n);
       // if Queue is Empty
       if (rear == null)
           front = rear = temp;
       else{
           rear.next = temp;
           rear = temp;
       }
       System.out.println(n+ " is enqueued into the QUEUE")
   }
   QueueElement dequeue(){
       // if Queue is Empty
       if (front == null)
           return null;
       QueueElement temp = front;
       front = front.next;

       if (front == null)
           rear = null;
       return temp;
   }
   int size(){
       QueueElement temp = front;
       int t_size = 0;
       while (temp != null){
           t_size += 1;
           temp = temp.next;
       }
       return t_size;
   }
   boolean isEmpty(){
       if (front == rear) return true;
       return false;
   }

}

class main{
   public static void main(String[] args){
       Queue que = new Queue();
       que.enqueue(12);
       que.enqueue(18);
       que.enqueue(24);
       System.out.println("Deque element is " +que.dequeue().getData());
       System.out.println("Deque element is " +que.dequeue().getData());
       que.enqueue(36);
       System.out.println("Deque element is " +que.dequeue().getData());  
   }
}

Add a comment
Know the answer?
Add Answer to:
Your assignment is to create and test a class for a queue of objects. You may...
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
  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two...

    Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two index variables to keep track of the start and the end of the queue. Consider the queue to be empty when the front/back index pointers are equal (which means that the 'back' pointer will always be pointing at the next place to add an item). Be careful of index increments and decrements; make sure you perform the operations necessary to make the pointers loop...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503....

    In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). Do NOT change your class name from "Main". The Main class should come first in your code. Your filename should be "Main.java". The Queue3503 class will contain:...

  • java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(),...

    java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(), dequeue(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods: A method to randomly generate a number of elements between two given values and save them in a queue A method to print a queue (10 elements per line). The original queue should remain as is after the print A method to return the number of elements on the...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

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