#Don't understand Recursive Helper Method.
171 messages · Page 1 of 1 (latest)
Hey, @frank finch!
Please remember to /close this post once your question has been answered!
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
so you basically wanna just know what it is?
pretty much
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?
it will print out the number?
It will print out the number then add it then repeat
first: do you think will it be infinite loop?
why do you think so?
and?
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
ok
idk if you understand
basically, if the method ends, it runs SAME METHOD again, but with bigger number
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
it will be infinite
ok I reread the code
it will be same as:
int x = 0
while (true) {
System.out.println(x++);
}
u called apon add at the end
ok got it
cause it always repeat itself
now
do you know how to finish the recursive method(
cause who would like to have infinite loop there
would you have to put in a if statement?
yes
can you make it here? so limit number will be 50
just for practice
sure one sec
so stop loop when currentNumber == 50
void add(int currentNumber) {
if(currentNumber <= 50)
add(currentNumber+1);
System.out.println(currentNumber);
}
Like this?
what don't you understand about them?
I just don't get the point behind it
I don't understand how I am suppose to use it
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
it works quite the same
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.
it repeats the method checking x - 1 string character and checks it, if finished and nothing found, it returns false,
works same as this, but it checks string there, not the number
oh
oh i understand a bit
So the helper method is to check for strings
that is it?
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
ya
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
think so
sure
check how many digits has string using iteration and recursive
example: hi18s6
output: 3
ofcourse its exercise just for you
if you dont wanna do it, dont do it
if any questions, ask them
alright you could do it like that, but you didnt use recursive here
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
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
alright, good luck
thank you for the help
np
but i am gonna ask for more help if i run into errors
ok
Oh I have a question regarding this sample, is this the correct way to do recursive helper method as compared to the screenshot above?
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
why?
nullpointer?
professor said so
he's stupid then
you compare objects using equals
otherwise you can use ==
but you use == for int, long, char etc
all she said was to use other predefined mehthods
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
make class that implements Comparable
and compare specific field
like: id, string, name, age
you know
ok
search on google
I never learned comparable so ill seach it up to save time
how to implement Comparable java
got it
yes
thx buh
np
instanceof
it works with regular classes too no?
yes
but you use it to check if class implements sth
try this
make a list of files in a directory it can be sub folder or more subfolder
its recursive q
a hint is make a method
public List<File> getfiles(File dir,List<File> oldlist){
}