Results 1 to 11 of 11
  1. #1
    Slin9 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Post Suggestions for writing this algorithm

    I am currently working on an assignment to write an algorithm in Java to produce and output that is of the perfect squares 1-15. (ie, 1, 4, 9, 16, 25.... 225)

    What I have now is...
    Java Code:
    class PerfectSquares{
         public static void main(Strings args[]){
            int i;
            for(i=1;i<=15;i++){
                 System.out.println(i*i);
            }
         }
    }
    I think i started out okay, but I'm rather lost.
    Last edited by Eranga; 03-03-2011 at 04:53 AM. Reason: code tags added

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Does your code compile?
    Does it run?
    Does it produce correct output?

    I'm also lost as I don't know what your problem is.

  3. #3
    Slin9 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    It compiles fine, but when i run the .class file, it gives me an "exception in the thread "main" java.lang.NoClassDefFoundError: PerfectSquares/class"

    The issue at the moment is that I can't get it to output the perfect squares of 1-15, and I'm not quite sure how to change it, so that it does.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Are you running your code from the command line? If so the commands are:

    Compile
    javac MyProgram.java

    Run
    java MyProgram

    Note how you leave the .class off.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Slin9 View Post
    It compiles fine, but when i run the .class file, it gives me an "exception in the thread "main" java.lang.NoClassDefFoundError: PerfectSquares/class"
    By the way, did it never occur to that this is vital information that you should have included in your first post?

  7. #7
    Slin9 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Wow... I feel like an absolute idiot right now.
    Earlier it compiled fine. This time I compiled it, and it gave me the error, "classPerfectSquare2.java:2: cannot find symbol
    symbol : Class strings
    location: class PerfectSquares
    public static void main(strings args[]){"

    so i fixed the strings into string.

    and it compiled and ran like a charm.

    I appreciate all your help! Sorry for wasting your time.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Slin9 View Post
    classPerfectSquare2.java
    class PerfectSquares
    Looks like you have multiple classes. Maybe you should clean things up.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Slin9 View Post
    Wow... I feel like an absolute idiot right now.
    Earlier it compiled fine. This time I compiled it, and it gave me the error, "classPerfectSquare2.java:2: cannot find symbol
    symbol : Class strings
    location: class PerfectSquares
    public static void main(strings args[]){"

    so i fixed the strings into string.

    and it compiled and ran like a charm.

    I appreciate all your help! Sorry for wasting your time.
    @Slin9: you should be more exact: you mention 'Strings' in your original post, you mention 'strings' in this post and you claim that your program compiles when you changed it to 'string'. You also didn't mention the compiler errors thrown at you nor the runtime errors when you tried to start your program an yet you wanted us to help you. We can't help you if you don't supply this vital information. This all may seem nitpicking to you but it isn't; we are not psychic; help us to help you; the more accurate your information is you supply the more accurate the answers will be.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Slin9 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    @Jos: I realized this the further I got along in this thread. Next time I will know better, as you can tell, I'm quite new at this. I thank everyone for their patience and help. Even thought I didn't provide vital information, you guys still asked me questions, that helped me think more thoroughly about the code, which was a great help all in itself!

    Thanks again!

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Slin9 View Post
    @Jos: I realized this the further I got along in this thread. Next time I will know better, as you can tell, I'm quite new at this. I thank everyone for their patience and help. Even thought I didn't provide vital information, you guys still asked me questions, that helped me think more thoroughly about the code, which was a great help all in itself!

    Thanks again!
    You're welcome of course; real programmers (like me) are real nitpickers, i.e. they notice every little detail so those details better be correct otherwise your thread may go astray into lala-land with discussions about details that don't matter. In fact it's simple: supply a small, compilable snippet of code that demonstrtates the problem; also supply the compiler error message(s) (if any) and give an accurate description of how your code is misbehaving. Otherwise your threads end up like a guessing game or they're simply ignored.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. [suggestions] minesweeper
    By temyong in forum New To Java
    Replies: 7
    Last Post: 12-30-2010, 03:10 AM
  2. looks good looking for suggestions..
    By search4survival in forum New To Java
    Replies: 10
    Last Post: 11-30-2010, 12:59 PM
  3. Looking for suggestions
    By tonino in forum New To Java
    Replies: 5
    Last Post: 09-26-2010, 07:17 PM
  4. Book suggestions
    By Lil_Aziz1 in forum Java Applets
    Replies: 3
    Last Post: 01-04-2010, 02:38 AM
  5. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 07:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •