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

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

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

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

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
ADVERTISEMENT