Hi, I am trying to implement a generic stack class and apply it on General Base Counting. Given a file with a number b (from 2 - 8) and another number n (written in base 10), output all numbers from 1 to n in base b. For example, if the file contains "3 5", you should output:
I have code already written, but there are errors I just can't seem to fix. I use intellij, so it would be easier if you copy paste the following code there. Here is the code, and I would appreciate any help:
Tthere are two prioncipal kinds of stack implementation, but those of each kind work the same way The two kinds are:-
1: Those based on an array.
2: Those based on a linked likst.
There is a basic eample of an array stack in Effective Java by Joshua Bloch.
Apart from what Piet showd you, I can see some more errors which will make your stack work incorrectly.
Don't make any of the methods generic methods. That means they might not necessarily have the same value for T in the method as in the rest of the class.
There is more wrong with your constructor; don't pass the array as a constructor parameter. You need to create that array inside your class. You can't create a T; you would have to do something ike this:-I shall let you work out whether I am right to write @SuppressWarnings.The reason you are setting −1, as Piet noticed, is because you made another mistake in line 44. Use the correct ++ and -- operators and you can start from 0. Then your value will be equal to the depth of the stack.
I think you shouldn't issue an error message for a full stack. I think you should either throw an exception or replace the backing array with a larger array. Look at ,int,int)]this method which can create a larger array.
Sulaman Yaqoob wrote:. . . I went back into the code and edited things. . . . there are minor errors . . .
But you left most of the errors unchanged. There are major errors, as Stephan and I told you.
You were right to get rid of the create() method; since it is an instance method, you cannot call it to create your first instance
I would go further than Stephan did about your taking an array as a constructor parameter: it is wrong whether you take a defensive copy or not. You are passing the responsibility for creating part of the stack to other code. I think an object should always, “take care of itself,” and passing that array means it is dependent on something else, and I think that is similar to a violation of the single responsibility principle.
Forget Steve. Look at this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners