Error : Main method not found in java class - java

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)

just change your Parameterized constructor to this
public Fact(int x) {
fact = 1;
i = 1;
n = x;
}
because you declare factorial in default constructor and you are not calling it. So, 0 is assigned to factorial and then you r trying to multiply it. Which makes no sense.

Rename the class file name Fact.java to FactMain.java.
private int fact;
public Fact()
{ fact=1;
i=1;
}
public Fact( int x)
{ n=x; }
Note, your default constructor set fact but constructor Fact( int x) set n. Hence fact is 0. So your output is 0 too.
Solution:
public Fact(int x) {
fact = 1;
i = 1;
n = x;
}
Or,
public Fact(int x) {
this(); // default constructor
n = x;
}
Here is the complete solution:
Create a single class file named FactMain.java, then paste the following code:
class Fact {
private int i;
private int n;
private int fact;
public Fact() {
fact = 1;
i = 1;
}
public Fact(int x) {
this();
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();
}
}

Your main method is in FactMain.java, but you are saving a file as Fact.java.
You will need to save the file as FactMain.java as JVM expects main to be in the same class as the name of .java file.

You have saved your file as Fact.java. So java is trying to find the main class in Fact. Save your file as FactMain.java It should work.

You have defined your main class in FactMain and most probably after compilation while running you're trying to execute
java Fact
And hence you got the error because there is no main method in Fact class.
Once you compile the .java file you will get two class files Fact.class and FactMain.class so you should execute
java FactMain

Move the FactMain class to FactMain.java
FactMain.java
public class FactMain
{
public static void main(String dt[])
{
Fact obj= new Fact(6);
obj.getAnswer();
}
}
Allow the Fact class to remain in the Fact.java file
Fact.java
public class Fact {
private int i;
private int n;
private int fact;
public Fact() {
fact = 1;
i = 1;
}
public Fact(int x) {
this();
n = x;
}
public void getAnswer() {
while (i <= n) {
fact = fact * i;
i++;
}
System.out.println(fact);
}
}
Compile the classes...
javac {package path}\FactMain.java
Run the main class
java {package path}.FactMain

Related

Which overridden methods are invoked?

I am having hard time to understand the solution of the given question. I can't understand at each step which of the class' methods are invoked.
I tried to make a list for what are a,b,c declared types and actual types then try to chose overridden or overloaded methods but it is complex.
class Upper {
private int i;
private String name;
public Upper(int i) {
name = "Upper";
this.i = i;
}
public void set(Upper n) {
i = n.show();
}
public int show() {
return i;
}
}
class Middle extends Upper {
private int j;
private String name;
public Middle(int i) {
super(i + 1);
name = "Middle";
this.j = i;
}
public void set(Upper n) {
j = n.show();
}
public int show() {
return j;
}
}
class Lower extends Middle {
private int i;
private String name;
public Lower(int i) {
super(i + 1);
name = "Lower";
this.i = i;
}
public void set(Lower n) {
i = n.show();
}
public int show() {
return i;
}
}
class Tester {
public static void main(String[] args) {
Lower a = new Lower(1);
Middle b = a;
Upper c = new Middle(5);
a.set(c);
b.set(a);
c.set(b);
System.out.println(a.show());
System.out.println(b.show());
System.out.println(c.show());
}
}
What is printed as a result of System.out.println(a.show()); after the set commands? Answer is 1
What is printed as a result of System.out.println(b.show()); after the set commands? Answer is 1
What is printed as a result of System.out.println(c.show()); after the set commands? Answer is 1
I don't get why the answers of all these are 1. Also I can't tell which class' overridden or overloaded methods that "a.set(c); b.set(a); c.set(b);" uses. A detailed explanation would be really helpful.
a.set(c) uses the set-method from Middle, as that overrides the one from Upper and the (overloaded) set from Lower is not applicable because c is not an instance of Lower.
Therfore j is set to c.show() which returns c's attribute j, so it will be set to 5. Consequently the (Lower-)attribute i of a is never touched and remains at 1 when it is shown and printed.
Try to resolve the others yourself.

Receives one integer and returns the factorial of the number passed

Creating two separate packages mathematics and Application. With a class called MathHelper and Application in either one. I need to add static method tothe MathHelper.java class called factorial(int) that receives one integer and returns the factorial of the number passed. A main method is added to application called and calls the Mathhelper.factorial. This is the code i have so far...
public class Application {
public static void main(String[]args) {
System.out.println(MathHelper.doubleInt((9)));
}
}
public class MathHelper {
public static void main(String[]args) {
}
public static int fact(int factNum) {
if (factNum==1) {
return 1;
}
else {
return factNum + (fact(factNum - 1));
}
}
}
You can calculate factorial using:
loops:
public long fact(int factNum) {
long fact = 1;
for (int iteration = 2; iteration <= factNum; iteration++) {
fact = fact * iteration;
}
return fact;
}
streams:
public long fact(int factNum) {
return LongStream.rangeClosed(1, factNum)
.reduce(1, (long fact, long iteration) -> fact * iteration);
}
recursion:
public long fact(int factNum) {
if (factNum <= 2) {
return factNum;
}
return factNum * fact(factNum - 1);
}

Testing a class

I am trying to test the class by making a program. I don't really understand how to. All I know is that it has something to do with prime numbers, but that is all I know. Can someone please help me? I would really appreciate it if you do. Thank you.
import java.util.Scanner;
public class Android
{
public static int tag = 1;
private String name;
public static int n = 0;
Android ()
{
name = "Bob";
changeTag();
}
String getName(Object input)
{
return input.getClass().getName();
}
private static boolean isPrime(int n)
{
for(int i = n - 1; i > 0; i--)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static void changeTag()
{
do
{
tag++;
} while (!isPrime(n));
}
}
There will be a class that contains the main method. (Assume MainClass the name of the class.)
Main class looks like:
public MainClass{
public static void main(String args[]){
/*Source Code*/
}
}
And you can make Android object in main method. Like this:
Android android = new Android();
And then, JVM (Java Virtual Machine) will run the Android constructor for Android class. Depending on Android constructor, the JVM will run changeTag() method. And changeTag() method finds the Prime by increasing the tag variable.
If you want to verify the prime, use follow this code:
System.out.println("prime"+tag);
You can see that the prime(tag) is output from the console window.
Full source:
Android Class :
import java.util.Scanner; //You don't need because you don't use Scanner.
public class Android
{
public static int tag = 1;
private String name;
public static int n = 0;
Android ()
{
name = "Bob";
changeTag();
}
String getName(Object input)
{
return input.getClass().getName();
}
private static boolean isPrime(int n)
{
for(int i = n - 1; i > 0; i--)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static void changeTag()
{
do
{
tag++;
} while (!isPrime(n));
System.out.println("prime"+tag);
}
}
Main Class :
public MainClass{
public static void main(String args[]){
Android android = new Android();
}
}
(May be an error because I didn't run this program. Explained only on the basis of the theory.)

Passing dynamic primitive type (int) to a method

In Java, the output of s is 0. I do not understand why and would it be possible to somehow get the correct value of s (1000 here)?
public static void main(String args) {
int s = 0;
List<Integer> list = getList(s);
System.out.println("s = " + s);
}
public static List<Integer> getList(int s) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i); s++;
}
}
In C# there were out descriptors to indicate that the variable is going to change if I'm not mistaken..
I'm not going to get the list.size() in general!
In Java, all method arguments are passed by value, i.e. copy. So, changes to the copy are not visible to the caller.
To address your second question, you can just use list.size() on the caller side.
I see two ways
1) Make 's' as static variable and move it to class level
2) Create class with getter/setter for list and int and return the object for getList call
public static MyWrapperObj getList(int s) {
......
return wrapperObj
}
class MyWrapperObj
{
private List<Integer>;
private countS;
....
//getter/setters.
}
Java doesn't allow for passing parameters by reference - but you could wrap it in an object like this:
class IntHolder {
private int s;
IntHolder(int s){
this.s = s;
}
public void setS(int s){
this.s = s;
}
public int getS(){
return s;
}
public void increment(){
s++;
}
}
class Test{
public static void main(String[] args) {
IntHolder s = new IntHolder(0);
List<Integer> list = getList(s);
System.out.println("s = " + s.getS());
}
public static List<Integer> getList(IntHolder s) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i); s.increment();
}
return list;
}
}
In java, arguments passed to methods are passed by value.. you will need to make s a global or instance variable in order to modify it in other methods. This is just the way java works. e.g.
public class Test{
private int s;
public Test(){
s=0;
increment();
//print now will be 1000.
}
private void increment(){
s = 1000;
}
}

Java 'this' keyword

I'm just beginning in programming and I'd like to make exercise from a book, but I can't. That's my problem:
public class increment {
int increment() {
return this + 1; // aka this++
}
public static void main(String[] args) {
int a = 0;
System.out.println(a.increment());
}
}
As you for sure guessed already, that it doesn't works, I want to ask you how to get outputed integer a incremented by one, but using keyword 'this'.
Regards and sorry for stupid questions.
It is strange to name a class like a method.
I guess you wanted this:
public class Counter {
int val;
public Counter (int start) {
val = start;
}
public void increment() {
val ++;
}
public String toString () {
return Integer.toString (val);
}
public static void main(String[] args) {
Counter counter = new Counter (0);
counter.increment ();
System.out.println(counter.toString ());
}
}
this is an object (the current object). You cannot "increment" it.
A way to do it is:
public class Increment {
int a = 0;
int increment() {
return a + 1;
// or: return this.a + 1;
// or: a++; return a; if you want a to be incremented from now on
}
public static void main(String[] args) {
Increment inc = new Increment();
System.out.println(inc.increment());
}
}
The this keyword in Java refers to the current scope's object instance. I don't think it's what you're looking for in this case.
In your example, a isn't an object of the class increment, it is a primitive int. In order to use the .increment() function you defined, it would have to be an object of type increment.
One option that may be what you're looking for would be the following.
public class Increment { //Java likes capitalized class names
private int myInt;
public Increment(int a) { //constructor
myInt = a;
}
public int increment() {
return ++myInt;
}
public static void main(String[] args) {
Increment a = new Increment(0);
System.out.println(a.increment());
}
}
In this example, we make a new class of type increment, which internally contains an integer. Its increment method increments that internal integer, and then returns the number.
you are using operator + for your current object (this). Operator overloading is not supported in java.
Something like this will work:
class MyInteger {
private int internal;
public MyInteger( int value ){
this.internal = value;
}
public int incerment(){
return ++this.internal;
}
}
public class Increment {
public static void main(String[] args) {
MyInteger a = new MyInteger(0);
System.out.println(a.increment());
}
}
You see, you can only implement methods for your own classes, not for existing classes, or for primitives like int.
i don't think you can use this to return the value, except if you're making a new class like this:
class Increment1
{
private int a;
public int increment2(int a)
{
this.a=a;
return this.a + 1;
}
}
public class Increment
{
static Increment1 b = new Increment1();
public static void main(String[] args)
{
int a = 0;
System.out.println(b.increment2(a));
}
}
You cannot increment a class like this.
You have to use a member variable that you can increment.
public class Test {
private int var;
public Test(int i) {
this.var = i;
}
int increment() {
this.var++;
}
}
public static void main(String[] args) {
Test t = new Test(0);
System.out.println(t.increment());
}
This refers to the current instance of the class, not a particular member.
You want to increment a property (I'm guessing of type long or int), and not the instance of your increment class (should be Increment, by the way).
Something like this would work:
public class increment {
private int innerValue = 0;
int increment() {
innerValue+=1
return innerValue; // aka this++
}
public static void main(String[] args) {
increment a = new increment()
System.out.println(a.increment());
}
}

Categories

Resources