Monday 5 September 2011

Class Demo in java

class Testing
{
    private int x;

    public void setX (int x) // setter method
    {
        this.x = x;
    }
   
    public int getX()  // getter method
    {
        return this.x;
    }
}



public class TestingMain
{
    public static void main(String[] args)
    {
        Testing testing = new Testing();

            testing.setX(100);
           
        int x = testing.getX();
       
       
        System.out.println("X is : "  + x);
           
       
        System.out.println("X is : " + testing.getX());
       
       
       
       
    }
}

POJO Class in java

 /*here employee is POJO class
POJO = "Plain Old Java Object"
POJO class contain only getter and setter methods*/

class Employee 
{
    private int empid;
    private String empname;
    private float empsalary;
   
    public int getEmpid()
    {
        return empid;
    }
   
    public void setEmpid(int empid)
    {
        this.empid = empid;
    }
   
    public String getEmpname()
    {
        return empname;
    }
    public void setEmpname(String empname)
    {
        this.empname = empname;
    }
    public float getEmpsalary()
    {
        return empsalary;
    }
    public void setEmpsalary(float empsalary)
    {
        this.empsalary = empsalary;
    }
}

public class EmployeeMain
{
    public static void main(String[] args)
    {
        Employee employee1 = new Employee();
        Employee employee2 = new Employee();
        Employee employee3 = new Employee();
       
       
        employee1.setEmpid(10001);
        employee1.setEmpname("Simran Singh");
        employee1.setEmpsalary(30000);
       
       
       
        System.out.println("Id          : " + employee1.getEmpid());
        System.out.println("Name        : " + employee1.getEmpname());
        System.out.println("Salary      : " + employee1.getEmpsalary());
       
        System.out.println("\n");
       
        employee2.setEmpid(10002);
        employee2.setEmpname("Rajat");
        employee2.setEmpsalary(30000);
       
       
        System.out.println("Id          : " + employee2.getEmpid());
        System.out.println("Name        : " + employee2.getEmpname());
        System.out.println("Salary      : " + employee2.getEmpsalary());
       
       
        System.out.println("\n");
       
        employee3.setEmpid(10003);
        employee3.setEmpname("Raghav");
        employee3.setEmpsalary(30000);
       
       
       
        System.out.println("Id          : " + employee3.getEmpid());
        System.out.println("Name        : " + employee3.getEmpname());
        System.out.println("Salary      : " + employee3.getEmpsalary());
       
       
       
       
    }
}

Nested Loop - to print a triangle pattern of star in c++


public class NestedLoop {
    public static void main(String[] args) {
        int i,j;
        for(i=0;i<10;i++){
            for(j=0;j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

Loop Demo - to print 1 to 10 on screen in c++

public class LoopDemo {
    public static void main(String[] args) {
        int i;
        for(i=1;i<=10;i++){
            System.out.println(i);
        }
    }
}

Use of getClass() in java

class MyClass extends Object
{
    public void showClassName()
    {
        System.out.println("Class is : " + getClass());
    }
}


public class UseMethods extends MyClass
{
    public void callMethods()
    {
        System.out.println("Class is : " + getClass());
    }
   
    public static void main(String[] args)
    {
            new UseMethods().callMethods();
     }
}

What is Inheritance in java

Inheritence
------------

    is process to create a new class from an existing class.
   
    existing class is called super class
    and
    new class is called derived class.
   
    new class inherits properties of super class.
   
    (in java main class is called super class)
   
    in java Object is the super class of all java classes.
   
    extends is a keyword to extend the class.
   
    class A    Object is super class.   bydefault
    {}
   
   
    OR
   
    class A extends Object
    {}
   
    in java any class can inherit the properties of Object class
    directly without creating the object.
   
    this class is defined in   java.lang.Object package.
   
    only derived classes can inherits the protected properties
    of super class.
   
   
    super
    -----
   
    is a keyword is used to access properties
    of super class.
   
   
   

Inheritance in a Company - Manager-Employee Inheritance in java

   
class Employee
{
    protected float salary;
   
    private String bname = "Vee Bank";
       
    public void showBankName()
    {
        System.out.println(bname);
    }
}

class Manager extends Employee
{
    public void setSalary(float salary)
    {
        super.salary = salary;
    }
   
    public float getSalary()
    {
        return super.salary;
       
    }
}


public class CompanyMain
{
    public static void main(String[] args)
    {
            Manager manager = new Manager();
                manager.setSalary(40000);
                manager.showBankName();
                System.out.println("Manager Salary : " + manager.getSalary());
    }
}

Inheritance Demo 2 in java

class A
{
    public void aShow()
    {
        System.out.println("A Show");
    }
}

class B extends A
{
    public void bShow()
    {
        aShow();
        System.out.println("B Show");
    }
}


public class ClassCaller
{
    public static void main(String[] args)
    {
            new B().bShow();
    }
}

Inheritance Demo in java

 class AClass
 {
         public void aCaller()
         {
             System.out.println("Class : " + getClass());
         }
 }

 class BClass
 {
         public void bCaller()
         {
             System.out.println("Class : " + getClass());
         }
 }



public class Demo extends java.lang.Object
{
    public void caller()
    {
        System.out.println("Class : " + getClass());
    }
   
    public static void main(String[] args)
    {
            Demo demo = new Demo();
                demo.caller();
   
                new AClass().aCaller();
                new BClass().bCaller();
               
   
    }
}

2D Array Testing in java

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class TwoDTesting
{
    public static void main(String[] args) throws Exception
    {
            final int ROWS = 2;
            final int COLS = 2;
            int arr[][] = new int[ROWS][COLS];
            int r;
            int c;
            BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
           
           
            System.out.println("Set Values\n");
           
            for(r=0; r<ROWS; r++)
            {
                for(c=0; c<COLS; c++)
                {
                    arr[r][c] = Integer.parseInt(buf.readLine());
                }
               
                System.out.println();
            }
           
        System.out.println("Set Values\n");
           
            for(r=0; r<ROWS; r++)
            {
                for(c=0; c<COLS; c++)
                {
                    System.out.print("\t"+arr[r][c]);
                }
               
               
                System.out.println();
            }
           
           
     }
}

Integer Array Testing in java

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class IntegerArrayTesting
{
    public static void main(String[] args) throws Exception
    {
        int arr[]=new int[3];
        int x;
       
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
       
        System.out.println("Enter Values\n");
       
        for(x=0; x<3; x++)
        {
            arr[x] = Integer.parseInt(buffer.readLine());
        }
       
        System.out.println("Display Values\n");
       
        for(x=0; x<3; x++)
        {
            System.out.println(arr[x]);
        }
    }
}

Sunday 4 September 2011

What is C++



 History
C++ (pronounced "see plus plus") is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C language. Originally named C with Classes, the language was later renamed C++ in 1983.


What is C++
C++ is one of the most popular programming languages with application domains including systems software (such as Microsoft Windows), application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Embarcadero Technologies. C++ has greatly influenced many other popular programming languages, most notably C# and Java.


Key Features
·        C++ is an "object oriented" programming language.
·        C++ adds a concept called "operator overloading" not seen in the earlier OOP languages and it makes the creation of libraries much cleaner.
·        C++ maintains aspects of the C programming language, yet has features which simplify memory management.
·        C++ could be considered a superset of C.

What is java

What is java?
Java is a programming language originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simplerobject model.

What is the purpose to develop java?
Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere." Java is currently one of the most popular programming languages in use, particularly for client-server web applications.You can use Java to write computer applications that crunch numbers, process words, play games, store data or do any of the thousands of other things computer software can do.

Comparision with c and c++
Compared to other programming languages, Java is most similar to C. However although Java shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++, will certainly help you to learn Java more quickly, but you don't need to know C to learn Java. Unlike C++ Java is not a superset of C. A Java compiler won't compile C code, and most large C programs need to be changed substantially before they can become Java programs.

It should not to be confused with JavaScript.