using:
class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public void enqueue(T data) { list.add(data); } public T dequeue() { return list.remove(0); } public T peek() { return list.get(0); } public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } } class MyQueueTest { public static void main(String[] args) { MyQueue<Integer> queue = new MyQueue<Integer>(); queue.enqueue(3); queue.enqueue(2); queue.enqueue(7); queue.enqueue(1); while (!queue.isEmpty()) { System.out.println(queue.dequeue()); } } }
please solve the following: either complete or don't solve it since it considered as one problem only/question only:
Problem 2: Create a new Java Application that test MyQueue by having the following methods in main class:
A method to generate a number of element 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 exchange the first element and the last element in a queue
A Boolean method to search for a value in a queue. The queue should remain the same after the search is
finished.
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.
A method to inverse a queue.
A method to make a copy of a queue into a queue (the original queue should remain as it is).
The main method that does
Create 2 queues Q1 and Q2
Insert 23 integers between 20 and 60 (do not insert duplicates) into Q1
Insert 35 integers between 10 and 80 (do not insert duplicates) into Q2.
Give the user the choice which methods (2-7) to call and option to exit
Code :-
Class MyQueueTest.java
import java.util.*;
import java.util.Random;
/*your package name here */
/*package mypack;*/
class MyQueueTest
{
private static Scanner sc =new Scanner
(System.in);
public static void main(String[] args)
{
MyQueue<Integer> q1 = new
MyQueue<Integer>();
MyQueue<Integer> q2 = new
MyQueue<Integer>();
//generating the queues
generate(q1,23,20,60);
generate(q2,35,10,80);
while (true)
{
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Enter an option :");
System.out.println("1.Print a queue");
System.out.println("2.Exchange first and last element in
queue");
System.out.println("3.Search value in queue");
System.out.println("4.Inverse a queue");
System.out.println("5.Check sub queue");
System.out.println("6.Copy queue into queue");
System.out.println("7.Exit");
System.out.print("\nYour Option : ");
int option =
sc.nextInt();
switch
(option)
{
case 1 :
{
//printing the queue
System.out.println ("Queue
1");
PrintQueue(q1);
break;
}
case 2 :
{
System.out.println ("Before
exchange");
PrintQueue(q1);
ExchangeFirstAndLast(q1);
//checking original
queue
System.out.println ("After
exchange");
PrintQueue(q1);
break;
}
case 3 :
{
System.out.println ("Queue
1");
PrintQueue(q1);
System.out.println("Enter a
value to check if it's there in the queue");
int var = sc.nextInt();
if
(CheckValueInQueue(q1,var)) System.out.println("Value is
present");
else
System.out.println("Value is not present");
//checking original
queue
System.out.println ("Queue
1");
PrintQueue(q1);
break;
}
case 4 :
{
System.out.println ("Before
Inverse");
PrintQueue(q1);
InverseQueue(q1);
System.out.println ("After
Inverse");
PrintQueue(q1);
break;
}
case 5 :
{
System.out.println ("Queue
1");
PrintQueue(q1);
MyQueue<Integer> q3 =
new MyQueue<Integer>();
//Accepting 3 values as part
of new queue
System.out.println("Enter
three inputs to the new queue");
q3.enqueue(sc.nextInt());
q3.enqueue(sc.nextInt());
q3.enqueue(sc.nextInt());
CheckSubQueue(q1,q3);
System.out.println ("Queue
1");
PrintQueue(q1);
System.out.println ("\nQueue
entered");
PrintQueue(q3);
break;
}
case 6 :
{
MyQueue<Integer> copy =
new MyQueue<Integer>();
CopyQueue(q1,copy);
System.out.println ("Queues
after copying ");
PrintQueue(q1);
PrintQueue(copy);
break;
}
case 7 : return;
default : System.out.println("Invalid
option");
}
}
}
//Q1
public static void generate(MyQueue<Integer> obj,int
number,int min_value,int max_value)
{
Random rand = new Random();
ArrayList<Integer> unique =
new ArrayList<Integer>();
int diff =
max_value-min_value;
int value = 0;
for (int i=0;i<number;i++)
{
value =
min_value + rand.nextInt(diff);
if
(unique.contains(value))
{
i--;
continue;
}
unique.add(value);
//Creating the
queue
obj.enqueue(value);
}
}
//Q2
public static void PrintQueue(MyQueue<Integer>
obj)
{
MyQueue<Integer> temp = new
MyQueue<Integer>();
int count = 0;
int data = 0;
while(!obj.isEmpty())
{
if (count%10==0)
System.out.println();
data =
obj.peek();
System.out.print(data+" ");
temp.enqueue(obj.dequeue());
count++;
}
while (!temp.isEmpty())
{
obj.enqueue(temp.dequeue());
}
System.out.println();
}
//Q3
public static void
ExchangeFirstAndLast(MyQueue<Integer> obj)
{
MyQueue<Integer> t2 = new
MyQueue<Integer>();
int first = obj.dequeue(),last =
0;
while (!obj.isEmpty())
{
last =
obj.peek();
t2.enqueue(obj.dequeue());
}
obj.enqueue(last);
while (!t2.isEmpty())
obj.enqueue(t2.dequeue());
obj.enqueue(first);
}
//Q4
public static boolean CheckValueInQueue
(MyQueue<Integer> obj,int value )
{
boolean flag = false;
int compare = 0;
MyQueue<Integer> copy = new
MyQueue<Integer> ();
CopyQueue(obj,copy);
while (!copy.isEmpty())
{
compare =
copy.dequeue();
if (compare ==
value)
{
flag = true;
break;
}
}
return flag;
}
//Q5
public static void
CheckSubQueue(MyQueue<Integer> obj1,MyQueue<Integer>
obj2)
{
MyQueue<Integer> t1 = new
MyQueue<Integer>();
MyQueue<Integer> t2 = new
MyQueue<Integer>();
int compare = 0;
CopyQueue(obj1,t1);
CopyQueue(obj2,t2);
while (!t1.isEmpty())
{
compare=t1.peek();
if (compare ==
t2.peek()) break;
t1.dequeue();
}
while (!t2.isEmpty())
{
if
(t1.peek()==t2.peek())
{
t1.dequeue();
t2.dequeue();
}
else
break;
}
if (t2.isEmpty())
System.out.println("Queue 2 is a sub-queue of Queue 1");
else System.out.println("Queue 2 is
not a sub-queue of Queue 1");
}
//Q6
private static void
InverseQueue(MyQueue<Integer> obj)
{ //Used inbuilt stack to inverse the queue
Stack<Integer> store = new
Stack<Integer>();
while(!obj.isEmpty())
store.push(obj.dequeue());
while(!store.isEmpty())
obj.enqueue(store.pop());
}
//Q7
public static void CopyQueue(MyQueue<Integer>
obj,MyQueue<Integer> copy)
{
MyQueue<Integer> temp = new
MyQueue<Integer>();
while (!obj.isEmpty())
temp.enqueue(obj.dequeue());
while (!temp.isEmpty())
{
copy.enqueue(temp.peek());
obj.enqueue(temp.dequeue());
}
}
}
Output :-
Questions 1,2 and 3 (generate to generate random values is executed in the main code )
Question 4
Question 5
Question 6
Question 7
using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public...
JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...
Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...
Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
use intellij idea main java wp the professor. Please make sure to only implement what is asked for. You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue...
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
i was able to make sense on the others but this two i need help Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters:...