Programming Java help!!!!!!!!!

Hexuze

Active member
Supreme
Joined
May 19, 2011
Messages
20,359
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
Awards


It's excellent for math help. I'm pretty sure they can help with programming in general.
 

abcplay57

Member
Joined
Aug 4, 2012
Messages
73
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
Recursion works like a loop. It "loops" by calling the method within itself. But to ensure that it doesn't go on forever, you need to include a base case that will end the recursion sequence. Usually in programming classes it has a format like (for example)...

private static void example(int counter)
{

//base case
if(counter == 10)
{
System.out.println("Recursion done");
}
else //recursive step
{
counter++;
example(counter);
}
}

After that make sure you call it first in the main method.

public static void main(String[] args)
{
example(0);
}

This is a reaaaally simple example.
 

waTiem

Member
Joined
Dec 9, 2012
Messages
9
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
sure.

All function are called with an activation record which is a data structure that contains all of the variables that are passed to the function and is placed on the program stack. A recursive function is a function that calls itself; each time it calls itself, a new activation record is created. The trick is to establish a stopping condition or otherwise call itself with different parameters so that eventually you reach the stopping condition.

As an example, here's a java function that prints "Sakura Sucks" 2^k times (call it with k =8 for 256 times).


static void SS(int k)
{
if(k == 0) System.out.println("Sakura Sucks");
else
{
SS(k - 1);
SS(k - 1);
}
}


It's the power of computers!
 

6ari8

Active member
Veteran
Joined
Jul 6, 2011
Messages
2,209
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
The main thing is to understand the Base Case and the Recursive case. Everything else is fairly simple.

I think the best example you can find for recursion is the factorial function. This example can help you:
 

Wabbit

Banned
Legendary
Joined
Nov 15, 2011
Messages
11,335
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
Recursion is the process of repeating itself several times until the condition is true.
A function calling it self is a recursive function.Idk java.
It would be like this in general for functions in C or C++
Code:
function()
            {
               statement;
                function(); //calling the same function.
               }
 

Pesh

Active member
Elite
Joined
Oct 21, 2008
Messages
5,501
Kin
0💸
Kumi
1,280💴
Trait Points
0⚔️
For me, this is one of the best tutorials on Java recursion.
It's very well explained, uses programming terminology and has some great exercises and examples.
 
Top