Search This Blog

Tuesday 13 August 2013

Core Java Examples

Core java examples
========================================================================
import java.util.HashMap;
import java.util.Map;
class Emp
{
    int _empId;
    String _eName;

    public Emp() {
        _empId = 70000;
        _eName = "Ltd";
    }
    public void setEmpId(int empId) {
        _empId = empId;
    }
    public int getEmpId() {
        return _empId;   
    }
    public void setEName(String eName) {
        _eName = eName;
    }
    public String getEName() {
        return _eName;
    }
    public String toString() {
        return "Employee Details\n\nId: " + _empId + "\n\nName: " + _eName;
    }
}
class  HashMapDemo
{
    public static void main(String[] args)
    {
        Map _map = new HashMap();
        _map.put("hai","Hai");
        _map.put("number", new Integer(1));
        _map.put("employee", new Emp());
       System.out.println(_map.get("hai"));
       System.out.println(_map.get("number"));
       System.out.println(_map.get("employee"));
       System.out.println("Hello World!");
    }
}
========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class MathTable
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Math Table value");
        int table = Integer.parseInt(br.readLine());
        for (int index = 1;index < 11 ;index++)
        {
            System.out.println(index + "    *    " + table + "    =    " + (index * table));
        }
        System.out.println("Hello World!");
    }
}

========================================================================
class MatrixAddDemo
{
    public static void main(String[] args)
    {
        int _matrix1[][] = {
                                        {10,20,30},
                                        {40,50,60},
                                        {70,80,90}
                                   };
        int _matrix2[][] = {
                                        {10,20,30},
                                        {40,50,60},
                                        {70,80,90}
                                   };
         int rows, columns;
         int resultMatrix[][] = new int[3][3];

         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    resultMatrix[rows][columns] = 0;
             }
         }

         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    resultMatrix[rows][columns] = _matrix1[rows][columns] + _matrix2[rows][columns];
             }
         }
         System.out.println("First matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(_matrix1[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
         System.out.println("Second matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(_matrix2[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
         System.out.println("Result matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(resultMatrix[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
        System.out.println("Hello World!");
    }
}

========================================================================
class MatrixMulDemo
{
    public static void main(String[] args)
    {
        int _matrix1[][] = {
                                        {10,20,30},
                                        {40,50,60},
                                        {70,80,90}
                                   };
        int _matrix2[][] = {
                                        {10,20,30},
                                        {40,50,60},
                                        {70,80,90}
                                   };
         int rows, columns;
         int resultMatrix[][] = new int[3][3];

         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    resultMatrix[rows][columns] = 0;
             }
         }

         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                 for (int k = 0; k< 3 ; k++ )
                 {
               
                    resultMatrix[rows][columns] += _matrix1[rows][k] * _matrix2[k][columns];
                 }
             }
         }
         System.out.println("First matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(_matrix1[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
         System.out.println("Second matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(_matrix2[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
         System.out.println("Result matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(resultMatrix[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
        System.out.println("Hello World!");
    }
}

========================================================================
class MatrixTranDemo
{
    public static void main(String[] args)
    {
        int _matrix1[][] = {
                                        {10,20,30},
                                        {40,50,60},
                                        {70,80,90}
                                   };

         int rows, columns;
         System.out.println("Original matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(_matrix1[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
         int temp = 0;
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < rows ; columns++ )
             {
                    temp = _matrix1[rows][columns];
                    _matrix1[rows][columns] = _matrix1[columns][rows];
                    _matrix1[columns][rows] = temp;
             }
         }

       
         System.out.println("Transposed matrix\n\n");
         for (rows = 0; rows < 3; rows++ )
         {
             for (columns = 0; columns < 3 ; columns++ )
             {
                    System.out.print(_matrix1[rows][columns] + "    ");
             }
             System.out.println("\n\n");
         }
        System.out.println("Hello World!");
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import Pack.PackDemo;
class PackExample
{
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter a and b values");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        PackDemo packObj = new PackDemo(a, b);
        System.out.println("Pack add: " + packObj.add());
        System.out.println("Pack mul: " + packObj.mul());
        System.out.println("Hello World!");
    }
}
package Pack;
public class  PackDemo
{
    int a = 0;
    int b = 0;
    public PackDemo(int a1, int b1) {
        a = a1;
        b = b1;
    }
    public int add() {
        return a + b;
    }
    public int mul() {
        return a * b;
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Prime
{
    public static void main(String[] args)  throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int number;
        number = Integer.parseInt(br.readLine());
        boolean isPrime = true;
        int divisor = number - 1;
        while(divisor > 1) {
            if ((number % divisor) == 0)
            {
                isPrime = false;
            }
            divisor --;
        }
        if (isPrime) {
            System.out.println(number + " is Prime");
        } else {
            System.out.println(number + " is not Prime");
        }
        System.out.println("Hello World!");
    }
}

========================================================================
class A
{
    static int a = 10;
    public static void display() {
        System.out.println("A......" + a);
    }
}
class B extends A
{
    static int a = 15;
    {
        a = 20;
    }
    public static void display() {
        System.out.println("B......" + a);
    }
}
public class StaticDemo
{
    public static void main(String arg[]) {
        A a = new B();
        a.display();
    }
}
========================================================================
import static java.lang.System.out;
public class StaticImpDemo
{
    public static void main(String[] args)
    {
        out.println("Hello World!");
    }
}

========================================================================
import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.Date;

public class StaticImporsEg {
    public static void main(String[] args) {
       
        double circles = PI * 105;
        int absolutes = abs(-5800);

       
        out.println("Todays: " + new Dates());
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class  Swap
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter A and B values");
        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        System.out.println("A and B values before swap: " + a + "    " + b);
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("A and B values after swap: " + a + "    " + b);
        System.out.println("Hello World!");
    }
}

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

========================================================================
class VarArgsDemo
{
    static void varTest(int... v) {
        System.out.println("v length::: " + v.length);
    }
    public static void main(String[] args)
    {
        varTest(1,2,3,4);
        System.out.println("Hello World!");
    }
}

========================================================================
import java.util.ArrayList;
import java.util.List;
class Employee
{
    int _empId;
    String _eName;

    public Employee() {
        _empId = 70000;
        _eName = "Ltd";
    }
    public void setEmpId(int empId) {
        _empId = empId;
    }
    public int getEmpId() {
        return _empId;   
    }
    public void setEName(String eName) {
        _eName = eName;
    }
    public String getEName() {
        return _eName;
    }
    public String toString() {
        return "Employee Details\n\nId: " + _empId + "\n\nName: " + _eName;
    }
}
class  ArrarListDemo
{
    public static void main(String[] args)
    {
        List _map = new ArrayList();
        _map.add("Hai");
        _map.add(new Integer(1));
        _map.add(new Employee());
       System.out.println(_map.get(1));
       System.out.println(_map.get(2));
//       System.out.println(_map.get(0));
       System.out.println("Hello World!");
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class DateDemo {
    public static void main(String[] args)  throws IOException    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the date(dd-mm-yyyy)");
        String date = br.readLine();
        String[] datePart = date.split("-");
        int[] monthsDays = {31, 28, 31, 30, 31, 30, 31,31, 30, 31,30, 31};

        if (datePart.length < 3 || datePart.length > 3) {
            System.out.println("Date is invalid");
        } else {
            int oddDays = 0;
            int leapYears = 0;
            int ordinaryYears = 0;
            int day = Integer.parseInt(datePart[0]);
            int month = Integer.parseInt(datePart[1]);
            int year = Integer.parseInt(datePart[2]);
            if (year % 4 == 0 && month > 2)    {
                oddDays += 1;
            }
            year -= 1;
            while (year > 400)    {
                year -= 400;
            }
            while(year > 100) {
                year -=100;
                oddDays += 5;
            }
            if (year > 0)    {
                leapYears = year /4;
                ordinaryYears = year - leapYears;
                oddDays += leapYears * 2 + ordinaryYears * 1;
            }
            oddDays += day % 7;
            for (int index = 0; index < month - 1 ; index++ ) {
                oddDays += monthsDays[index] ;
            }
            oddDays %= 7;
            System.out.println("The date name is:");
            switch (oddDays) {
            case 0:
                System.out.println("Sunday");
                 break;
            case 1:
                System.out.println("Monday");
                 break;
            case 2:
                System.out.println("Tuesday");
                 break;
            case 3:
                System.out.println("Wednesday");
                 break;
            case 4:
                System.out.println("Thrusday");
                 break;
            case 5:
                System.out.println("Friday");
                 break;
            case 6:
                System.out.println("Saturday");
                 break;
            }
            System.out.println("Hello World!" + date);
/*            for (int index =0; index < datePart.length; index++) {
                System.out.println(datePart[index]);
            }*/   
        }
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Factorial
{
    public static void main(String[] args) throws IOException
    {
        int number,temp;
        System.out.println("Enter the number");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        number = Integer.parseInt(br.readLine());
        int fact = 1;
        temp = number;
        while (temp > 1) {
            fact *= temp--;
        }       
        System.out.println(number + " factorial is "+ fact);
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Febnocci
{
    public static void main(String[] args)  throws IOException
    {
        int _firstNumber = 0;
        int _secondNumber = 1;
        int temp;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the series length");
        String _seriesLength = br.readLine();
        int _size = 0;
        if (_seriesLength != null && _seriesLength.trim().length()>0)
        {
               _size = Integer.parseInt(_seriesLength);
               System.out.print(_firstNumber + "     "  + _secondNumber + "     ");
               for (int index = 0; index < _size - 2; index++ )
               {
                     temp = _firstNumber + _secondNumber;
                     _firstNumber = _secondNumber;
                     _secondNumber = temp;
                      System.out.print(temp + "     ");
               }
        } else {
              System.out.println("Series size is not valid");
        }

        System.out.println("Hello World!");
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
class FileRead
{
    public static void main(String[] args)  throws Exception
    {
        File _file = new File("G:\\Java Programs\\Test.java");
        BufferedReader br = new BufferedReader(new FileReader(_file));
        StringBuffer fileData = new StringBuffer();
        String data = "";
        try
        {
            while ((data = br.readLine()) != null)
            {
                    fileData.append(data);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
//         br.clsoe();
    //     _file.close();
        }
        System.out.println(fileData.toString() + "Hello World!");
    }
}

========================================================================
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

class FileReadAndWriter
{
    public static void main(String[] args) throws IOException
    {
        File readFile = null;
        File writeFile = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
       
        try
        {
        readFile = new File("G:\\Java Programs\\ArrayListDemo.java");
        writeFile = new File("G:\\Java Programs\\JavaFileWriteTest.txt");
        br = new BufferedReader(new FileReader(readFile));
        StringBuffer fileContent = readFileContent(br);
        bw = new BufferedWriter(new FileWriter(writeFile));
        boolean status = writeFileContent(bw, fileContent);           
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally{
        br.close();
        bw.close();
        }
        System.out.println("Hello World!");
    }
    public static StringBuffer readFileContent(BufferedReader br) throws IOException{
        StringBuffer data = new StringBuffer();
        String temp = null;
        while ((temp = br.readLine()) != null)
        {
            data.append(temp);
        }
        return data;
    }
    public static boolean writeFileContent(BufferedWriter bw, StringBuffer fileContent) throws IOException {
        boolean status = false;
        System.out.println(fileContent.toString());
        bw.write(fileContent.toString());
        bw.flush();
        status = true;
        return status;
    }
}

========================================================================

No comments:

Post a Comment