Question

Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

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.

  1. 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”

  2. Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word. Example: If the user enters a sentence "Live and Let Live" the program will display "Live Let and Live"

  3. Write a method to randomly generate a number of elements between two given values and save them in a stack.

  4. Write a method to print a stack (10 elements per line). The original stack should remain as is after the print.

  5. Write a method to return the number of elements on the stack (without changing the stack).

  6. Write a method to search for a value in a stack. The stack should remain the same after the search is finished.

  7. Write a method to print the second element of the stack and leave the original stack unchanged

  8. Write a method to count the number of elements in a stack that are larger than a given value and leave the original stack unchanged.

  9. Write a method peekLowestElement() to return the smallest element. The original stack should remain unchanged.

10. Write a method peekHighestElement() to return the largest element. The original stack should remain unchanged.

11. Write a method to return the inverse a stack. The original stack should remain unchanged.
12. Write a method to make a copy of a stack into another stack and return it. The original stack should remain

unchanged.

In the main method test all your methods including the following cases:

  • Create 2 Stacks S1, S2

  • Insert 20 integers between 20 and 60 (do not insert duplicates) into S1

  • Insert 30 integers between 10 and 80 (do not insert duplicates) into S2.

  • Test all your methods using S1 and S2

I already wrote the code for the BOLD one I'm stuck in 9 , 10 ,11 ,12

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Main
{
   public int arr[];
   public int top;
   public int capacity;
   public int max;
   public int min;

   // Constructor to initialize stack
   Main(int size)
   {
       arr = new int[size];
       capacity = size;
       top = -1;
   }

   // Utility function to add an element x in the stack
   public void push(int x)
   {
       if (isFull())
       {
           System.out.println("OverFlow\nProgram Terminated\n");
           System.exit(1);
       }

   //   System.out.println("Inserting " + x);
       arr[++top] = x;
   }

   // Utility function to pop top element from the stack
   public int pop()
   {
       // check for stack underflow
       if (isEmpty())
       {
           System.out.println("UnderFlow\nProgram Terminated");
           System.exit(1);
       }

       System.out.println("Removing " + peek());

       // decrease stack size by 1 and (optionally) return the popped element
       return arr[top--];
   }

   // Utility function to return top element in a stack
   public int peek()
   {
       if (!isEmpty())
           return arr[top];
       else
           System.exit(1);

       return -1;
   }
  
   public int peekHighestElement(Main Object){ //This function checks for the highest element in the stack passed to it
   Object.max = check_element(Object.top);
   for(int i = 0;i<=Object.top;i++){
   if(Object.max < check_element(i)){
   Object.max = check_element(i);
   }
   }
   return Object.max;
   }
  
public int peekLowestElement(Main Object){ //This function checks for the lowest element in the stack passed to it
   Object.min = check_element(Object.top);
   for(int i = 0;i<=Object.top;i++){
   if(Object.min > check_element(i)){
   Object.min = check_element(i);
   }
   }
   return Object.min;
   }
  
public int check_element(int x){
if (!isEmpty())
           return arr[x];
       else
           System.exit(1);

       return -1;
}
  
   // Utility function to return the size of the stack
   public int size()
   {
       return top + 1;
   }

public Main stack_copy(Main Object){ //This function returns a copied stack of stack object passed to it
Main copied_stack = new Main(Object.capacity);
int array[] = new int [Object.capacity];
for(int i = 0;i<array.length;i++){
array[i]= check_element(i);
}
for(int i = 0;i<=array.length - 1;i++){
copied_stack.push(array[i]);
}
return copied_stack;
}
public Main stack_reverse(Main Object){ //This function returns a reverse stack of stack object passed to it
Main reverse_stack = new Main(Object.capacity);
int array[] = new int [Object.capacity];
for(int i = 0;i<array.length;i++){
array[i]= check_element(i);
}
for(int i = array.length-1;i>=0;i--){
reverse_stack.push(array[i]);
}
return reverse_stack;
}
  
public String printstack(Main object){ //this function prints the stack
for(int i=0;i<=object.top;i++){
System.out.print(object.arr[i]+" ");
  
}
  
return null;
}
  
   // Utility function to check if the stack is empty or not
   public Boolean isEmpty()
   {
       return top == -1;   // or return size() == 0;
   }

   // Utility function to check if the stack is full or not
   public Boolean isFull()
   {
       return top == capacity - 1;   // or return size() == capacity;
   }

   public static void main (String[] args)
   {
       Main stack = new Main(20);
Main stack1 = new Main(30);
for(int i = 20;i< 40;i++){
stack.push(i);
}
for(int i = 10;i< 40;i++){
stack1.push(i);
}
  
stack1.printstack(stack1);
System.out.println("");
stack.printstack(stack);
System.out.println("\nlowest element of stack :"+stack.peekLowestElement(stack)+" highest element of stack: "+stack.peekHighestElement(stack));
System.out.println("lowest element of stack1 :"+stack1.peekLowestElement(stack1)+" highest element of stack1: "+stack1.peekHighestElement(stack1));
System.out.println("reverse_stack:");
stack.printstack(stack.stack_reverse(stack));
System.out.println("\ncopied_stack:");
stack.printstack(stack.stack_copy(stack));
  
   }
}

I have copied and reversed the stack for you in the main function you can try it for stack1 too.

Also Since you have to make a Stack class please make sure to replace the Main with Stack everywhere in the code.

Add a comment
Know the answer?
Add Answer to:
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    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(),...

    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...

  • 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 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...

  • using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public...

    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:...

  • ) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and...

    ) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and clear. Complete the two unfinished methods. Do not modify any other parts of the class.               // Looks at the top two elements of the stack, and removes and returns the larger        // of the two elements from the stack, returning the other element to the stack.        // For example, if the stack, from the top, is 8 10 7 2...

  • Create an array-based implementation of a stack. Each element of the stack should store a string....

    Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a string),...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • On java create the following: please solve it complete or don't since it considered as part...

    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>)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT