Question

In: Computer Science

Problem 1: based on java.util.LinkedList class, create MyQueue class that should have the methods:enqueue(), dequeue(), peek(),...

Problem 1: based on java.util.LinkedList class, create MyQueue class that should have the methods:enqueue(), dequeue(), peek(), size(), isEmpty().

Problem 2: Create a new Java Application that test MyQueue by having the following methods in main class:

  1. A method to generate a number of element between two given values and save them in a queue

  2. A method to print a queue (10 elements per line). The original queue should remain as is after the print

  3. A method to exchange the first element and the last element in a queue

  4. A Boolean method to search for a value in a queue. The queue should remain the same after the search is

    finished.

  5. A method to check if a queue is included in another queue (q1 is included in q2 if q1 is a sub-queue of q2). q1

    and q2 should remain as they were originally.

  6. A method to inverse a queue.

  7. A method to make a copy of a queue into a queue (the original queue should remain as it is).

  8. The main method that does

    1. Create 2 queues Q1 and Q2

    2. Insert 23 integers between 20 and 60 (do not insert duplicates) into Q1

    3. Insert 35 integers between 10 and 80 (do not insert duplicates) into Q2.

    4. Give the user the choice which methods (2-7) to call and option to exit

Solutions

Expert Solution

A queue is a FIFO( First In First Out) data structure.

Java provides a Queue Interface.

OUTPUT:

Here is the required java code.

import java.util.*;

class QueueDemo
{
   // Queue implementation in java
   public static void main(String[] args)
   {
       Queue<String> queue = new LinkedList<String>();

       queue.add("A");   // Insert "A" in the queue
       queue.add("B");   // Insert "B" in the queue
       queue.add("C");   // Insert "C" in the queue
       queue.add("D");   // Insert "D" in the queue

       // Prints the front of the queue ("A")
       System.out.println("Front element is: " + queue.peek());

       queue.remove();   // removing the front element ("A")
       queue.remove();   // removing the front element ("B")

       // Prints the front of the queue ("C")
       System.out.println("Front element is: " + queue.peek());

       // Returns the number of elements present in the queue
       System.out.println("Queue size is " + queue.size());

       // check if queue is empty
       if (queue.isEmpty())
           System.out.println("Queue is Empty");
       else
           System.out.println("Queue is not Empty");
   }
}

I hope it was helpful.


Related Solutions

Create a Deque class based on the following description of deque (double-ended queues). A dequeue is...
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is a double-ended queue. You can insert items at either end and delete them from either end. Therefore, the deque offers two insert operations and two remove operations: insertLeft() and insertRight() removeLeft() and removeRight(). Deques may also have "peek" methods ( peekLeft() and peekRight() )that let you see the left or right item in the deque without remove the item from the deque. For this...
1. Create a1. Create a new java file named Name.java that should have Name class based...
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML Name - first: String - last: String + Name () + Name (String firstName, String lastName) + getName () : String + setName (String firstName, String lastName) : void + getFirst () : String + setFirst (String firstName) : void + getLast () : String + setLast (String...
1. Based on the hormones we have discussed in class, create a list of hormones that...
1. Based on the hormones we have discussed in class, create a list of hormones that play a role in the following areas: increased CHO utilization, increased PRO synthesis, increased lipid breakdown/FFA utilization. 2. Explain the two ways in which plasma volume and osmolality are regulated during exercise. Be sure to define all key terms.
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on Program 13-1 (see attachment Pr13-1.cpp) in the textbook, create a class name “Circle” with the following declarations (hint: you can use PI=3.14): //Circle class declaration class Circle { private: double radius; public: void setRadius(double); double getRadius() const; double getArea() const; double getPerimeter() const; }; 2: Based on Program 13-2 (see attachment Pr13-2.cpp) in the textbook, create two instances of the Circle class, pizza1 and...
Create a file called grocery.ts. It should have a definition of a class with the obvious...
Create a file called grocery.ts. It should have a definition of a class with the obvious name Grocery. The class should have some basic attributes such as name, quantity, etc. Feel free to add any other attributes you think will be necessary. Add few grocery items to an array of groceries, such as milk, bread, and eggs, along with some quantities (i.e. 3, 6, 11). Display these grocery items as HTML output. The output of this assignment will be grocery.ts...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
You are to create a class called Person. You should create the definition of the class...
You are to create a class called Person. You should create the definition of the class Person in a file person.h and the functions for Person in person.cpp. You will also create a main program to be housed in personmain.cpp. A Person will have attributes of Height (in inches) Weight (in pounds to 2 decimal places) Name (string) Gender (‘m’ or ‘f’) Ethnicity/Race (could be more than one word) Occupation (also more than a single word) A Person will have...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
(Java Problem) Create a Produce class that have an instance variable of type String for the...
(Java Problem) Create a Produce class that have an instance variable of type String for the name, appropriate constructors, appropriate accessor and mutator methods, and a public toString() method. Then create a Fruit and a Vegetable class that are derived from Produce. These classes should have constructors that take the name as a String, the price (this is the price per box) as a double, the quantity as an integer, and invoke the appropriate constructor from the base class to...
for java Welcome to a classic homework problem! Create a public class called Last8. You should...
for java Welcome to a classic homework problem! Create a public class called Last8. You should exposed two public methods: add: adds a value, does not return a value last: returns an array containing the last 8 values that were added, in any order. You do not need a constructor, but you can add an empty one if you need. Until 8 values have been added you should return 0s in their place. For example, here's how an instance of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT