Assignment FAQ Development Guide Code Checklist |
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
.
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.
$ ./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.
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
$ ./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.
$ 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
$ ./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.
$ ./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.
$ ~/../public/hw3/java/driver [-x]
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.
Assignment FAQ Development Guide Code Checklist |