Factorial Program

class Factorial{

String message;

Factorial(int n){

message=n+"!= ";

}

int fact(int n){

if(n==1||n==0){

message=new String (message+"1= ");

return(1);

}

else{

message=new String ( message+ n +" * ");

return(n*fact(n-1));

}

}

publicstaticvoid main (String args[]){

int numb;

numb=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number..."));

if(numb<0){

JOptionPane.showMessageDialog(null,"Invalid Number!");

}

else{

Factorial ob=new Factorial(numb);

JOptionPane.showMessageDialog(null,ob.message);

}

}

}

The program is not displaying the message properly. It should be like 2*1 for 2!

[1874 byte] By [riddhi] at [2007-11-26 12:03:55]
# 1
What is it doing instead?
jverd at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...
# 2
You create a Factorial instance, but never invoke the method fact.
comstevebrecher at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...
# 3
Maybe throw a ob.fact(numb) in there.
ChargersTule1 at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...
# 4

changed code is here.....

import javax.swing.*;

import java.awt.*;

class Factorial{

String message;

Factorial(int n){

message=n+"!= ";

}

int fact(int n){

if(n==1||n==0){

message=new String (message+"1= ");

return(1);

}

else{

message=new String ( message+ n +" * ");

return(n*fact(n-1));

}

}

public static void main (String args[]){

int numb;

numb=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number..."));

if(numb<0){

JOptionPane.showMessageDialog(null,"Invalid Number!");

}

else{

Factorial ob=new Factorial(numb);

// Changed here

ob.fact(numb);

JOptionPane.showMessageDialog(null,ob.message);

}

}

}

in the above code .... u, nowhere tried to display the calculated fatorial value. rest ur program is running correctly....

bye

sadash at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...
# 5

sorry guys for not making it clear....

else{

Factorial ob=new Factorial(numb);

JOptionPane.showMessageDialog(null,ob.message+ ob.fact(numb));

}

well here is the modified on that I have made. Actually, I want to give output like

3!=3*2*1=6

but it is only giving 3!=6.The rest is missing. I mean once the string message is reinitialised with n! inspite of creating a new reference of the string and assigning it is not working?

riddhi at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...
# 6

> >

> else{

>Factorial ob=new Factorial(numb);

> JOptionPane.showMessageDialog(null,ob.message+ ob.fact(numb));

> }

>

> well here is the modified on that I have made.

> Actually, I want to give output like

>

> 3!=3*2*1=6

>

> but it is only giving 3!=6.The rest is missing.

One step at a time:

Factorial ob=new Factorial(numb);

At this point, message contains "6!=".

JOptionPane.showMessageDialog(null,ob.message+ ob.fact(numb));

Now, how does the output string concatenation happen? The second component is concatenated with the first component.But the first component is presented before, textually and chronologically, the second component which invokes ob.fact. Do you see the solution?

comstevebrecher at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...
# 7

oh, Thanks now I understood it thanks a lot dear here is the modified code:

else{

Factorial ob=new Factorial(numb);

int fact=ob.fact(numb); // A variable holds value

JOptionPane.showMessageDialog(null,ob.message+fact);

}

I this fine or there is a better way to implement?

another thing will you please tell how after

JOptionPane.showMessageDialog(null,ob.message+fact);

I want to append the output to already exising one but how?

riddhi at 2007-7-7 12:29:40 > top of Java-index,Java Essentials,New To Java...