Skip to main content

Posts

Showing posts with the label Java

Difference between Java and C++

How Java is different from C++ 1.       Java does not support “goto”, “sizeof”, and “typedef” keywords. 2.       Java does not support keywords like “auto”, “extern”, “register”, “signed”, and “unsigned”. 3.       Java does not support any pre-processor like #define, #include. 4.       Java does not support operator overloading. 5.       Java does not support template class. 6.       Java does not support multiple inheritance. 7.       Java does not support global variable. 8.       Java does not support pointers as in C++. 9.       Java replace destructor() function with finalize() method. 10.   There are no header files in Java. 11.   Java is a highly case sensitive language.

Implement Button Click (ActionEvent) using swing and applet

Following example is to demonstrate button click event using actionListener and ActionEvent in java  import java.awt.*; import java.applet.*; import java.awt.event.*; /* <Applet code=buttonclick width=300 height=300> </Applet> */ public class buttonclick extends Applet implements ActionListener { TextField text1;    Button button1;    Label l1;      public void init()    { l1=new Label("Message: ");       add(l1);       text1=new TextField(15);       add(text1);       button1=new Button("Click");       add(button1);       button1.addActionListener(this);    }     public void actionPerformed(ActionEvent e1)    { String str="Hello, java is Great";       if(e1.getSource()==button1)        text1.setText(str);    } } To compile this pr...

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.setPr...

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.pri...

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 Al...

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 + " ");     }   } }    

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);            ...

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);    } }

Java program to calculate BMI (Body Mass Index)

import java.util.Scanner; public class BMI_Calc {         public static void main(String args[])         {                 double wt, ht,bmi;                 Scanner sc=new Scanner(System.in);                 System.out.print("Enter your wieght in pounds: ");                 wt=sc.nextDouble();                 System.out.print("\nEnter your hieght in inches: ");                 ht=sc.nextDouble();                 wt=wt*0.45359237;                 ht=ht*0.0254;                 bmi=wt/(ht*ht);                 System.out.print("\n\nBMI =...

Java program to display formatted output

Suppose the tuition for a university is $5,000 this year and increases 4.5% every year. Write a program that displays the tuition for the next four years. Display two digits after the decimal point. public class Q1 {         public static void main(String args[])         {                 double fee=5000;                 for(int i=0;i<4;i++)                 {                         fee=(fee*0.045) + fee;                         System.out.format("Increased fee for %d year = $%6.2f%n",(i+1),fee);                 }         } }

Java program To read a string and change it to alphabetical order. eg STRING=GINRST

import java.io.*; class charsort { public static void main(String args[])throws IOException    { String s1;       char temp;           DataInputStream in=new DataInputStream(System.in);       System.out.print("\n\n Enter string:  ");       s1=in.readLine();       char z[]=new char[s1.length()];       z[0]+=s1.charAt(0);       for(int i=1;i<s1.length();i++)       { z[i]+=s1.charAt(i);                    for(int j=0;j<=i;j++)          { for(int k=j+1;k<=i;k++)            { if(z[j]>z[k])               { temp=z[k];                  z[k]=z[j];                  z[j]=temp;     ...

Java program to find perfect integers

import java.util.Scanner; public class PerfectIntegers {         public static void main(String args[])         {                 int num,sum=0,count=2,p_count=0;                 Scanner sc=new Scanner(System.in);                 System.out.println("First four Perfect Integers are:");                 while(true)                 {                               for(int i=1;i<=count/2;i++)                         {                                 if(count%i==0)                  ...

How to Compile Java Program Using Command Prompt

Step 1 : Go to command prompt (or type cmd in Run Dialog box). Step 2 : Set the path as the location of the bin directory of java which is located in the program file directory. In my case path = C:\Program Files\Java\jdk1.7.0_01\bin Step 3 : Now change directory to the location where source code is residing. Step 4 : After that time to compile java program. You need to type following commands: -   javac source_code.java   ( for compilation, if successfully compiled then program is ready to execute) java source_code (for execution)