- Jun 22, 2005
- 1,687
- 0
- 0
I have a pretty hefty worksheet i have to get done before school starts next week, its for AP Computer Science..........a worksheet on just Recursion
I was absent the day of class he taugh recursion, but he still thinks i should do this worksheet for points, so i need some help
1.)
Iterative Solution
public int Factorial(int number)
{
int factor, count;
factor = 1;
for(count = 2; count <= number; count++)
factor = factor*count;
return factor;
}
Now write the Recursive Solution.
2.)
What control structure appears most commonly in a recursive method?
3.)
Write a Java value-returning method that implements the recursive formula:
f(n)=f(n-1) + f(n-2) with the base cases f(0) = 1 and f(1) = 1.
4.)
Predict the output of the call Review(9);
public void Review(int n)
{
System.out.println("Entering function, N = " + n);
if (n>0)
Review(n/2);
System.out.println("Leaving Function, N = " + n);
}
5.)
Predict the output of the following calls:
Exer(13)
Exer(124)
Exer(21785)
public void Exer(int n)
{
if (n>0)
{
Exer(n/10);
System.out.println(n%10);
}
}
6.)
Describe the output of the following method when the user enters the characters T, E, S, T,.?
public void WhatZitDo()
{
EasyReader console = new EasyReader();
char ch;
ch = console.readChar();
if (ch != '.')
{
WhatZitDo();
System.out.println(ch);
}
else
System.out.println(ch);
}
I was absent the day of class he taugh recursion, but he still thinks i should do this worksheet for points, so i need some help
1.)
Iterative Solution
public int Factorial(int number)
{
int factor, count;
factor = 1;
for(count = 2; count <= number; count++)
factor = factor*count;
return factor;
}
Now write the Recursive Solution.
2.)
What control structure appears most commonly in a recursive method?
3.)
Write a Java value-returning method that implements the recursive formula:
f(n)=f(n-1) + f(n-2) with the base cases f(0) = 1 and f(1) = 1.
4.)
Predict the output of the call Review(9);
public void Review(int n)
{
System.out.println("Entering function, N = " + n);
if (n>0)
Review(n/2);
System.out.println("Leaving Function, N = " + n);
}
5.)
Predict the output of the following calls:
Exer(13)
Exer(124)
Exer(21785)
public void Exer(int n)
{
if (n>0)
{
Exer(n/10);
System.out.println(n%10);
}
}
6.)
Describe the output of the following method when the user enters the characters T, E, S, T,.?
public void WhatZitDo()
{
EasyReader console = new EasyReader();
char ch;
ch = console.readChar();
if (ch != '.')
{
WhatZitDo();
System.out.println(ch);
}
else
System.out.println(ch);
}