#Don't understand Recursive Helper Method.

171 messages · Page 1 of 1 (latest)

frank finch
#

I am currently learning Recursive methods but one thing I do not understand is how the Recursive Helper Method works and how it is to be used.

frank ingotBOT
#

Hey, @frank finch!
Please remember to /close this post once your question has been answered!

frank finch
#

What I don't understand about is the the use behind it or the implementation. When my professor showed an example of it then I saw that there were two methods using the same name with one having the base case

#

But then when the professor had us do it and gave us a sample to work off of then it just threw me off

mint basin
#

so you basically wanna just know what it is?

frank finch
#

pretty much

mint basin
#

it's running method in same method

#

why people use it? It's type of loop

#

you should be careful with it, it may cause infinite loop

#

alright, so listen:

#
void add(int currentNumber) {
       System.out.println(currentNumber);
add(currentNumber+1);
}
#

do you know what will it do?

frank finch
#

it will print out the number?

mint basin
#

oh wait

#

forgot to add it

#

now

frank finch
#

It will print out the number then add it then repeat

mint basin
#

first: do you think will it be infinite loop?

frank finch
#

Ya

#

actually no

#

it does not have a return statement

mint basin
#

why do you think so?

mint basin
#

let me explain nkw

#

output will be infinite

#

it will go 0, 1, 2, 3, ...

#

why?

method

sends current number

go back to method and adds +1 to the number

frank finch
#

ok

mint basin
#

idk if you understand

#

basically, if the method ends, it runs SAME METHOD again, but with bigger number

frank finch
#

So the code u posted would not be in a infinite loop because the method is not returning? I all got is that it would print out the number and then +1

frank finch
#

ok I reread the code

mint basin
#

it will be same as:
int x = 0
while (true) {
System.out.println(x++);
}

frank finch
#

u called apon add at the end

mint basin
#

yes, the method is being repeated

#

it NEVER ENDS

frank finch
#

ok got it

mint basin
#

cause it always repeat itself

#

now

#

do you know how to finish the recursive method(

#

cause who would like to have infinite loop there

frank finch
#

would you have to put in a if statement?

mint basin
#

yes

mint basin
#

just for practice

frank finch
#

sure one sec

mint basin
#

so stop loop when currentNumber == 50

frank finch
#

void add(int currentNumber) {
if(currentNumber <= 50)
add(currentNumber+1);
System.out.println(currentNumber);
}

#

Like this?

mint basin
#

yes

#

so now it would print number 0-50

#

idk if you understand recursive now

frank finch
#

I understand it better now, thx.

#

but now i don't get the Recursive Helper Methods

hexed nacelle
frank finch
#

I just don't get the point behind it

#

I don't understand how I am suppose to use it

hexed nacelle
#

it's used to add another lay of complexity

#

say we wanted to find the factorial of every number to 100

#

and add each of those sums together

mint basin
#

it works quite the same

hexed nacelle
#

you would have a helper method loop through to 100

#

and call that method each time

#

adding it to a sum

#

another example is a WebCrawler

#

sometimes you recursive method can't do all the work by itself.

mint basin
#

it repeats the method checking x - 1 string character and checks it, if finished and nothing found, it returns false,

frank finch
#

i dont get it

#

sorry

mint basin
frank finch
#

the string?

#

but there is no strings?

mint basin
#

String s

#

s.chatAt(x)

frank finch
#

oh

#

oh i understand a bit

#

So the helper method is to check for strings

#

that is it?

mint basin
#

to check string character at specific index

#

which CHANGES every method run

#

return isPalindrome(s, low-1, high+1);

#

you see it? low-1, high+1

frank finch
#

ya

mint basin
#

so example string: hello

#

that would check:
x = 4
string.charAt(x) - o (x-1)
string.charAt(x) - l (x-1)
...
if (x <= 0) return false; (nothing found AFTER string character iteration)

#

do you understand now?

#

recursive works basically same as while() loop

frank finch
#

think so

mint basin
#

can you make simple method for recursive?

#

just to make sure

#

I will give u a task

frank finch
#

sure

mint basin
#

check how many digits has string using iteration and recursive

#

example: hi18s6
output: 3

frank finch
#

uh suew

#

sure

mint basin
#

ofcourse its exercise just for you

#

if you dont wanna do it, dont do it

#

if any questions, ask them

frank finch
mint basin
#

dont return boolean 🙂

#

return int

frank finch
#

So change this to int?

mint basin
#

yes

#

and dont do num++

#

its recursive

mint basin
# frank finch

alright you could do it like that, but you didnt use recursive here

frank finch
#

I didn't implement a function to return the method

mint basin
#
public static int checkDig(String e, int index, int results) {
if (e.length() <= index) return results;
if (Character.isDigit(e.chatAt(index)) {
checkDig(e, ++index, ++results);
}
return checkDig(e, ++index, results);
}
#

I think that should it be it

frank finch
#

didn't know u could do that

#

but then how would the helper method com in

mint basin
#

oh right

#

that would cause an error

#

now

#

wanna make another task? 😄

frank finch
#

I have an hw where I have to use recursive that takes two object types to print out true or false if the object is same data type and value

#

So I will suffer with that one lmfao

mint basin
#

alright, good luck

frank finch
#

thank you for the help

mint basin
#

np

frank finch
#

but i am gonna ask for more help if i run into errors

mint basin
#

ok

frank finch
# frank finch

Oh I have a question regarding this sample, is this the correct way to do recursive helper method as compared to the screenshot above?

mint basin
#

not really

#

+it causes an infinite loop

#

method should be 1

frank finch
#

So I found out that I can't use the equals() function to compare the data types and value of two objects

#

is there a function that can compare data types because all I have so far is a if statement for the values

frank finch
#

professor said so

mint basin
#

he's stupid then

#

you compare objects using equals

#

otherwise you can use ==

#

but you use == for int, long, char etc

frank finch
#

all she said was to use other predefined mehthods

mint basin
#

to override equals?

#

or you can use compareTo

#

if (object.compareTo(object2) == 0)

frank finch
#

ya but the thing is that I need to make a function that compares both data and value and if they are the same then it prints out true

mint basin
#

and compare specific field

#

like: id, string, name, age

#

you know

frank finch
#

ok

mint basin
#

search on google

frank finch
#

I never learned comparable so ill seach it up to save time

mint basin
#

how to implement Comparable java

frank finch
#

got it

mint basin
#

yes

frank finch
#

thx buh

mint basin
#

np

mint basin
#

its for interfaces and abstract classes

#

he is looking for something to compare

hexed nacelle
mint basin
#

yes

mint basin
#

but you use it to check if class implements sth

dim grove
#

its recursive q

#

a hint is make a method

#

public List<File> getfiles(File dir,List<File> oldlist){
}