I am new to concepts of java. while preparing my first program of classes with objects i encountered a problem. here is the code and the error..please resolve.. PROGRAM:
class Fact
{
private int i;
private int n;
private int fact;
public Fact()
{ fact=1;
i=1;
}
public Fact( int x)
{ n=x; }
public void getAnswer()
{
while(i<=n)
{fact=fact*i;
i++;}
System.out.println(fact);
}
}
class FactMain
{
public static void main(String dt[])
{
Fact obj= new Fact(6);
obj.getAnswer();
}
}
ERROR:
Main method not found in class Fact, please define the main method as:
public static void main(String[] args)
Rename the class file name
Fact.javatoFactMain.java.Note, your default constructor set
factbut constructorFact( int x)setn. Hencefactis0. So your output is0too.Solution:
Or,
Here is the complete solution:
Create a single
classfile namedFactMain.java, then paste the following code: