Friday, October 31, 2014

Java Useful Pro



Check whether a number is prime or not with output

 
import java.util.*;
          public class PrimeNumber 
{
public static void main(String[] args) 
{
 Scanner scanner = new Scanner(System.in);
           System.out.println("Please enter a number: ");
 int num = scanner.nextInt();
 PrimeNumber primeNum = new PrimeNumber();
                    if ( primeNum.isPrime(num) ) 
            {
            System.out.printf("\n Result: The number %d is Prime", num);
             } 
           else
           {
            System.out.printf("\n Result: The number %d is not Prime", num);
        }
    }
    public boolean isPrime(int num) 
   {
        if ( num < 2 ) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) 
        {
            if ( num % i == 0 ) 
            {
                return false;
            }
        }
        return true;
    }
}

Prime numbers upto given number using for loop

 
import java.util.*;
           public class PrimeNumber 
{
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
                   System.out.println("Please enter a number: ");
        int num = scanner.nextInt();
        if ( num < 2 ) 
        {
            System.out.println("\n There are no Prime Numbers available");
            System.exit(0);
        }
        System.out.printf("\n The Prime Numbers from 1 to %d are: \n 2", num);
        PrimeNumber primeNum = new PrimeNumber();
 
        for (int i = 3; i <= num; i++) 
        {
            if ( primeNum.isPrime(i) ) 
           {
                System.out.print(", " + i);
            }
        }
    }
    public boolean isPrime(int num) 
    {
        if ( num < 2 ) return false;
        for (int i = 2; i <= Math.sqrt(num); i++) 
        {
            if ( num % i == 0 ) 
           {
                return false;
            }
        }
        return true;
    }
}

Strings vs StringBuffer vs Stringbuilder with to string()
                            
                  class Employee 
                  {
        int empno;
        String ename;
        String job;
        String hireDate;
        int sal;
                  public Employee(int empno, String ename, String job,String hireDate,   int sal) 
                 {
            this.empno = empno;
            this.ename = ename;
            this.job = job;
            this.hireDate = hireDate;
            this.sal = sal;
        }
              public String toString() 
              {
            String str = " Employee [ empno=" + empno
                       + " , ename=" + ename
                       + " , job=" + job
                       + " , hireDate=" + hireDate
                       + " , sal=$" + sal
                       + " ]";
            return str;
        }
    }

Why Strings and Wrapper classes are immutable and final in java

 
class WhyMutable 
    {
        public static void main(String[] args) 
        {
            String name = "Parker";
            Double sal = 60000.00;
            displayTax(name, sal);
        }
        
    static void displayTax(String name, Double num) 
        {
            name = "Hello " + name.concat("!");
            num = num * 30 / 100;
            System.out.println(name + " You have to pay tax $" + num);
        }
    }

Exception handling in java
 
import java.io.*;
          class Demo1 
          public static FileInputStream f1(String fileName)throws  FileNotFoundException 
                 {
            FileInputStream fis = new FileInputStream(fileName);
            System.out.println("f1: File input stream created");
            return fis;
        }
         public static FileInputStream f2(String fileName) 
         {
            FileInputStream fis = null;
            try 
           {
              fis = new FileInputStream(fileName);
            } 
            catch (FileNotFoundException ex) 
            {
              System.out.println("f2: Oops, FileNotFoundException caught");
            } 
            finally 
            {
              System.out.println("f2: finally block");
            }
            System.out.println("f2: Returning from f2");
            return fis;
           }
                     public static void main(String args[]) 
                     {
            FileInputStream fis1 = null;
            FileInputStream fis2 = null;
            String fileName = "foo.bar";
            String fileName = null;
                       System.out.println(  "main: Starting " + Demo1.class.getName()
                               + " with file name = " + fileName);
             try 
             {
              fis1 = f1(fileName);
             } 
             catch (FileNotFoundException ex) 
             {
              System.out.println("main: Oops, FileNotFoundException caught");
             } 
             catch (Exception ex) 
            {
              System.out.println("main: Oops, genreal exception caught");
            }
            fis2 = f2(fileName);
                      System.out.println("main: " + Demo1.class.getName() + " ended");
        }
    }

No comments:

Post a Comment