954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Recursion Help

Hey everyone, I recently had the assignment to write a recursive program to calculate greatest common denominator.
I barely understood recursion when our instructor explained it to us.
I understood how it works and calling on itself I am just bad at implementing it.
I think I almost got my program to run correctly. it does everything but print out the answer. It will take my two numbers but will not do anything after. Any help would be greatly appreciated.

import TerminalIO.KeyboardReader;

 public class gcd
 {
  public static void main(String []args)
  {

	KeyboardReader reader = new KeyboardReader();
	int gcd;
	int a;
	int b;
	System.out.print("Enter your first number-");
	a = reader.readInt();
	System.out.print("Enter your second number-");
	b = reader.readInt();
  }
	public static int gcd(int a, int b)
	{
	 if(a > b) 
	  return gcd(b, a % b);
	   else
	 return gcd(a, b % a);
	}
  }


Thanks

MrHardRock
Light Poster
41 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

well, you're not calling your method, and you have written no code to be performed after reading those two numbers.

what do you expect it to do?

also, in your recursive method, I think in one of the cases you should not call the method again, like this:

// return a if a is the smallest number
// if b is the smallest, make a recursive call to the method
public static int recursiveMethod(a, b){
if ( a < b )
  return recursiveMethod(b, a);
return a;
}
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Ok so trying to understand recursion better, what is different about what you did than me? and so are you saying I have no method to print my answer?

MrHardRock
Light Poster
41 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

your code in your main method (and thus the running of your application) stops immediately after you read your input, you don't call your gcd method and you never print your results.

Also, the difference between my method and yours is, that in my method there will be either zero or one recursive calls, your method will call itself indefinetely.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Oh I see what you mean. I just need to figure out how to do that. Thanks

MrHardRock
Light Poster
41 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Hey everyone, I recently had the assignment to write a recursive program to calculate greatest common denominator. I barely understood recursion when our instructor explained it to us. I understood how it works and calling on itself I am just bad at implementing it. I think I almost got my program to run correctly. it does everything but print out the answer. It will take my two numbers but will not do anything after. Any help would be greatly appreciated.

import TerminalIO.KeyboardReader;

 public class gcd
 {
  public static void main(String []args)
  {

	KeyboardReader reader = new KeyboardReader();
	int gcd;
	int a;
	int b;
	System.out.print("Enter your first number-");
	a = reader.readInt();
	System.out.print("Enter your second number-");
	b = reader.readInt();
  }
	public static int gcd(int a, int b)
	{
	 if(a > b) 
	  return gcd(b, a % b);
	   else
	 return gcd(a, b % a);
	}
  }

Thanks


This might also help: http://www.danzig.us/java_class/recursion.html and here: http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Recursionamethodfunctioncallsitself.htm and this: http://www.java-samples.com/showtutorial.php?tutorialid=151

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

well, it's best not to use the method call as a return statement, and have whether or not the method is recursively called depend on the value of an expression of an if statement, or another structure that 'll give the option to once reach an end, so that the correct value is returned.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
MrHardRock
Light Poster
41 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

I think I got it now, thank you stultuske.

MrHardRock
Light Poster
41 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: