Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

I am working on a program using stacks. Is there anybody willing an able to explain a few things?

OpenStudy (anonymous):

Attached is a word document with my code so far and it outlines exactly what I need help with.

OpenStudy (rsmith6559):

Part 4: The calls to Stack.push doesn't check the array to see if there's room to push anything. Calls to Stack.pop don't check to see if the array is empty. Either of these is an error. Part 2: These methods check for those error conditions. Part 3, 4: The code for these is the same. One is done in main() and the other changes the push and pop methods to be safe. With both, it's redundant, but it really is needed in the class. Part 6: I'm not sure, sorry.

OpenStudy (anonymous):

Hi, I am not a java programmer by trade so you may need to check this implementation but this is the essence of the class with regard to the assertions. They are intended as guard statement to check that the preconditions, class invariant, and post conditions have been met, they come from design by contract which is an important OO principle. You probably want to change the assert messages :) An important thing to remember about assertions is you only put guard statements in them not code that is performing domain functionality. I.e. if the asserts were not included in this class it would still function just throw exceptions probably some sort of access or out of range exception when the Edge case failed. Another thing to note about assertions is that there should be some way for the user of the class to check whatever you are asserting as otherwise you are just creating a ticking bomb, not a nice thing to do. Assertions can be excluded from the build which is a technical reasons why you do not put code that is required for the class to work The other thing I am not sure about with Java is if you can use the constants to define the size of a array sorry, like a said I am not a Java programmer (It was about ten years ago the last time I wrote any java) but this answer is mainly about the assertion part. public class Stack { private static final int MAX_SIZE = 100; private int[] stackArray = new int[MAX_SIZE]; private int top = 0; public void push(int pushValue) { assert(!isFull(), "Err we are going to blow the stack if we do that, never a good idea!"); stackArray[top] = pushValue; top++; } public int pop() { assert(!isEmpty(), "What exactly are we supposed to pop eh?"); top--; return stackArray[top]; } public bool isEmpty() { return top==0; } public bool isFull() { return top==MAX_SIZE; } } Anyway hope some of that helps.

OpenStudy (anonymous):

That does help, though it's not quite java. I'm working on trying to figure out how to access and create methods for each part, the isFull or isEmpty.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!