Question: /* Java program to implement basic stack operations */ class Stack { static final int MAX = 1000; int top; int a[] = new int[MAX];

/* Java program to implement basic stack operations */ class Stack { static final int MAX = 1000; int top; int a[] = new int[MAX]; // Maximum size of Stack boolean isEmpty() { return (top < 0); } Stack() { top = -1; } boolean put(int x) { if (top >= MAX) { System.out.println("Stack Overflow"); return false; } else { a[++top] = x; return true; } } int take() { if (top < 0) { System.out.println("Stack Underflow"); return 0; } else { int x = a[top--]; return x; } } } \*java file to implement bag*/ public class Bag { //Initialise instantiate hashmap to implement bag private HashMap map; public Bag(){ //Initialise the hasmap in the no-args constructor //this keyword refers to the current class we are in this.map = new HashMap(); } if(map.isEmpty()) { //Return null if it is return null; } /**** * Method to add item x of type T to Bag *****/ public void add(T x) { //Check if the map contains this item already if(map.containsKey(x)) { //If it does see how many times this element occurs int numElem = map.get(x); //Increment by one the number of times this element occurs //i.e. add another of these elements to the bag map.put(x, (numElem+1)); } else { //If the element does not exist create a new item in the map //with the number of items set to one map.put(x, 1); } } //MyClientTest1 import java.uti.*; public class MyClientTest1 { public static void main(String args[]) { Stack s = new Stack(); s.push(10); s.push(20); s.push(30); System.out.println(s.pop() + " Popped from stack"); Bag bagTest = new Bag(); bagTest.add("Hello"); bagTest.add("Hello"); bagTest.add("Hello"); } } /*A bag is a collection of objects, where you can keep adding objects to the bag, but you cannot remove them once added to the bag. So with a bag data structure, you can collect all the objects, and then iterate through them. ThereforeBag must be super class followed by Stack as a derived class while implementing inheritance*/ //MyClientTest2 public class MyClientTest1 { public static void main(String args[]) { Stack s = new Stack(); s.put(10); s.put(20); s.put(30); System.out.println(s.pop() + " Popped from stack"); Bag bagTest = new Bag(); bagTest.add("Hello"); bagTest.add("Hello"); bagTest.add("Hello"); } the above code for stack and bag is not working. i need to convert it into 2 different programs for stack and bag and make them execute with proper output.please help me to do this.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!