school assignment

hey, i was wondering if someone could help me. this what im trying to do. im trying to write a method that uses recursion to calculate the factorial of a given number. i need to promt the user for an integer, then use that number to calculate the factorial, for example, 3! would be 3*2*1 =
[299 byte] By [redzone646a] at [2007-11-26 16:46:07]
# 1
Alright. What have you done so far?
CaptainMorgan08a at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 2

What part are you having trouble with?

Most people here aren't interested in writing your code for you, nor do we want to guess at where your difficulty lies.

Do you know what factorial is?

Do you know a recursive definition of factorial, independent of Java?

Do you know how to write a method in Java?

Do you know how to do multiplication in Java?

etc.

jverda at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 3
I think you should try it on your own if you haven't already. You could do this in about 3 lines of code... I think you can do it!
ErikSilkensena at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 4

i was thinking about somthing like this:

import java.util.*;

public class Recursion

{

int r;

public void main(){

System.out.println("Enter An Integer");

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

factorial(n);

System.out.println(n);

}

public void factorial(int n){

if(n > 0){

r = n*(n-1);

factorial(n-1);

}

}

}

redzone646a at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 5
Java is pass-by-value and not pass-by-reference. Therefore any changes made to the local variables n in the factorial method will NOT change the variable n in the main method
floundera at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 6

> i was thinking about somthing like this:

Did you compile and run it? (Lemme guess - no, it doesn't compile.)

What answer did it give you? (None, since it won't compile.)

Are you satisfied with the result? (Nope.)

If not, what do you plan to do about it?

Better think again. That code is wrong.

%

duffymoa at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 7
i know its wrong, it compiles but gives me zero everytime
redzone646a at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 8
Give any thought as to why?Do you know how to run a debugger? Are you using an IDE at all?If not, did you add any print statements to see what's going on?Or did you plan to wait until someone fixed your grand mistake for you?%
duffymoa at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 9

Learn

import java.util.Scanner;

class RecursiveTester

{

int factorial(int n)

{

if(n==1)

return 1;

return n * factorial(n-1);

}

}

class Recursive

{

public static void main (String[] args)

{

int num;

Scanner s = new Scanner(System.in);

System.out.println("Enter a number ");

num = s.nextInt();

RecursiveTester test = new RecursiveTester();

int ans = test.factorial(num);

System.out.println("The factorial for " + num + " Is " + ans);

}

}

Output

Enter a number

4

The factorial for 4 Is 24

fastmikea at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 10
As I mentioned in another thread, what if the user enters zero or a negative number?
floundera at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 11

i think the program should terminate.

if(n==1)

return 1;

else if(n<=0)

System.exit(0);

return n * factorial(n-1);

or

else if(n<=0)

return 0;

return n * factorial(n-1);

Message was edited by:

fastmike

Message was edited by:

fastmike

fastmikea at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 12
fastmike,Did you want to help the original poster learn to program? Notice how other posters were asking questions and getting the OP to think about what he was doing. It's that give-a-man-a-fish versus teach-a-man-fishing thing.
DrLaszloJamfa at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 13
i thought he could study from the code i posted not to submit the same thing. if he did submit my code then sooner or later he will get caught by getting a big F in his course.
fastmikea at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 14
> System.exit(0);Never call exit from low level code. Give the higher level code a chance to react to an error: throw an exception instead.
DrLaszloJamfa at 2007-7-8 23:13:30 > top of Java-index,Java Essentials,New To Java...
# 15
hmm nice. thanks for pointing it out. i have never used system.exit(0). so i thought if a user enters 0 or - number the program should terminate but your explanation makes sense.
fastmikea at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...
# 16

> i thought he could study from the code i posted not

> to submit the same thing. if he did submit my code

> then sooner or later he will get caught by getting a

> big F in his course.

I used to think that, but I noticed that the same people would come back again and again, asking for forum members to do other homework assignments. So they learned not to think. They learn that sooner or latter someone on the forum would do their homework for them. I'd like to hope that such people would get caught, but I honestly doubt it happens often enough to deter such people.

DrLaszloJamfa at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...
# 17
Appologies Doc will never happen again. i am also a newbie just trying to increase my skills in java .
fastmikea at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...
# 18

> Appologies Doc will never happen again. i am also a

> newbie just trying to increase my skills in java .

That's cool. There are plenty of questions here where you can do that. I'd just like to see help tempered with not giving away too much. When the end of semester looms at Unis (and finals for AP High School courses) this forum gets flooded with "do my homework" posts.

DrLaszloJamfa at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...
# 19
Gotcha ! ;-)p.s: when they graduate and look for a programming job and the Employer ask them a simple question what is OO Methadology? what is encapsulation? what is inheritance? what is polymorphism?Employee Answers: sorry sorry and one more time sorry or pass.
fastmikea at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...
# 20

> Appologies Doc will never happen again. i am also a

> newbie just trying to increase my skills in java .

Then perhaps giving a discussion on the principles and general structure of recursive functions would have served better both you and the OP? Posting a couple of lines of trivial source code doesn't really make you or him/her any wiser.

jsalonena at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...
# 21

no never and i will never claim myself to be very good in java. i can say this that i am better than most of the newbies over here. explanation always helps me out but in this program no need of explanation since i knew how the factorial function should work. the only difference b/w me and Op is that i was able to write a program for him so he can learn what mistakes he was making.

fastmikea at 2007-7-21 16:50:18 > top of Java-index,Java Essentials,New To Java...