Where to put main method ?
I am working on a small factorial problem and keep recieving Exeption in thread "main" java.lang.NoSuchMethod: main error
I know my class is missing the public static void main(String[] args) method but since i am used to using Blue J and have now moved to SDK im not sure how to implement the main method into my current program
I would appreciate any help, current program is :
public class Factorial
{
private int total;
private int temp;
private int counter;
public Factorial(int factor)
{
counter = factor;
total = factor;
Calculation();
}
public void Calculation()
{
if(counter > 1){
temp = counter - 1;
total = (total * temp);
counter = temp;
Calculation();
}
else{
System.out.println(total);
}
}
}
I normally put the public static void main(String[] args) method as thelast method in the class. But that's just a matter of taste, because youcan put it anywhere at the class level.kind regards,Jos (<-- good taste ;-)
JosAHa at 2007-7-15 21:45:36 >

But how should it look in this program is what i mean ?ive put it in at the start as public static void main(String[] args){}but if i do it that way the program does not actually do anythingsorry if the answer is obvious and im just missing it
> but if i do it that way the program does not actually do anything
... because there's nothing in the body of that main method. I'm sure
you want to create a Factorial object there. Seeing your code, the
newly created object takes over from there, so this should do:public static void main(String[] args) {
Factorial fact= new Factorial(10);
}
kind regards,
Jos
JosAHa at 2007-7-15 21:45:36 >

Just putting the main method is not enough. There should be something in it.
public static void main(String[] args)
{
//create instance ot the class
new Factorial(5);
}
-
FeedTV : Ajav TV
http://www.feedfeeds.com/feedtv
jessua at 2007-7-15 21:45:36 >

You have to put some code in the main method ofcourse, otherwise the program doesn't do anything.
What should you put in the main method? That depends on what you want the program to do. So get clear first what you want your program to do, and write some code.
Please try it yourself first instead of waiting until somebody here gives you a pre-cooked anwer - you learn by doing it yourself, not by copying code written by others.
Okay i see how to call the factorial method now
say i want it to ask me for the number to use instead of a constant what can i use to do that
like here i want it to ask me for the number instead of using 5 :
public static void main(String[] args)
{
//create instance ot the class
new Factorial(5);
}
thanks again
> say i want it to ask me for the number to use instead
> of a constant what can i use to do that
You do realize that this new little problem has nothng to do with
factorials, do you? I suggest you'd build a small NumberPrompter
class that can handle the job. The benefit is that this class most
certainly can be useful again when you're dealing with other problems.
For starters, have a look at the Integer.parseInt(String s) method.
kind regards,
Jos
JosAHa at 2007-7-15 21:45:36 >

Sorry i should have explained a little better
i have to create to new programs, one iterative and one recursive for calculating : fac(n) = n * (n -1) * (n-2) * (n-3) ... till n = 1
the program does its job properly when run in Blue J (the whole reason for this stupid programme is to practice using SDK instead of Blue J funny enough)
im still working through it and hope to solve it on my own (for the sake of pride more than anything ) but i have a limit and im really struggling with using this new main method .
Really appreciatte any help and thanks for it so far
i did it !! weeeeesorry been at it for a while thanks to everyone for the great helpnow i just need to figure out how to make the main method ask me for the factor N instead of setting it inside the program :Scheers again
> i did it !! weeeeeCongratulations.> now i just need to figure out how to make the main> method ask me for the factor N instead of setting it> inside the program :SSee my reply #7.kind regards,Jos
JosAHa at 2007-7-15 21:45:36 >

I did get the input part using the Scanner method taught in a book i bought for refrence which is working a treatjust wanted to thank everyone again for the great helpi think im gonna miss BlueJ lol