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 queue (without changing the queue).
A method to search for a value in a queue. The queue should remain the same after the search is finished.
A method to exchange the first element and the last element in a queue (a switch/swap).
A method to count the number of elements in the queue that are larger than a certain value. Queue should remain unchanged.
A method peekLowestElement() to return the smallest element. The original Queue should remain unchanged.
A method peekHighestElement() to return the largest element. The original Queue should remain unchanged.
A method to return an inverse copy a queue. The original Queue should remain unchanged.
Amethodtomakeandreturnacopyofaqueueintoastack(theoriginalqueueshouldremainasis).
In the main method test all your methods including the following cases:
- 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.
- Test all your methods using q1 and q2
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
public class Queue<T extends Comparable<T>> {
LinkedList<T> list = new LinkedList<T>();
void enqueue(T e) {
list.addLast(e);
}
T dequeue() {
return list.removeFirst();
}
T peek() {
return list.peekFirst();
}
boolean isEmpty() {
return list.isEmpty();
}
static int randomGen(int a, int b) {
Random r = new Random();
int size = b - a + 1;
return r.nextInt(size) + a;
}
int size() {
return list.size();
}
void print() {
for (int i = 0; i < list.size();
++i) {
System.out.print(list.get(i) + " ");
if (i != 0
&& i % 10 == 0)
System.out.println();
}
}
boolean search(T ele) {
for (int i = 0; i < list.size();
++i) {
if
(list.get(i) == ele)
return true;
}
return false;
}
void swap() {
if (!list.isEmpty()) {
T temp =
list.removeFirst();
list.addFirst(list.removeLast());
list.addLast(temp);
}
}
int count(T ele) {
int ans = 0;
for (int i = 0; i < list.size();
++i) {
if
(list.get(i).compareTo(ele) > 0)
++ans;
}
return ans;
}
public T peekLowestElement() {
if(list.isEmpty())
return
null;
T ans = list.get(0);
for (int i = 1; i < list.size();
++i) {
if
(list.get(i).compareTo(ans) < 0)
ans = list.get(i);
}
return ans;
}
public T peekHighestElement() {
if(list.isEmpty())
return
null;
T ans = list.get(0);
for (int i = 1; i < list.size();
++i) {
if
(list.get(i).compareTo(ans) > 0)
ans = list.get(i);
}
return ans;
}
public Queue<T> inverse() {
Queue<T> q = new
Queue<T>();
q.list = this.list;
Collections.reverse(q.list);
return q;
}
public String toString() {
return list.toString();
}
public static void main(String[] args) {
Queue<Integer> q1 = new
Queue<Integer>();
for(int i=0; i<23; ++i) {
q1.enqueue(randomGen(20,60));
}
Queue<Integer> q2 = new
Queue<Integer>();
for(int i=0; i<35; ++i) {
q2.enqueue(randomGen(10,80));
}
System.out.println(q1);
System.out.println(q1.dequeue());
System.out.println(q1.size());
System.out.println(q1.search(35));
System.out.println(q1.peekHighestElement());
System.out.println(q1.peekLowestElement());
System.out.println(q2);
}
}
================================================================
See Output
Thanks, PLEASE COMMENT
if there is any concern.
java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(),...
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....
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:...
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....
Create a new Java Application that test MyStack 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 stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...
Create a new Java Application that test MyStack 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 stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...
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:...
Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in Stack class and Queue interface. The Stack class is a generic class containing these methods: public void push(E value) - pushes a new value onto the Stack public E pop() - pops the next value off of the stack and returns it; throws EmptyStackExceptionl if stack is empty public E peek() - returns the next value on the Stack but does not pop it...
Queues This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program. Step 1 - Create a new project in Netbeans Use the following naming convention: “Module4_Lastname_Assignment1”. Step 2 - Build a solution You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new...
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...
On java create the following:
please solve it complete or don't since it considered as part of
problem 1 not even the whole question.
The main method that does:
Create 2 stacks s1 and s2
Insert 23 integers between 20 and 60 (do not insert duplicates)
into s1
Insert 35 integers between 10 and 80 (do not insert duplicates)
into s2.
Give the user the choice which methods (2-7) to call and option
to exit
LinkedList<Integer> LL = new LinkedList<Integer>)...