Java


Sample Programs :




·                  Can we overload main() method·                 
·                  Can we override main method in java
·                  Program to Date into String Type and Date Type.
·                  Area
·                  Program to calculate Biggest no from given 3 no's
·                  Prime Number
·                  Program to Determine Palindrome 
·                  Program to Calculate Pi value upto 50 decimal places.


Can we overload main() method in java:
Yes we can !!

But only public static void main(String[] args) will be used when your class is launched by JVM , as :- 


But it will always print 
main(String[] args)

public class Test {
    public static void main(String[] args) {
        System.out.println("main(String[] args)");
    }

    public static void main(String arg1) {
        System.out.println("main(String arg1)");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("main(String arg1, String arg2)");
    }
}


We can call the main() method yourself from code, of course - at which point the normal overloading rules will be applied. 
Note that you can use a varargs signature, as that's equivalent from a JVM standpoint: -
public static void main(String... args)


Can we override the main method in java

No!
main is a static method, and is thus not polymorphic. You may hide it be defining another static main method in a subclass, though.

Java Program to Convert Date Type to String and vice versa:

import java.text.SimpleDateFormat;
import java.util.Locale;


public class Date
{
    public static void main(String[] args) throws ParseException
    {
        String string = "12-02-2012";
        /* Converting from String to Date type */
        java.util.Date date1 = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH).parse(string);
        System.out.println("DateType-date-is ----->"+date1);
       
        /* Converting from DateType Date to String type */
        DateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
        String date2 = formater.format(date1);
        System.out.println("StringType-date-is --->"+date2);
       
       
    }
}


/*

output:-
DateType-date-is ----->Sun Feb 12 00:00:00 IST 2012
StringType-date-is --->12-02-2012

*/




Java Program to calculate Area and perimeter of a square:

import java.util.Scanner;
public class Area
{
 static int a,l,b,p;
 public static void main(String[] args)
 {
  System.out.println("Programe to calculate Area and Perimeter of Square or Rctangle ");
  Scanner l=new Scanner(System.in);
  System.out.println("Enter length:");
  String s1=l.next();
  int i=Integer.parseInt(s1);
  Scanner b=new Scanner(System.in);
  System.out.println("Enter Breadth: ");
  String s2=b.next();
  int j=Integer.parseInt(s2);
  a=i*j;
  p=2*(i+j);
  System.out.println("Area of the given square/rectange is: "+a);
  System.out.println("Perimeter of the square/rectangle is: "+p);
 }
}

/*
output:
Programe to calculate Area and Perimeter of Square or Rctangle 
Enter length:
5
Enter Breadth: 
6
Area of the given square/rectange is: 30
Perimeter of the square/rectangle is: 22

*/


Program to calculate Biggest no from given 3 no's
import java.util.Scanner;
public class Big
{
 int a,b,c,i,j,k;
 Big(int i,int j,int k)
 {
  if((i>j) && (i>k))
  {
   System.out.println("The biggest No is: "+i);
  }
  else if(j>k)
  {
   System.out.println("The biggest No is: "+j);
  }
  else
  {
   System.out.println("The biggest No is: "+k);
  }
 }
 
 public static void main(String[] args)
 {
  System.out.println("Program to calculate Biggest no from given 3 no's");
  Scanner a=new Scanner(System.in);
  System.out.println("Enter First Number:");
  String s1=a.next();
  int i=Integer.parseInt(s1);
  Scanner b=new Scanner(System.in);
  System.out.println("Enter Second Number:");
  String s2=b.next();
  int j=Integer.parseInt(s2);
  Scanner c=new Scanner(System.in);
  System.out.println("Enter Third Number:");
  String s3=b.next();
  int k=Integer.parseInt(s3);
  Big obj=new Big(i,j,k);
 
 }
}


/*
output:

Program to calculate Biggest no from given 3 no's
Enter First Number:
5
Enter Second Number:
9
Enter Third Number:
-8
The biggest No is: 9
*/


Prime Number

import java.util.Scanner;
public class Prime
{
 Prime(int j)
 {
  int i=1;
  int k=1;
  i++;
  while(i<=j)
  {
   if(j%i==0)
   {
    k=k+1;
   }
   i++;
  }
  if(k==2)
  {
   System.out.println(j+" Is a prime number ");
  }
  else
  {
   System.out.println(j+" Is not a prime number");
  }
 }
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter any Number to find whether it is prime or not: ");
  String s1=sc.next();
  int n=Integer.parseInt(s1);
  Prime obj=new Prime(n);
 }
}


/*
output:

Enter any Number to find whether it is prime or not: 
3
3 Is a prime number 

*/


Program to Determine Palindrome

package com.san;
import java.util.Scanner;


public class Palindrome
{
 static int l;
 public static void main(String[] args)
 {
  System.out.println("Programe to Determine Palindrome ");
  Scanner p=new Scanner(System.in);
  System.out.println("Enter a String:");
  String s1=p.next();
  String s2=new StringBuffer(s1).reverse().toString();
  if(s1.equals(s2))
  {
   System.out.println("String is Palindrome");
  }
  else
  {
   System.out.println("String is not a Palindrome");
  }
 }
}

/*
output:-
Programe to Determine Palindrome 
Enter a String:
i am a i
String is Palindrome
*/

Java Program to calculate Pi value upto 50 decimal places:

import java.math.BigDecimal;


public class PiReCalc {
    public static final int N = 1000; // # of terms
       public static void main(String[] args) {
      BigDecimal sum = new BigDecimal(0);      // final sum
      BigDecimal term = new BigDecimal(0);           // term without sign
      BigDecimal sign = new BigDecimal(1.0);     // sign on each term

      BigDecimal one = new BigDecimal(1.0);
      BigDecimal two = new BigDecimal(2.0);

      for (int k = 0; k < N; k++) {
         BigDecimal count = new BigDecimal(k);
         //term = 1.0/(2.0*k + 1.0);
         BigDecimal temp1 = two.multiply(count);
         BigDecimal temp2 = temp1.add(one);

        
         // The divide takes three arguments ie. BigDecimal divisor, int scale, int roundingMode   
        // We provided scale as 50 for getting 50 decimal points
         term = one.divide(temp2,50,BigDecimal.ROUND_FLOOR);

         //sum = sum + sign*term;
         BigDecimal temp3 = sign.multiply(term);
         sum = sum.add(temp3);

         sign = sign.negate();
      }
      BigDecimal pi = new BigDecimal(0);
      BigDecimal four = new BigDecimal(4);
      pi = sum.multiply(four);

      System.out.println("Calculated pi (approx., " + N + " terms and 50 Decimal Places): " + pi);
      System.out.println("Actual pi: " + Math.PI);
       }
}



/*
output:-
Calculated pi (approx., 1000 terms and 50 Decimal Places): 3.14059265383979292596359650286939597045138933077984
Actual pi: 3.141592653589793
*/