Question

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

MyStack class:

public class MyStack {

LinkedList<Integer> list = new LinkedList<Integer>();

public boolean isEmpty() {
return list.isEmpty();
}

public void push(Integer o) {
list.add(0, o);

}

public Integer pop() {
return list.remove(0);
}

public Integer peek() {
return list.get(0);

}

public int size() {
return list.size();
}
}

use java language in netbeans

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

MyStack.java

import java.util.*;
public class MyStack {
   private LinkedList<Integer> list = new LinkedList<Integer>();
   public boolean isEmpty()
   {
       return list.isEmpty();
   }

   public void push(Integer o)
   {
       list.add(0, o);
   }

   public Integer pop()
   {
       return list.remove(0);
   }
   public Integer peek()
   {
       return list.get(0);
   }

   public int size()
   {
       return list.size();
   }
}

TestMyStack.java

import java.util.*;
import java.util.Random;
/*your package name here */
/*package mypack;*/
class TestMyStack
{
   private static Scanner sc =new Scanner (System.in);
   public static void main (String[] args)
   {
       MyStack s1 = new MyStack();
       MyStack s2 = new MyStack();
       generate(s1,23,20,60);
       //Have used only stack s1 in the following procedures
       //s2 can be used instead
       generate(s2,35,10,80);
       while (true)
       {
           System.out.println("--------------------------------------------------------------------------------");
           System.out.println("Enter an option :");
           System.out.println("1.Print a stack");
           System.out.println("2.Exchange first and last element in stack");
           System.out.println("3.Search value in stack");
           System.out.println("4.Inverse a stack");
           System.out.println("5.Check sub stack");
           System.out.println("6.Copy stack into stack");
           System.out.println("7.Exit");
           System.out.print("\nYour Option : ");
           int option = sc.nextInt();

           switch (option)
           {
               case 1 :
               {
                   PrintStack(s1);
                   break;
               }
               case 2 :
               {
                   PrintStack(s1);
                   ExchangeFirstAndLast(s1);
                   PrintStack(s1);
                   break;
               }
               case 3 :
               {
                   PrintStack(s1);
                   System.out.println("Enter a value to check if it's there in the stack");
                   int var = sc.nextInt();
                   if (CheckValueInStack(s1,var)) System.out.println("Value is present");
                   else System.out.println("Value is not present");
                   break;
               }
               case 4 :
               {
                   PrintStack(s1);
                   InverseStack(s1);
                   PrintStack(s1);
                   break;
               }
               case 5 :
               {
                   PrintStack(s1);
                   MyStack s3 = new MyStack();
                   //Accepting 3 values as part of new Stack
                   s3.push(sc.nextInt());
                   s3.push(sc.nextInt());
                   s3.push(sc.nextInt());
                   CheckSubStack(s1,s3);
                   break;
               }
               case 6 :
               {
                   MyStack copy = new MyStack();
                   CopyStack(s1,copy);
                   PrintStack(s1);
                   PrintStack(copy);
                   break;
               }
               case 7 : return;
               default : System.out.println("Invalid option");
           }
       }
          
   }
   public static void generate(MyStack 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);
           obj.push(value);
       }
   }
   public static void PrintStack(MyStack obj)
   {
       int data;
       MyStack temp = new MyStack();
       int count = 0;
       while(!obj.isEmpty())
       {
           if (count%10==0) System.out.println();
           data = obj.peek();
           System.out.print(data+" ");
           temp.push(obj.pop());
           count++;
       }
       while (!temp.isEmpty())
       {
           obj.push(temp.pop());
       }
       System.out.println();
   }
   private static void InverseStack(MyStack obj)
   {
       MyStack t1 = new MyStack();
       MyStack t2 = new MyStack();
       while (!obj.isEmpty()) t1.push(obj.pop());
       while (!t1.isEmpty()) t2.push(t1.pop());
       while (!t2.isEmpty()) obj.push(t2.pop());
   }
   public static void ExchangeFirstAndLast(MyStack obj)
   {
       MyStack t2 = new MyStack();
       int first = obj.pop();
       while (!obj.isEmpty()) t2.push(obj.pop());
       obj.push(first);
       first = t2.pop();
       while (!t2.isEmpty()) obj.push(t2.pop());
       obj.push(first);
   }
   public static boolean CheckValueInStack (MyStack obj,int value )
   {
       boolean flag = false;
       int compare = 0;
       MyStack copy = new MyStack();
       CopyStack(obj,copy);
       while (!copy.isEmpty())
       {
           compare = copy.pop();
           if (compare == value)
           {
               flag = true;
               break;
           }
       }
       return flag;
   }
   public static void CopyStack(MyStack obj,MyStack copy)
   {
       MyStack temp = new MyStack();
       while (!obj.isEmpty()) temp.push(obj.pop());
       while (!temp.isEmpty())
       {
           copy.push(temp.peek());
           obj.push(temp.pop());
       }
   }
   public static void CheckSubStack(MyStack obj1,MyStack obj2)
   {
       MyStack t1 = new MyStack();
       MyStack t2 = new MyStack();
       int compare = 0;
       CopyStack(obj1,t1);
       CopyStack(obj2,t2);
       while (!t1.isEmpty())
       {
           compare=t1.peek();
           if (compare == t2.peek()) break;
           t1.pop();
       }
       while (!t2.isEmpty())
       {
           if (t1.peek()==t2.peek())
           {
               t1.pop();
               t2.pop();
           }
           else break;
       }
       if (t2.isEmpty()) System.out.println("Stack 2 is a sub-stack of Stack 1");
       else System.out.println("Stack 2 is not a sub-stack of Stack 1");
   }
}

Output :-

Question 1 and 2

Question 3

Question 4

Question 5

Question 6

Question 7

Add a comment
Know the answer?
Add Answer to:
Create a new Java Application that test MyStack by having the following methods in main class:...
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 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:...

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

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

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

  • 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 java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • I was told I need three seperate files for these classes is there anyway to tie...

    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: ");...

  • Create a MyQueue class in Java, similar to what we did when we created the MyStack...

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

  • Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in...

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

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