PART 1 -- Debugging in VSC

VSC includes a debugging facility far superior to System.out.println. This is well described in https://code.visualstudio.com/docs/editor/debugging and https://code.visualstudio.com/docs/java/java-debugging. (You already have installed the "debugger for java" extension). The former of these links is, I think, more useful. Realistically, both articles cover much more on debugging than you will need, the sections on "Debug actions" in the first article, and the sections on Breakpoints, Pause and Continue, Step In/Out/Over, Variables, and Call Stacks in the second are more than enough (for today). (When you are reading this page, keep in mind that you are using VSC in what the describe as "single file" rather than "in a project". So the section "Debugging single files" applies to you, even if you have more than one Java file in your current folder.) Skip the section on "debugging session inputs", we have discussed a way to handle this issue from within Java (ie, default values for command line inputs) this will be sufficient for this course. I have never used "Hot code replace"; you may find it useful.

Using the code from any previous assignment experiment with breakpoints and the other things described in this article. Be sure you understand how to set breakpoints (and why), how to view the value of variables, how to execute a program line by line, and how to resume execution.

I expect that you will never again use print statements for debugging. (This is a massive overstatement, I use both print statements and breakpoints; sometimes at the same time. )

Part 2 -- finish the stack

Start a new folder (lab6) and bring into it Stack151.java and ArrayStack.java. All of the code and data files are available from the class website: or via scp (for example)
        scp YOURUNIXLOGIN@goldengate.cs.brynmawr.edu:/home/gtowell/Public/151/Data/Lab6/Stack151.java Stack151.java
    

ArrayStack is incomplete (the methods peek, pop and push are only stubs. A stub method has a correctly first line -- eg "public int getInt() {" -- and a return value that is sufficiently correct to satisfy the compiler and that is all). VSC will often offer to write stub method with you say a class should implement an interface.) Write correct bodies for each of the stub method in the class.

Part 3 -- Breakpoints in Stacks

Set a breakpoint in you completed stack code so that on every pop, the code stops executing. What is the value of the local variables of the pop function and the instance variables of the ArrayStack class the second time the breakpoint is hit?

Part 2 -- Alternate

Here is the factorial code from class today
        public class Factorial {
            public static void main(String[] args) {
                String[] defaultArgs = { "5" };
                if (args.length == 0) 
                    args = defaultArgs;
                try {
                    int fb = Integer.parseInt(args[0]);
                    Factorial f = new Factorial();
                    int b = f.factorial(fb);
                    System.out.println(b);
                } catch (NumberFormatException nfe) {
                    System.err.println("<<" + args[0] + ">> must be an integer");
                }
                
            }
        
            /**
             * Compute factorial recursively
             * @param n the number whose factorial you want to compute
             * @return the factorial of the given number
             */
            public int factorial(int n) {
                if (n == 1)
                    return 1;
                if (n < 1)
                    return 0;
                int f = factorial(n - 1);
                return f*n;
            }
        }
        Get this into VSC (no need for comments). 
        With this code, set a breakpoint so you can see exactly what the value of f in the factorial function just before the return statement. DO not use a print statement. 
        
  1. How big a factorial can you compute before the numbers start getting weird.
  2. What is the value of f just before things get weird.

What to turn in

Send email to gtowell@brynmawr.edu with the answers to the question in part 3 about the values of variables. If you did not get to the question, send your versions, as far as you got, of peek, pop and push.