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.java
toFactMain.java
.Note, your default constructor set
fact
but constructorFact( int x)
setn
. Hencefact
is0
. So your output is0
too.Solution:
Or,
Here is the complete solution:
Create a single
class
file namedFactMain.java
, then paste the following code: