Stacks

Hello,

I’m currently taking a data structures class in Java and I’m currently really confused on where my code is going wrong. I tried reading the lecture slides, watching the lecture video, watching YouTube videos, reading the textbook, and asking my classmates for help. Currently this is my code:

import java.util.Stack;

public class ArrayStack2Lab implements StackInterface {

private T[] stack;
private int topIndex;
private static final int DEFAULT_INITIAL_CAPACITY = 100;
private static int scores;

public static void main(String[] args) {
    Stack<Integer> scores = new Stack<Integer>();
    
    scores.add(12);
    scores.add(15);
    scores.add(23);
    scores.add(95);
    
}

public static void display() {
    if (scores.isEmpty()){
        System.out.println("The stack is empty.");
    } else {
        System.out.println(scores);
    }
}

public int remove(int n) {
    int scores.pop();
}

}

And the instructions are asking us to:

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.

As well as:

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.

I tried reading the lecture slides, watching the lecture video, watching YouTube videos, reading the textbook, and asking my classmates for help.

Before all, did you use a debugger? And also throw in office-hours/tutoring if you haven’t tried those.

Also technically we’re not allowed to help for these sorts of problems here. As in, you dump the code and ask what’s wrong. However, if you re-phrase your question like, if I do this, why does y happen in java… or my idea is that a stack should do x, but I notice it’s doing y, why? Etc. … if you re-phrase it this way we can help you.

The process is more important than the immediate goal here.

1 Like