7 Replies - 5766 Views - Last Post: 24 October 2006 - 07:26 PM Rate Topic: -----

#1 ProGraM   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Recursion help

Posted 19 October 2006 - 02:17 PM

Program with recursion(the real program):
import java.io.*;
import java.util.Random;
public class Recursion
{
	private static int ErroR(int userNum)
	{ 
		try
		{			
			if(userNum < 0 ){System.out.println("\nERROR\n-----------------------\nNo Numbers Lower then 0\n-----------------------\nPlease Start Over");}	   
			if(userNum > 100 ){System.out.println("\nERROR\n--------------------------\nNo Numbers Higher then 100\n--------------------------\nPlease Start Over");}
		}
		catch (NumberFormatException e){System.out.println("\nERROR\n--------------\nInvalid Answer\n--------------\nPlease Start Over");}
		catch (NullPointerException e){System.out.println("\nERROR\n--------------\nInvalid Answer\n--------------\nPlease Start Over");}
		return userNum;
	}
	private static int GuessNum(int userNum, int randNum)
	{ 
		{
			/* The number is to High, to Low, or Exact */
   			if(userNum < randNum){System.out.println("Go Higher!");}	
 			else if(userNum > randNum){System.out.println("Go Lower!");}
			else if(userNum ==randNum){System.out.println("You got it, YOU WIN!)");}
			return userNum;
		}		
	}
	public static void main (String[] args)throws IOException
	{
		int userNum, randNum; 
		String input;
		BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in));
		/* Generates random number */
		final int Max_Num = 100;
		Random ranGenerator = new Random();
		randNum = ranGenerator.nextInt(Max_Num);
		System.out.println("Hello!, and welcome to the guessing game. \nThe computer will generate a random number from 0 to 100 for you, \nand you will try to guess the number by entering a WHOLE NUMBER. \nIf you do not guess the number in 5 turns, GAME OVER! \nIf you guess the number in 5 turns, YOU WIN!\nGOOD LUCK!");		
		for(double guess=1; guess<=4; guess++)
		System.out.print( "\nGuess a Number: " );
		userNum = Integer.parseInt(input = keyboard.readLine().trim());
	}
}	  


This post has been edited by ProGraM: 25 October 2006 - 02:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Recursion help

#2 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Recursion help

Posted 19 October 2006 - 02:44 PM

I've run your code, but just by looking at it, you're not understanding recursion.... there are not recursive calls in your code, and you have included a for loop... which is what is replaced in recursion. Do not attempt to use the main method for the base of your code, rather use a sub-method to delegate the recursion, as the main method cannot call itself (which is recursion: a method which invokes itself).
You will need to add a counter which controls the number of guesses into your code since you will not have the for loop to do this for you.
give it another go and i'll help you some more :)
Was This Post Helpful? 0
  • +
  • -

#3 ProGraM   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Recursion help

Posted 20 October 2006 - 02:14 PM

ok this what i got now i think i get recursion but still confsued a bit.
import java.io.*;
import java.util.Random;
public class Recursion
{
private static String error1 (String check1)
{
	BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	try
	{
		int userInput = Integer.valueOf(check1);
	}		
	catch (NumberFormatException e)
	{
		System.out.println ("Enter Numbers only: ");
		userInput = Integer.parseInt(check1 = keyboard.readLine().trim());
		check1 = error1(check1);
	}
	return check1;
}
private static String error2 (String check2)throws IOException
{
	BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
	int userInput = Integer.valueOf(check2);
	while(userInput< 0|| userInput > 100)
	{
		System.out.print("Number between 1 & 100 only: ");
		userInput = Integer.parseInt(check2 = keyboard.readLine().trim());
		check2 = error2(check2);
	}
	return check2;
}
private static int HiLo (int userNum, int randNum)//Working on making this recursion cant figure it out :/
{
	if(userNum < randNum){System.out.println("Go Higher!");}	
	if(userNum > randNum){System.out.println("Go Lower!");}
	if(userNum ==randNum){System.out.println("You got it, YOU WIN!");}
	return randNum;
}	
public static void main (String[] args)throws IOException
{
	int userNum, randNum; 
	String input;
	BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in));
	/* Generates random number */
	final int Max_Num = 100;
	Random ranGenerator = new Random();
	randNum = ranGenerator.nextInt(Max_Num);
	System.out.println("Hello!, and welcome to the guessing game. \nThe computer will generate a random number from 0 to 100 for you, \nand you will try to guess the number by entering a WHOLE NUMBER. \nIf you do not guess the number in 5 turns, GAME OVER! \nIf you guess the number in 5 turns, YOU WIN!\nGOOD LUCK!");		
	System.out.print( "\nGuess a Number: " );
	userNum = Integer.parseInt(input = keyboard.readLine().trim());
}
}


This post has been edited by ProGraM: 22 October 2006 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Recursion help

Posted 22 October 2006 - 12:01 PM

Please do no PM me for homework assignments, i will answer all posts i have time for, there are many knowledgable people on this site.

first of all it took nearly a dozen changes just for this code to compile.
Integer and int are not the same thing.
int is a primative, while Integer is an object.
an Integer can be transformed to it's primative form:
Integer I = new Integer("1");
int i = I.intValue();



also none of your methods can access your variable named userInput as it is defined in a single method, change this to a global declaration (make it private to protect other classes from accessing it)

Your catch block error1 also throws another exeption and IOException, which will need to be handled, by either throwing it back to main where it can be handled, or with another try catch block.


now that your code should now compile (which compiler are you using btw?)...

This will allow your code to start, but after 1 number entered it will end, as there is no recursion yet.
A simple search of recursion on DIC turns up an example I wrote a while back, it's even in java: Java Recursion
read this and you should understand that the method must call itself. The example is a factorial example, which uses tail recursion, but there are many other types. Recursion can even be iterative if there is no cummalitive operation.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Recursion help

Posted 22 October 2006 - 12:06 PM

In the future please do not PM me to get answers to your questions.

You still don't understand the concept of recursion.

Here is a link that will get you started in Recursion.
Recursion

void myMethod( int counter)
{
if(counter == 0)
	 return;
else
	   {
	   System.out.println(""+counter);
	   myMethod(--counter);
	   return;
	   }
}


Here is an example of a recursive method. Notice the name of the method is myMethod. If you examine the code you will notice that inside the ELSE portion of the IF statement that there is another call to myMethod myMethod(--counter);. Recursion is essentially making a call to the method from within that very same method.

You will need a base case in order to stop the recursive process. In this example, recursion stops when the counter hits 0. This example method is only going to print out the counter backwards from whatever value is received by the method until it hits 0.
Was This Post Helpful? 0
  • +
  • -

#6 ajwsurfer   User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 385
  • Joined: 24-October 06

Re: Recursion help

Posted 24 October 2006 - 02:24 PM

Yo G, there are three parts to recursion that must be proved in order to make it work.

Looking at the code that was posted last:

1. You must prove an entry point:

"void myMethod( int counter)"

(can be called from anywhere)

2. You must prove a regular case

"myMethod(--counter);"

(the important thing here is "--counter" that decrements on each call)

3. You must prove an end case that will cause the recursion to exit

"if(counter == 0) return;"

(when the counter drops [by one each time] to 0 the function will recursivly exit all instances)


Happy recuring, I hope that this helps ;)
Was This Post Helpful? 0
  • +
  • -

#7 ProGraM   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Recursion help

Posted 24 October 2006 - 02:34 PM

yea....thx for the help sorry about msging you guys but i finished my program just touching it up.
Was This Post Helpful? 0
  • +
  • -

#8 Soymeatz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-August 06

Re: Recursion help

Posted 24 October 2006 - 07:26 PM

If you still need help, and example of recursion would be:
//Recursion to print 1 through 10 onto the screen.
public class recursion_example
{
	public static void main(String[] args)
	{
		int number = 1;
		Recursion(number);
	}
	public static void Recursion(int number)
	{
		if(number<11){ <--- This is the base case to prevent an infinite loop
			System.out.println(number); <--- This is the code that will be looped recursively
		Recursion(number+1);} <--- This is the actual recursion part
	}
}


This will do the same thing as:
for(int x=1; x<11; x++)
System.out.println(x);

The Iterative solution (the for loop) is more straight forward than direct recursion (a method calling itself) but recursion has major benefits in solving some more advanced problems. Hope this clears things up

This post has been edited by Soymeatz: 24 October 2006 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1