Need help with this Java problems. Java files are attached. Add your code to the file ArrayStack2Lab.java. Add your tests to the main() method. Submit ArrayStack2Lab.java. Problem 1 Implement the method public void display() which displays the entries in a stack starting from the top. If the stack is empty, print “The stack is empty”. Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java. Problem 2 Implement the method public int remove(int n) The method removes the n top most entries for a stack . If the stack contains less than n items, the stack becomes empty. The method returns the number of items removed. Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java.
Need help with this Java problems. Java files are attached.
Add your code to the file ArrayStack2Lab.java.
Add your tests to the main() method.
Submit ArrayStack2Lab.java.
Problem 1
Implement the method
public void display()
which displays the entries in a stack starting from the top. If the stack is empty, print “The stack
is empty”.
Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java.
Problem 2
Implement the method
public int remove(int n)
The method removes the n top most entries for a stack . If the stack contains less than n items,
the stack becomes empty. The method returns the number of items removed.
Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java.
![Main.java :
1 /**
2
3
4
5 */
6- import java.util.Arrays;
7 public class ArrayStack2Lab<T> implements StackInterface<T>
8. {
9. public static void main(String[] args) {
// Add you lab tests here
10
11
12
13
14
15-
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 34 35 36 37 38 39 40 41 42 43 0
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
A class of stacks whose entries are stored in an array.
@author Frank M. Carrano
@version 3.0
79
80
81
82
}
// Problem 1
public void display() {
}
// Problem 2
public int remove(int n) {
}
private T[] stack;
// array of stack entries
private int topIndex; // index of top entry
private static final int DEFAULT_INITIAL_CAPACITY = 50;
public ArrayStack2Lab()
{
44 public
45
{
46
47
48
49
50
51
52
53
54
55
56
this (DEFAULT_INITIAL_CAPACITY);
} // end default constructor
public
{
ArrayStack2Lab(int initialCapacity)
// the cast is safe because the new array contains null entries
@SuppressWarnings
("unchecked")
T[] tempStack = (T[])new Object[initial Capacity];
stack = tempStack;
topIndex = -1;
} // end constructor
void push(T newEntry)
ensureCapacity();
top Index++;
stack [topIndex] = newEntry;
} // end push
private void ensureCapacity()
{
if (topIndex == stack.length - 1) // if array is full, double size
stack = Arrays.copyof(stack, 2*stack.length);
} // end ensureCapacity
public T peek()
{
T top = null;
if (isEmpty())
top = tack[topIndex];
return top;
} // end peek
public T pop()
{
T top = null;
if (!isEmpty()) {
top = stack[topIndex];
stack [topIndex] = null;
top Index--;
} // end if
return top;
} // end pop
public boolean isEmpty()
{
return topIndex < 0;
} // end is Empty
public void clear()
{
83
84
85
86
87
}
88
89 } // end ArrayStack2Lab
90
for(int i = 0; i
stack[i] = null;
topIndex = -1;
<=
topIndex; ++i)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fbc0ef4be-b393-4fab-a253-cbedc48af370%2Fcba019a6-56fd-4c24-b06d-3ad306dd4728%2Fnxxnr4t_processed.jpeg&w=3840&q=75)


Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









