Skip to main content

Posts

Showing posts from September, 2012

Invoking main method from another class in java

public class test1 {         static int count; public static void main(String args[]) { System.out.println("Inside test1 class"); if(count<=1) { count++;        test2 obj = new test2();  } } } class test2 { public test2() { String args[] = new String[] {"ABC"}; System.out.println(" Inside test2 class\n Now, invoking main method of test1 class"); test1.main(args); } }                                                                     Next >>  Error: main methods starts with capital M in java

Error: main method's m starts with capital letter

Code:- public class test1 { public static void Main(String args[]) // here  main method starts with capital letter (like Main) { System.out.println("Testing Main Method"); } } This program will compile but does not run. IT give a runtime error: "main method not found in test1 class" Solution: change the capital m to small case letter (main).

Invoking main Method in java program

main Method placed in another class rather than the program's starting class Suppose I create a java program named as Test1.java . In this I added two classes Test1 and Test2. If I place main method in Test2 class then it will give a runtime error like "main method not found in test1 class". For Example: - class test1 { public static void display() { System.out.println("Inside Test1 Class"); } } class test2 { public static void main(String args[]) { System.out.println("Inside Test2 class"); test1.display(); } }   Solution: Save the above program with test2.java then compile and run.                                                                              Next >>  Invoking main method from another class

Multiple main methods in a Java Program

Main method is the only entry point of the java console application. Normally we used only one main method in our program. A java application can have multiple main methods. Code: -  // myclass.java class test { public static void main(String args[]) { System.out.println("Main of test class..."); } } public class Myclass { public static void main(String args[]) { String str[]={"A","B","C"}; test.main(str); } }  In the above example, program starts from Myclass . For invoking a  main method it require a command line arguments also. So that we declared and initialized str[] and passed it to the test.main(str);

Java.lang.NoClassDefFoundError problem Solved

Exception in thread "main" java.lang.NoClassDefFoundError Sometimes when we try to execute java program we got error message like "Exception in thread "main" java.lang.NoClassDefFoundError". But this is not a major issue. It just a common human mistake. You might have compiled java program with small letter-case followed by .java extension but actually file name is in the capital letters. The program will compile without any error but do not. Note: Check filename before you compile java program ( Java is case Sensitive). for example: In my case: filename is MyClass.java I compiled it as javac myClass.java        Compiled successfully But do not execute... give error message like this Solution: Compile it as javac Myclass.java Java is very case sensitive language.

Use of setPriority method of Thread Class in java

import java.io.*; class A extends Thread { public void run()    { System.out.println("Thread A started");       for(int i=5;i>0;i--)       { System.out.println(i);        }     } } class B extends Thread { public void run()    { System.out.println("Thread B started");       for(int i=5;i>0;i--)       { System.out.println(i);       }     } } class C extends Thread { public void run()    { System.out.println("Thread C started");       for(int i=5;i>0;i--)       { System.out.println(i); }     } } class thread3 { public static void main(String args[])    { A thread_a=new A();       B thread_b=new B();       C thread_c=new C();       thread_c.setPriority(Thread.MAX_PRIORITY);       thread_b.setPriority(thread_a.MAX_PRIORITY);       thread_a.setPriority(Thread.MIN_PRIORITY);       System.out.println("Thread A");       thread_a.start();       System.out.println("Thread B");       thread_b.st

Implementation of all methods of the String class in java

import java.io.*; class strclass { public static void main(String args[])throws IOException    { String str,s2;       int ch,pos;       DataInputStream in=new DataInputStream(System.in);       while(true)       {       System.out.println("\n-------STRING CLASS--------\n");       System.out.print("\n 1. To Upper Case");       System.out.print("\n 2. To Lower Case");       System.out.print("\n 3. Replace");       System.out.print("\n 4. Trim");       System.out.print("\n 5. Equals");       System.out.print("\n 6. Equals Ignore Case");       System.out.print("\n 7. Length");       System.out.print("\n 8. Char At");       System.out.print("\n 9. Compare To");       System.out.print("\n 10. Concatenate");       System.out.print("\n 11. Substring");       System.out.print("\n 12. IndexOf");       System.out.print("\n 0. EXIT");  

Implementation of all methods of the vector class in java

import java.io.*; import java.util.*; class vecmethod { public static void main(String args[])throws IOException    { String s1,s2;       int ch,size,pos;       Vector list=new Vector();       DataInputStream in=new DataInputStream(System.in); System.out.print("\n\n Enter no. of Items: ");                     size=Integer.parseInt(in.readLine()); String s[]=new String[size];       while(true)       {       System.out.println("\n-------VECTOR CLASS--------\n");       System.out.print("\n 1. Add Element");       System.out.print("\n 2. Element At");       System.out.print("\n 3. Size");       System.out.print("\n 4. Remove Element(Item)");       System.out.print("\n 5. Remove Element(Pos) ");       System.out.print("\n 6. Remove All Element");       System.out.print("\n 7. Copy Into(array)");       System.out.print("\n 8. Insert Element");       System.out.print(&q

Generate series of prime numbers using java

import java.io.*; class prime_series {    public static void main(String args[])   { int st,end;     int flag=0;     DataInputStream in=new DataInputStream(System.in);     System.out.print("Enter starting range: ");     st=Integer.parseInt(in.readLine());     System.out.print("\nEnter ending range: ");     end=Integer.parseInt(in.readLine());     for(int i=st;i<=end;i++)     {      for(int j=2;j<=i/2;j++)      { if(i%j==0)         { flag=1;            break;          }        else         flag=0;      }      if(flag==0)       System.out.print(i + " ");     }   } }    

C++ program to implement scaling w.r.t origin

#include"graphics.h" #include<iostream> using namespace std; int main() { int count=0,y,factor[3][3],sx,sy; cout<<"Enter No. of cordinates: "; cin>>y; int **matrix, **m2; matrix=new int*[3]; m2=new int*[3]; for(int i=0;i<y;i++) { matrix[i]=new int[y]; m2[i]=new int[y]; } //initailization for(int i=0;i<y;i++) { for(int j=0;j<3;j++) { if(j==1) { cout<<"\n\nEnter y-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } else if(j==2) matrix[j][i]=1; else { cout<<"\n\nEnter x-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } } } cout<<"\n\nEnter scaling factor for x: "; cin>>sx; cout<<"\n\nEnter scaling factor for y: "; cin>>sy; for(int i=0;i<3;i++) { for(int j=0;j<y;j++)

C++ program to implement Translation w.r.t origin

#include"graphics.h" #include<iostream> using namespace std; int main() { int count=0,y,factor[3][3],tx,ty; cout<<"Enter No. of cordinates: "; cin>>y; int **matrix, **m2; matrix=new int*[3]; m2=new int*[3]; for(int i=0;i<y;i++) { matrix[i]=new int[y]; m2[i]=new int[y]; } //initailization for(int i=0;i<y;i++) { for(int j=0;j<3;j++) { if(j==1) { cout<<"\n\nEnter y-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } else if(j==2) matrix[j][i]=1; else { cout<<"\n\nEnter x-axis value for cordinate-"<<i+1<<" = "; cin>>matrix[j][i]; } } } cout<<"\n\nEnter translation factor for x: "; cin>>tx; cout<<"\n\nEnter translation factor for y: "; cin>>ty; for(int i=0;i<3;i++) { for(int j=0;j<y;

Program to implement Thread by extending Thread class in java

import java.io.*; class NewThread extends Thread { NewThread()   { super("Demo Thread");      System.out.println("child thread: "+this);      start();    }    public void run()    { try       { for(int i=5;i>0;i--)          { System.out.println("child thread: " + i);             Thread.sleep(500);           }        }catch(Exception e){}       System.out.println("Exiting child thread");     }  }     class thread2     { public static void main(String args[])       { NewThread obj=new NewThread();          try          { for(int i=5;i>0;i--)             { System.out.println(" Main thread: " +i);                Thread.sleep(1000);              }           }catch(Exception e){}           System.out.println("Exiting Main Thread");        }       }    

Java program to count objects using static variable

import java.io.*; class obj_call { static int count;      obj_call()    { count++;    } } public class count_obj { public static void main(String args[])    { obj_call e1= new obj_call();       System.out.println("\n Hi, Welcome to Java programming");       obj_call e2= new obj_call();       System.out.println("\n Java is fully OOP's language");       obj_call e3= new obj_call();       System.out.println("\n It provide more security than the c++");       System.out.println("\n No. of objects : "+ obj_call.count);    } }