Help!! Objective: The students will learn how to work with queues. 1-Create a ne
ID: 3708778 • Letter: H
Question
Help!!
Objective: The students will learn how to work with queues.
1-Create a new Java Application that has the following methods:
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 search for a value in a queue. The queue should remain the same after the search is finished.
4- A method to exchange the first element and the last element in a queue
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 stack (the original queue should remain as is).
8-The main method that does:
a. Create 2 queues q1 and q2
b. Insert 23 integers between 20 and 60 ( do not insert duplicates) into q1
c. Insert 35 integers between 10 and 80 (do not insert duplicates) into q2.
d. Give the user the choice which methods (2-7) to call and option to exit
2- Write a java program to manage the queues at a bank.
1- Create a class "Person" defined by name, age, account number and transaction. The transaction can be "account" or "enquiry" or "others"
2- Create the Queue class to manage queues.
3- Create the Bank class that has three Queues namely elderQ, accountQ and otherQ.
4- Your program will read the details of Persons coming to the Bank from a file named "today.txt".
5- Your program will assign each person to one of the three queues.
6- People above the age of 50 are assigned to the elderQ, those with “account” transactions are assigned to the accountQ and “others” are sent to the otherQ.
7- Your program must display the status of all three Queues.
Explanation / Answer
import java.util.*;
/* Class arrayQueue */
class Queue
{
protected int Queue[] , Stack[];
protected int front, rear,size, top;
/* Constructor */
public Queue(int n)
{
size = n;
Queue = new int[size];
Stack = new int[size];
front = 0;
rear = -1;
top=0;
}
/* this function will insert a value into queue */
public void insert(int i)
{
Queue[++rear] = i;
}
/* Function to print all the elements in the queue */
public void printQueue()
{
System.out.println("Queue : ");
for (int i = front; i <= rear; i++)
{
System.out.print(Queue[i]+" ");
if(i==0 && i%10 == 0)
System.out.print(" ");
}
}
// this function will create a queue of specified length
public void createQueue(int n1, int n2)
{
Random r=new Random();
if(n2<n1)
{
swapTwoElements(n1,n2);
}
for(int i=0;i<size;i++)
{
int n= r.nextInt(n2-n1) + n1; // generates random number
insert(n);// insert into a queue
}
}
// this function will search an element from the queue
public boolean searchElement(int n)
{
boolean flag=false;
for (int i = front; i <= rear; i++)
{
if(Queue[i]==n)
flag=true;
}
return flag;
}
// swaps two elements
public void swapTwoElements(int n1,int n2)
{
int t=n1;
n1=n2;
n2=t;
}
public void swapFirstLastElement()
{
swapTwoElements(Queue[front],Queue[rear]);
}
// checks whether a queue contains another queue
public boolean containsQueue(Queue q)
{
return Arrays.asList(Queue).containsAll(Arrays.asList(q.Queue));
}
// inverse the queue
public void inverseQueue()
{
int i=0,j=0,m=rear/2;
for(i=front,j=rear ; i<=(rear/2) ;i++,j--)
{
int temp=Queue[i];
Queue[i] = Queue[j];
Queue[j]=temp;
}
}
// copies queue onto stack
public void copyToStack()
{
for(int i=front ;i<=rear;i++)
{
Stack[top]=Queue[i];
top++;
}
}
}
/* Class TestQueue */
public class TestQueue
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of Queue :");
Queue q = new Queue(scan.nextInt());
System.out.println("Please specify the range :");
int n1=scan.nextInt();
int n2 = scan.nextInt();
q.createQueue(n1,n2);
char c;
while(true)
{
System.out.println(" Operation lists");
System.out.println("1. Print Queue");
System.out.println("2. Search an element in queue");
System.out.println("3. Swap first and last element");
System.out.println("4. check subqueue");
System.out.println("5. Inverse a Queue");
System.out.println("6. Copy Queue to Stack");
int choice = scan.nextInt();
switch (choice)
{
case 1 : q.printQueue(); break;
case 2 : System.out.println("Enter an element to be search : ");
boolean flag = q.searchElement(scan.nextInt());
if(flag)
System.out.println("The element exist in queue");
else
System.out.println("The element does not exist in queue");
break;
case 3 : q.swapFirstLastElement(); break;
case 4 :
System.out.println("Enter no of element for subqueue :");
Queue q1 = new Queue(scan.nextInt());
System.out.println("Please specify the range :");
int n11=scan.nextInt();
int n21 = scan.nextInt();
q1.createQueue(n11,n21);
boolean cflag = q.containsQueue(q1);
if(cflag)
System.out.println("It contains another queue");
else
System.out.println("It does not contains another queue");
break;
case 5 : q.inverseQueue(); break;
case 6 : q.copyToStack(); break;
default : System.out.println("Wrong Entry ");
break;
}
System.out.println(" Do you want to continue (Type y or n) ");
c = scan.next().charAt(0);
if(c=='N' || c=='n')
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.