HW3 Development Guide

 

You should use this guide to help you complete your assignment. You should follow it step by step, resolving errors before going on to the next step. Once you can follow this guide without errors, you should come up with more test cases and compare the output of your program with that of the solution.

    For all methods, we recommend that you include code to display the corresponding catastrophic error and debug messages before writing any other code in each empty method body. (Except for push, pop, and top, because you need to include the data that you are pushing/popping/topping to the stack). Once those messages are in place, you'll be ready to add the expected functionality for each method. In this assignment, you should use System.err.print to print and String.format to build these messages. You may also combine the two and use System.err.printf which will format and print.

    Before writing each method, refer to its Hints: section on the assignment page. Start your development process by folowing the getting started section at the bottom of the assignment page. In addition, if you want to run the executable at any point to check your output, run make driver to compile, and run your Java executable with ./Driver. If you want to run the public executable as reference, run ~/../public/hw3/java/driver.

  1. Write the constructor LongStackEngine in the file LongStack.java
    Once you've done the following, compile your program using make new:
    1) Add debug and catastrophic messages to all methods,
    2) Add "return false;" or "return null;" to all empty method bodies,
    3) Write the code for the LongStackEngine method to allocate and initialize a Stack Engine to serve as the Stack container.
    If you get any compiler errors or warnings, resolve those identified problems and compile again.

    To check that your LongStackEngine method works correctly, run your program through, JDB, the debugger:

    A Note On JDB

    In the JDB section we provided some expected output of JDB.
    However, the line numbers that you are seeing on your terminal versus what we listed in the development guide might be different, but it is okay because it is caused by the differences in the impelementations of the code.
    What you should check is you are stopping at the line with the corresponding functionality that is demonstrated in the development guide, so that you can carry out all the operations and get desired results.

    --- JDB Section ---

    To run JDB, the debugger for Java, please follow the following instructions.

    First open another terminal, (if you are on your local machine, don't forget to ssh into your cs12 account).
    Change directory into your hw3/java for both terminals.

    In your first terminal, run
    $ jdb Driver

    You should see something printed as \jdb -attach 80[xx] where the xx is some arbitrary number.
    Copy this line into your second terminal to run the debugger.

    To begin, set a breakpoint at the start of your Driver.main method. To do so, type this in your terminal,
    main[1] stop in Driver.main
    You should see this as the output in your after the above command.
    Deferring breakpoint Driver.main
    It will be set after the class is loaded.

    Now, execute your program until a stack is allocated. To do so, in the same screen type
    main[1] run

    Because we are creating an initial stack by default, you should hit a break point in Driver.java and see something like
    Breakpoint hit: "thread=main", Driver.main(), line=7 bci=0
    7      LongStack mainStack = new LongStack (FIRST_STACK_SIZE,

    Then type
    main[1] next
    You should see the previous line got executed, and it prints
    Step completed: "thread=main", Driver.main(), line=12 bci=19
    15      LongStack.debug_off(); // initialize debug states

    The above step should create a stack called mainStack in the main method of Driver.

    Now, examine the fields of mainStack by some print statements:
    main[1] print mainStack.stackEngine.stackID
    main[1] print mainStack.stackEngine.stackPointer
    main[1] print mainStack.stackEngine.stackSize
    main[1] print mainStack.stackEngine.stack
    Think about what you should be seeing and make sure eveything is as expected.

    Note that the stack instance variable should be an array of size 10.

    After you checked everything, to terminate the debugger, in the same terminal type
    main[1] quit

    Switch to the other screen, type Control-D to kill the program.

    --- End of JDB Section ---

    After you have confirmed these values to be the same, run the Driver executable with the debug flag on to verify the debug message is displayed as you expect. You should get the following results:

    $ ./Driver -x

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: a
    Please enter the number of objects to be able to store: 3
    [Stack 1 has been jettisoned]
    [Stack 1 has been allocated]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: ^D
    [Stack 1 has been jettisoned]

    No memory leaks! All memory has been correctly deallocated.

  2. Write the methods isEmptyStack, getCapacity, getOccupancy and isFullStack. Compile, test and run in the debugger to verify that these methods work as expected. Then run with the -x option to see the debug messages display as expected. Since you haven't written push, you can only test half of the functionalties of these methods. Make sure to test it again once you impelemented push.

  3. Write the method push, which will allow you to add items to the stack. Return true for a successful push, and false otherwise. Be sure to include the debug statement and error messages.

    To confirm that your push method works, run JDB. The following steps are to understand how the stack maintains elements and keeps track of the top of the stack. You should get the following results:

    --- JDB Section ---

    In your terminal, $ jdb Driver
    Open another terminal, copy the command and start the debugger like before.
    In this terminal set a break point in your push method by:
    main[1] stop in LongStack$LongStackEngine.push
    And then run the program:
    main[1] run

    In the other terminal, you should see and do:
    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 10

    Now go to the other terminal, and type next to step through your program line by line until you execute the line of code that pushes 10 to the stack.

    Now let's make sure the element is pushed to the stack by:

    main[1] print stack[0]
    stack[0] = 10
    main[1] cont
    Enter cont so that your program resumes execution until a breakpoint is hit again.

    Go back to the other terminal, you should see and do:

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 20

    Now go back to the debugger terminal, do the same thing until 20 is pushed into your stack, and examine the result by:
    main[1] print stack[1]
    stack[1] = 20
    This shows which element is at the top of the stack currently.
    If you would have printed the element at index 0 it should show you value 10.
    Try to understand why this is the case.


    Type this to terminate your debugging session:
    main[1] quit

    --- End of JDB Section ---

    If any of these values are incorrect, check to see if your stackPointer has the correct value that you want.

    Test the code with the debug flag as follows:

    $ ./Driver -x

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: a
    Please enter the number of objects to be able to store: 3
    [Stack 1 has been jettisoned]
    [Stack 1 has been allocated]

    Please enter choice: e
    Stack is empty.

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 1
    [Stack 1 - Pushing 1]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 2
    [Stack 1 - Pushing 2]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 3
    [Stack 1 - Pushing 3]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 4
    Pushing to a full stack!!!

    WARNING: push FAILED

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: ^D
    [Stack 1 has been jettisoned]

    No memory leaks! All memory has been correctly deallocated.

  4. Write the emptyStack method.
    Test with jdb as follows. Here you will be checking how the values in the stack change when you delete all elements that you previously entered in the stack using the empty command.

    --- JDB Section ---

    $ jdb Driver
    Open another terminal and start the debugger as before. Break the empty_Stack method by:
    main[1] stop in LongStack$LongStackEngine.emptyStack

    This will ensure jdb stops at the beginning of the empty_Stack method

    And then run the program:
    main[1] run

    Now go to the program terminal

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 10

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 20

    Now you should have two elements in the stack and the next step is to test program by running empty command

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: E

    Now go back to the debugger terminal, type: main[1] print stackPointer
    stackPointer = 1
    This is correct since you have pushed two elements into your stack

    main[1]next
    Enter next until you reach the end of your empty_Stack method

    Now let's print the value of the stackPointer:
    main[1] print stackPointer
    stackPointer = -1
    This also makes sense since now you have an empty stack with 0 elements in it (we initialize your empty stack to 0)

    Type quit to end the debugger session
    main[1] quit

    Note: If your second print statement does not print -1, you did not successfully empty the stack. Make sure you are resetting your stackPointer correctly in your emptyStack method.

    --- End of JDB Section ---

    Now you can test with the debug flag as follows:
  5. $ ./Driver -x

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: a
    Please enter the number of objects to be able to store: 5
    [Stack 1 has been jettisoned]
    [Stack 1 has been allocated]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 1
    [Stack 1 - Pushing 1]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 2
    [Stack 1 - Pushing 2]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 3
    [Stack 1 - Pushing 3]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 4
    [Stack 1 - Pushing 4]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: w

    The Stack contains:
    1 2 3 4
    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: E
    [Stack 1 - Emptied]
    Stack is empty.

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: o
    Number of elements on the stack is: 0

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: w

    The Stack contains:

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: ^D
    [Stack 1 has been jettisoned]

    No memory leaks! All memory has been correctly deallocated.

  6. Write the methods pop and top. These methods should be similar, except that pop removes an item, while top does not. Be sure to add debug statements and error messages for these methods.

    Now try running jdb on your own by breaking in your pop and top methods. Check to see that pop is changing the stackPointer and that top is not. Print the stack values at these indices to see if they point to the correct values. If you are having trouble with the commands, refer to the jdb commands listed in the previous steps! You should be checking for the values at top and how the variables of the stack change when you do top or pop and how they should differ from each other.

    After confirming this has the correct functionality, test as follows with the debug flag:

    $ ./Driver -x

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: a
    Please enter the number of objects to be able to store: 5
    [Stack 1 has been jettisoned]
    [Stack 1 has been allocated]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 1
    [Stack 1 - Pushing 1]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 2
    [Stack 1 - Pushing 2]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: u

    Please enter a number to push to stack: 3
    [Stack 1 - Pushing 3]

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: w

    The Stack contains:
    1 2 3
    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: t
    [Stack 1 - Topping 3]
    Number at top of the stack is: 3

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: p
    [Stack 1 - Popping 3]
    Number popped from the stack is: 3

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: t
    [Stack 1 - Topping 2]
    Number at top of the stack is: 2

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: p
    [Stack 1 - Popping 2]
    Number popped from the stack is: 2

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: t
    [Stack 1 - Topping 1]
    Number at top of the stack is: 1

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: p
    [Stack 1 - Popping 1]
    Number popped from the stack is: 1

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: t
    Topping from an empty stack!!!

    WARNING: top FAILED

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: p
    Popping from an empty stack!!!

    WARNING: pop FAILED

    Please enter a command:
        (a)llocate, (c)apacity, (C)heck memory, is(e)mpty, (E)mpty, is(f)ull, (j)ettison,
        (o)ccupancy, (p)op, (t)op, p(u)sh, (w)rite, to System.out, (W)rite to System.err.
    Please enter choice: ^D
    [Stack 1 has been jettisoned]

    No memory leaks! All memory has been correctly deallocated.

  7. At this point, you should test your assignment extensively by coming up with your own test cases. Be sure to test it with and without the "-x" option. To verify that your program behaves correctly, check the output against the solution's output. To run the solution program, type
  8. $ ~/../public/hw3/java/driver [-x]

  9. Make sure that you have fully commented your program and added method and file headers. Make sure your style follows the Style Outline for CSE 12. Then turn in your program by following The Turnin Procedure.

    Note that, for your homework to be collected correctly, you must name your files LongStack.java and Driver.java and the files must be located in a folder called hw3 in your home directory.