Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advance:)

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advance:)

    I'm supposed to create a method to find the nth harmonic number. Say the use asks for the third harmonic number, I want it to return the sum of 1, 1/2, and 1/3. I am required to solve this recursively and I have difficulty thinking that way. I am nonplussed as to why this code does not work. The System.out.prints I do indicate that n is decrementing as intended but the sum of harmonic numbers is not accumulating. I've included the mini test program so if you want to run this you can go ahead. Thanks! This is my first post here btw.

    public class HarmonicTest {
        public static void main(String[] args)
        {
            System.out.println(Harmonic.getHarmonic(3));  //getHarmonic(3));
        }
     
     
    }
     
    import java.util.*;
    public class Harmonic
    {
     
        public static double getHarmonic(int n)
        {
            if(n>0)
            {
                return(getHarm(n, 0));
            }
            else
                throw new InputMismatchException("The input for n must be greater than 0.");
     
        }
     
            public static double getHarm(int n, double harmonic)
            {
     
                harmonic = harmonic + (1/n);
     
                System.out.println("Int is: " +   n);
                System.out.println("Harmonic is: " + harmonic);
     
                if(n == 1)
                {
                     return harmonic;
                }
                else
                  return(getHarm((n-1), harmonic));
            }
     
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,616
    Thanks
    25
    Thanked 321 Times in 299 Posts

    Default Re: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advanc

    Whenever I hear about people new to programming doing mathematics that involve division, I always look for the same thing, and lo and behold I see it again.

    There is this little thing in programming that is called: Integer Division (division : Java Glossary)

    Integer Division is when you divide one integer from another integer. Everything you have ever learned in math will tell you that 1/2 = 0.5, but that is not the case in programming. In programming, 1/2 = 0. But, "Why?" you ask. The reason is that when you do any math with all integers, your result is an integer. So it will perform the 1 divided by 2, but it will throw away all of the decimals. So, in programming, 10/3 = 3, while in real math 10/3 = 3.33 (and a bunch more 3s). How can we solve this problem? Well if even ONE of the numbers in our math is a double instead of an int, all of our problems are solved. So, 1.0/2 = 0.5 since 1.0 is a double and not an int. Here are a few examples:
    1/2 = 0
    1.0/2 = 0.5
    1/2.0 = 0.5
    1.0/2.0 = 0.5
    5/4 = 1
    5.0/4 = 1.25
    5/4.0 = 1.25

    Now, why does this help you? Because of this line:
    harmonic = harmonic + (1/n);
    Notice the integer division: 1/n
    To solve this, you just need the 1 to be a double instead of an int, so this will solve it:
    harmonic = harmonic + (1.0/n);


    Tell me if all of that makes sense.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,616
    Thanks
    25
    Thanked 321 Times in 299 Posts

    Default Re: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advanc

    Also, a tidbit about recursion.

    You can actually make your code much more appropriate for recursion. Here is a step-by-step of what I would do:

    Usually when doing recursion, the first thing you want to do is check the base-case.
    public static double getHarm(int n)
    {
    	if(n==1)
    		return 1;
    }

    Next we determine what we will be doing. Since we are adding, we can add the result of our recursion to our current value. We also want to remember to alter the parameters in some way when we call the recursive method.
    public static double getHarm(int n)
    {
    	if(n==1)
    		return 1;
    	return getHarm(n-1) + (1.0/n);
    }

    And that is actually all it takes. The purpose of recursion is to create a simple algorithm (that is near impossible to understand just by looking at it). When you do recursion, you are sacrificing some efficiency, but sometimes it is the best way of doing something.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advanc

    This is great! I know exactly what you are talking about. I learned that back in my first comp sci class and of course if you don't use it, you lose it. I'll remember it forever now because it's been an issue. That's how these things work. Thanks a lot!

Similar Threads

  1. [SOLVED] Output isn't as expected.
    By woodcutterni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 10:55 AM
  2. How do you read this syntax?
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 8th, 2010, 03:09 PM
  3. New Syntax Highlighting Feature!
    By JavaPF in forum Forum Updates & Feedback
    Replies: 15
    Last Post: July 14th, 2010, 09:20 AM

Tags for this Thread