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 a stack. The stack should remain the same after the search is finished.
5. A method to check if a stack is included in another stack (q1 is included in q2 if q1 is a sub-stack of q2). q1 and q2 should remain as they were originally
. 6. A method to inverse a stack.
7. A method to make a copy of a stack into a stack (the original stack should remain as it is).
8. The main method that does a.
Create 2 stacks s1 and s2 b. Insert 23 integers between 20 and 60 (do not insert duplicates) into s1 c. Insert 35 integers between 10 and 80 (do not insert duplicates) into s2. d. Give the user the choice which methods (2-7) to call and option to exit
1. For Generating random numbers and inserting into stacks:
public class Main {
final Random random = new Random();
final Set<Integer> stackSet1 = new HashSet<>(); //for stack1
final Set<Integer> stackSet2 = new HashSet<>(); // for stack2
final int[] numbers1 = new int[stackSet1.size()];
final int[] numbers2 = new int[stackSet2.size()];
public void Randomnumbers1 (){
while (stackSet1.size() < 24) { //for stack 1 as it contains only 23 elements.
stackSet1.add(random.nextInt(60-20) + 20); // range for stack 1 is given between 20 to 60.
}
public void Randomnumbers2 (){
while (stackSet2.size() < 36) { //for stack 2 as it contains only 35 elements.
stackSet2.add(random.nextInt(80-10) + 10); // range for stack 1 is given between 10 to 80.
}
final Iterator<Integer> iter1 = stackSet1.iterator();
for (int i = 0; iter1.hasNext(); ++i) {
numbers1[i] = iter1.next();
}
final Iterator<Integer> iter2 = stackSet2.iterator();
for (int i = 0; iter2.hasNext(); ++i) {
numbers2[i] = iter2.next();
}
System.out.println(Arrays.toString(numbers1)); //elements of stack 1
System.out.println(Arrays.toString(numbers2)); // elements of stack 2
}
For inserting elements into stack:
follow the following snippet:
public void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 24; i++)
{
stack.push(numbers1);
}
for(int i = 0; i < 36; i++)
{
stack.push(numbers2);
}
}
2. For printing the stack
static void printStack()
{
if(stack1.size() > 0)
{
int x = stack1.peek(); //same for stack 2
stack1.pop();
printStack();
}
}
3. Method to exchange 1st and last element of stack: same can be done with second stack
public void exchange(){
Object[] arr = stackSet1.toArray();
int x = stack1[0];
stack1[0] = stack1[stack1.length-1];
stack1[stack1.length-1] = x;
System.out.println("after swaping of first and last elements: "+Arrays.toString(stack1));
}
4. search a value in stack
public boolean serach(){
if (stack1.contains(numbers1[i])){
return true;
}
else
return false;
}
5. Inverse of stack can be implemented using function defined in 2 step, just add this method to the method mentioned in second step.
public void insert_at_the_bottom_of_array(int x)
{
if(stack1.isEmpty())
stack1.push(numbers1[i]);
else
{
int a = stack1.peek();
stack1.pop();
insert_at_the_bottom_of_array(x)
stack1.push(a);
}
} // same for stack 2
Others methods can be implemented using simple logic.
Create a new Java Application that test MyStack by having the following methods in main class:...
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 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 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....
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...
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:...
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>)...
Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...
1) Write a segment of code (application level) to perform each of the following operations. Assume myStack is an object of the class ArrayStack. You may call any of the public methods of ArrayStack. You may declare additional stack objects. a) Set secondElement to the second element from the top of myStack, leaving myStack without its original top two elements. b) Set bottom equal to the bottom element in myStack, leaving myStack empty. c) Set bottom equal to the bottom...
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...
Create a MyQueue class in Java, similar to what we did when we created the MyStack class. Include a test class to show everything works correctly. Include all the methods mentioned in the power points that are normally included in a Queue. This is MyStack class we did in a class. Have to make Queue class. public class MyStack<E> { private Node start; private int currentCount; public MyStack() { start = null; currentCount = 0; } public void printStack()//available for...