Java Bluej worksheet

BigCoolJesus

Banned
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);
}
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Do you have specific questions about recursion? Have you read your textbook and online resources about the subject?

We aren't going to answer your HW questions for you, but if you have specific questions, you'll find help here.
 

BigCoolJesus

Banned
Jun 22, 2005
1,687
0
0
Originally posted by: MrChad
Do you have specific questions about recursion? Have you read your textbook and online resources about the subject?

We aren't going to answer your HW questions for you, but if you have specific questions, you'll find help here.

well the people in off topic told me to post my specific h/w questions, so i did


basically, i know nothing about recursion, as i said i wasnt there the day he showed how to do it (i left for vacation) and he emailed these questions to me and told me to have em done before i got back.

I guess i can start by asking how do you do recursion? (like a step through)
 

talyn00

Golden Member
Oct 18, 2003
1,666
0
0
Recursion - An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task.

base case(s), in which the problem is simple enough to be solved directly, and
recursive case(s). A recursive case has three components:
divide the problem into one or more simpler or smaller parts of the problem,
call the function (recursively) on each part, and
combine the solutions of the parts into a solution for the problem.

from: http://www.nist.gov/dads/HTML/recursion.html

You can find plenty of tutorials about recursion
 

BigCoolJesus

Banned
Jun 22, 2005
1,687
0
0
ok, ill read up and try em.........


Ok, so even the help your giving me is a little to complicated........i can really only grasp a new idea if i see a problem given to me (a simple one at first) and then are stepped through it....

How bout this, can someone walk me through the first question on my worksheet, and then ill go from there?
 

talyn00

Golden Member
Oct 18, 2003
1,666
0
0
This site. has far easier to understand examples. For the factorial program it may be far easier to think of it like this.

Function F = factorial

Base case: F(0) = 1
Recursive case: F(x) = x*F(x-1)
where x is some number greater or equal to 0.

this notation should also help you out with question # 3, which looks like the fibonacci sequence one

also base case may also be referred to as stopping case by some people
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Just sit down for a couple of hours and read up. You should be able to get all those problems fairly easy once you have even a basic understanding of recursion. At times it may seem pointless but it really is something you should know somewhat (if you are about CS at all).