I've created a little program to print out all factors of a certain number (supplied by the command line). It seems to work pretty nice and helps me refresh my memory of math while I'm learning Java Could I have some opinions on the code, telling me if I've done something wrong/not well/inefficient etc? It's the first time I've truly "rolled my own" as my previous code has really been out of books - so be gentle please
Obviously I run this by java Factors [number] from the command line. Thanks all. [ August 11, 2002: Message edited by: John Logan ]
Originally posted by Michael Morris: You probably shouldn't write to stdout except in your main method. Think about returning an array of ints and writing them out in main.
I agree with that - letting it write directly to stdout violates the Single Responsibility Rule and decreases reusability. Even better than returning an array might be to implement it iterator-like:
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jan 30, 2002
Originally posted by Ilja Preuss: Even better than returning an array might be to implement it iterator-like
No doubt a more elegant solution. But then we should probably store our results in a Set as well to make iteration very simple to implement. Michael Morris
Joined: Jul 11, 2001
Originally posted by Michael Morris: No doubt a more elegant solution. But then we should probably store our results in a Set as well to make iteration very simple to implement.
That certainly would be one way. I wonder how hard it might be if we simply remembered the next factor to return, though...