Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//An ArrayBased Implementation of the ADT Stack public class StackArrayBased imp

ID: 3756991 • Letter: #

Question

//An ArrayBased Implementation of the ADT Stack

public class StackArrayBased implements StackInterface{

final int MAX_STACK = 50;

private Object items[];

private int top;

public StackArrayBased(){

items = new Object[MAX_STACK];

top = -1;

}

public boolean isEmpty(){

return top < 0;

}

public boolean isFull(){

return top == MAX_STACK-1;

}

public void push(Object newItem) throws StackException{

if(!isFull()){

items[++top] = newItem;

}

else{

throws new StackException("StackException on " + "push: stack full");

}

}

public void popAll(){

items = new Object[MAX_STACK];

top = -1;

}

public Object pop() throws StackException{

if(isEmpty()){

return items[top--];

}

else{

throws new StackException("StackException on " + "pop: stack empty");

}

}

public Object peek() throws StackException{

if(!isEmpty()){

return items[top];

}

else{

throws new StackException("Stack exception on " + "peek - stack empty");

}

}

public static final int MAX_ITEMS = 15;

public static void main(String[] args){

StackArrayBased stack = new StackArrayBased();

Integer items[] = new Integer[MAX_ITEMS];

for(int i=0; i<MAX_ITEMS;i++){

items[i] = new Integer(i);

if(!stack.isFull()){

stack.push(items[i]);

}

}

while(!stack.isEmpty()){

// need a to finish this line

//thnks

System.out.println((Integer)(stack.pop()));

}

}

  

}

need to finish the while loop;; thks

Explanation / Answer


Given below is the fixed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

//An ArrayBased Implementation of the ADT Stack
public class StackArrayBased implements StackInterface{
final int MAX_STACK = 50;
private Object items[];
private int top;
public StackArrayBased(){
items = new Object[MAX_STACK];
top = -1;
}
public boolean isEmpty(){
return top < 0;
}
public boolean isFull(){
return top == MAX_STACK-1;
}
public void push(Object newItem) throws StackException{
if(!isFull()){
items[++top] = newItem;
}
else{
throw new StackException("StackException on " + "push: stack full");
}
}
public void popAll(){
items = new Object[MAX_STACK];
top = -1;
}
public Object pop() throws StackException{
if(!isEmpty()){
return items[top--];
}
else{
throw new StackException("StackException on " + "pop: stack empty");
}
}
public Object peek() throws StackException{
if(!isEmpty()){
return items[top];
}
else{
throw new StackException("Stack exception on " + "peek - stack empty");
}
}
public static final int MAX_ITEMS = 15;
public static void main(String[] args){
StackArrayBased stack = new StackArrayBased();
Integer items[] = new Integer[MAX_ITEMS];
try{
for(int i=0; i<MAX_ITEMS;i++){
items[i] = new Integer(i);
if(!stack.isFull()){
System.out.println("Pushing " + items[i]);
stack.push(items[i]);
}
}
}
catch(StackException e){
System.out.println(e.getMessage());
}

System.out.println("=================");
try{
while(!stack.isEmpty()){
System.out.println("Popped " + (Integer)(stack.pop()));
}
}catch(StackException e){
System.out.println(e.getMessage());
}
}


}
===========
output
Pushing 0
Pushing 1
Pushing 2
Pushing 3
Pushing 4
Pushing 5
Pushing 6
Pushing 7
Pushing 8
Pushing 9
Pushing 10
Pushing 11
Pushing 12
Pushing 13
Pushing 14
=================
Popped 14
Popped 13
Popped 12
Popped 11
Popped 10
Popped 9
Popped 8
Popped 7
Popped 6
Popped 5
Popped 4
Popped 3
Popped 2
Popped 1
Popped 0