Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STACK - java, plz use simple java language, netbeans 8.1 or 8.2 1. Create a Stac

ID: 3822771 • Letter: S

Question

STACK - java, plz use simple java language, netbeans 8.1 or 8.2

1. Create a Stack class based on MyLinkedList class. Use this class in the following:

2. Create a new Java Application that has the following methods:

1. A method to generate a number of elements 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 return the number of elements on the stack.

4. A method to return if the stack is empty or not.

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

6. A method to print the second element of the stack and leave the original stack unchanged

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

stack unchanged.

8. A method peekLowestElement to return the smallest element. The original stack should remain unchanged.

9. A method peekHighestElement to return the largest element. The original stack should remain unchanged.

10. A method to inverse a stack.

11. A method to make a copy of a stack into another stack

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

! Create 3 Stacks S1, S2, S3

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

Explanation / Answer

//Find code here for STACK
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class STACK{

   private List<Integer> stack = new LinkedList<>();
   private int size;
  
   //method to generate a number of elements between two given values and save them in a stack.
   public void createStack(int a,int b,int count){
       int size=0;
       Random rand=new Random();
   int item;
       for(int i=0;i<b;i++){
           item=a+rand.nextInt(b);
           if(item>=a && item<=b && !stack.contains(item) && size <count){
               stack.add(item);
               size++;
           }
          
       }
   }
  
   // method to print a stack (10 elements per line). The original stack should remain as is after the print.
   public void printStack(){
  
       for(int i=0;i<stack.size();i++){
          
           if(i%10 == 0){
               System.out.println();
           }
          
           System.out.print(stack.get(i)+" ");
       }
       System.out.println();
}
  
   //A method to return the number of elements on the stack.
   public int getStackSize(){
       size=stack.size();
       return size;
   }
  
   //A method to return if the stack is empty or not.
   public boolean isStackEmpty(){
      
       if(stack.size() == 0){
           return true;
       }else{
           return false;
       }
   }
  
   //method to search for a value in a stack. The stack should remain the same after the search is finished.
   public boolean searchElement(Integer item){
      
       if(stack.contains(item)){
           return true;
       }else{
           return false;
       }
   }
  
   //. A method to print the second element of the stack and leave the original stack unchanged
   public void printSecondElement(){
       if(!stack.isEmpty()){
           System.out.println(stack.get(1));
       }else{
           System.out.println("stack is empty");
       }
      
   }
   //A method to count the number of elements in a stack that are larger than a given value and leave the original
   public int countElementsGreaterThanNumber(Integer item){
       int count=0;
       for(int i=0;i<stack.size();i++){
           if(stack.get(i)>item){
               count++;
           }
       }
       System.out.println("size of stack is "+count );
      
       return count;
   }
  
   // A method peekLowestElement to return the smallest element. The original stack should remain unchanged.
   public int peekLowestElement(){
       List<Integer> list = stack;
       Collections.sort(list);
       return list.get(0);
   }
  
   //A method peekHighestElement to return the largest element. The original stack should remain unchanged.
   public int peekHighestElement(){
       List<Integer> list = stack;
       Collections.sort(list);
       return list.get(list.size()-1);
   }
  
   //A method to inverse a stack.
   public void inverseStack(){
      
       Collections.reverse(stack);
   }
  
   public List copyStack(){
      
       List stack=this.stack;
       return stack;
   }
  
}

//Find here TestSTACK
import java.util.Random;

public class TestSTACK {

   public static void main(String[] args) {
       Random rand=new Random();
       STACK s1 = new STACK();
       STACK s2 = new STACK();
       STACK s3 = new STACK();
      
       System.out.println("Stack s1 output");
       //creating stack of 20 items between 20 and 60. All are unique numbers
       s1.createStack(20, 60, 20);
       s1.printStack();
      
      
       System.out.println("Stack s2 output");
       //creating stack of 30 items between 10 and 80. All are unique numbers
       s2.createStack(10, 80, 30);
       s2.printStack();
   }
}

//Find here for Output

Stack s1 output

47 26 58 31 55 56 33 45 32 49
51 39 60 30 21 53 42 44 25 52
Stack s2 output

26 28 44 29 10 25 23 71 33 51
72 48 62 39 24 43 22 45 69 16
78 40 36 35 37 53 42 13 30 49