wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Simple Class, how does this look? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Class, how does this look?" Watch "Simple Class, how does this look?" New topic
Author

Simple Class, how does this look?

John Logan
Greenhorn

Joined: Aug 02, 2002
Posts: 6
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 ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi John,
Well for a first attempt at writing a class it ain't bad. Here are some suggestions:
  • The javadoc comment for your class should occur before not inside the class definition
  • For your constructor you used a block comment instead of a javadoc comment
  • You left your num variable at package scope. It should probably be private.
  • There may be more efficient ways to factor an integer using recursion, but that's a judgement call. I discourage recursion except in known algorithms.
  • 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.


  • Hope this helps,
    Michael Morris


    Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
    John Logan
    Greenhorn

    Joined: Aug 02, 2002
    Posts: 6
    Hi Michael,
    Thanks for the comments info, I wasn't sure about that.
    # 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.

    That's great, I'll work on trying this out now.
    Cheers,
    Barkat Mardhani
    Ranch Hand

    Joined: Aug 05, 2002
    Posts: 787
    Hi John:
    What if a zero, negative integer or non-integer is provided at command line....
    Barkat
    Michael Morris
    Ranch Hand

    Joined: Jan 30, 2002
    Posts: 3451
    Good point Barkat. We should throw some sort of Exception, shouldn't we?

    Michael Morris
    John Logan
    Greenhorn

    Joined: Aug 02, 2002
    Posts: 6
    Hi guys,
    I think exceptions/error catching is a bit beyond me atm, but it's something I'll be hoping to put in a later time when I know a bit more about them
    Thanks for the suggestion.
    John
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    You can try the exceptions tutorial from Sun.
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9044
        
      10
    Or -- You can test
    if number > 0, continue
    and if not, print "number must be greater than zero".


    JavaBeginnersFaq
    "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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
    Michael Morris
    Ranch Hand

    Joined: Jan 30, 2002
    Posts: 3451

    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
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Simple Class, how does this look?