Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Generic Stack Class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


1

2

10

11

12


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:

 
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sulaman,

welcome to the Ranch and enjoy the stay!

Can you also show the part that makes a number being expressed in base b?

Can you tell us what there errors are?

One thing I noticed is this:

Why is stackTop -1?
 
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.

    There are other things less serious.
     
    Sulaman Yaqoob
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi and thanks for helping, I went back into the code and edited things. I think this following code is much simpler and the problems are less. But still there are minor errors:


     
    Marshal
    Posts: 26463
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I can see quite a few minor errors. But perhaps they are not the same as the minor errors that you can see, so if you'd like to discuss your minor errors (or mine) then let us know.
     
    Saloon Keeper
    Posts: 12811
    278
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I see some big errors as well as some minor ones.

  • You're redeclaring your generic type parameters in your method declarations, hiding the generic type parameter used by your interface.
  • Declaration of a generic array. This will lead to problems 9 out of 10 times, including:
  • Accepting an array in a public constructor without performing a defensive copy.
  • The guards in your methods don't enforce the method's pre-conditions. Don't print error messages, throw exceptions instead.
  • Stacks class implements the interface without parameterizing it.
  • Raw type used to declare the Name variable in the main method.
  • Redundant or inconsistent naming. Name the interface Stack and name the class FixedArrayStack.
  • Confusing name stackTop. Use length or size instead.
  • Unused field capacity. Get rid of it.
  • Pointless ternary statements. Statements of the form (expression ? true : false) can be rewritten to just (expression).
  • Don't repeat yourself. Instead of comparing stackTop to data.length all over your code, just call isFull().
  •  
    Campbell Ritchie
    Marshal
    Posts: 72423
    315
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic