java all about recursion and reflection many thanks 1.Write a recursive method t
ID: 3720378 • Letter: J
Question
java
all about recursion and reflection
many thanks
1.Write a recursive method to add up the integers from 1 to N (where N is passed in as a parameter).
2.Write a recursive method to add up the contents of an array of doubles.
3.The Towers of Hanoi problem (Google it!) can be solved by the following recursive algorithm ...
Write a recursive Java method to implement this algorithm.
4.Does the following recursive method get into infinite recursion? Why?
5.What is the output when the display method of an object created from this class is called?
6.What's the output from the following program?
7.Write an if statement that prints "I'm a snob" if the current object is an instance of the Upper class.
Explanation / Answer
1.Write a recursive method to add up the integers from 1 to N (where N is passed in as a parameter).
Answer)
A recursive method to add up the integers from 1 to N :
public class RecursionAdd {
public static void main(String[] args) {
int input = 10;
int result = recAddNos(input); // Calling the recursion method with number 10
System.out.println("Result of the sum is: " + result);
}
public static int recAddNos(int number) { // REcursive method to add number from 1 to N which is passed as the parameter in the number
if (number != 0)
return number + recAddNos(number - 1); // Recursion call
else
return number;
}
}
Output:
Result of the sum is: 55
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.