Implement in java an infinite StackOfObjects class: the elements in the stack ar
ID: 3805220 • Letter: I
Question
Implement in java an infinite StackOfObjects class: the elements in the stack are stored in an array Object[] named elements. When you create a stack, the array is also created. The no-arg constructor creates an array with the default capacity of 2. The variable size counts the number of elements in the stack, and size - 1 is the index of the element at the top of the stack. The size should be increased if there is a need of more space in the stack.
Implement a push() method to push an object into the top of the stack and a pop method to return and remove the top element from the stack.
Explanation / Answer
Find the answer for the above question asked:
Step 01:
Create an interface of stackArray<Item>
**************
package com.sql.acadgild;
public interface StackArray<Item> {
Item getTop(); // return the top item without removing it from stack
Item pop(); // return the top item and removes it from stack
void push(Item itm); // adds an item to the stack
boolean isEmpty(); // returns true if stack is empty, false otherwise
int size(); // returns the number of items in stack right now
}
*******************
Step 02: Create a class to implement that interface with required method implementation
****************
package com.sql.acadgild;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class StackOfObjects<Item> implements StackArray<Item> {
private Item container[];//Stack of objects stored in the elements
private int top;
private final static int DEFAULT_SIZE = 2;
//Initlizing the default size of array capacity is 02
public StackOfObjects ()
{
this(DEFAULT_SIZE);
}
//Below are the methods of push and pop methods
public StackOfObjects (int initSize)
{
container = (Item[]) new Object [initSize];
top = -1;
}
public Item getTop()
{
if (top == -1)
return null;
return container[top];
}
public boolean isEmpty()
{
return (top == -1);
}
public Item pop()
{
if (top == -1)
return null;
Item itm = container[top];
container[top--] = null;
if(top > 0 && top == container.length / 4)
resize (container.length/2);
return itm;
}
public void push(Item itm)
{
if (top == container.length - 1)
resize(2 * container.length);
container[++top] = itm;
}
public int size()
{
return (top + 1);
}
private void resize (int newSize)
{
Item t[] = (Item[]) new Object[newSize];
for (int i = 0; i <= top; i++)
t[i] = container[i];
container = t;
}
public Iterator<Item> iterator()
{
return new ArrayStackIterator();
}
private class ArrayStackIterator implements Iterator <Item>
{
private int i = top;
public boolean hasNext()
{
return (i > -1);
}
public Item next()
{
return container[i--];
}
public void remove()
{
// not needed
}
}
}
*****************
Step 03: Driver Class to test the functinallity
****************
package com.sql.acadgild;
import java.util.Iterator;
public class StackArrayDriver {
public static void main(String a[]) {
// TODO Auto-generated method stub
StackArray<Integer> s = new StackOfObjects<Integer>();
s.push(10);
s.push(20);
System.out.println("Size of the stack: " + s.size());
// iterate through stack
System.out.println("Following items pushed to Stack as of now:");
for (Integer i : s)
System.out.println(i);
}
}
}
***********
Output:
Size of the stack: 2
Following items pushed to stack as of now:
20
10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.