Wednesday 21 December 2016

Object Oriented Programming

Object Oriented Programming (OOP) is a programming technique in which programs are written on the basis of objects. Object Oriented Programming is a powerful technique to develop software. It is used to analyze and design the applications in terms of objects. It deals with data and the procedures that processes the data as a single object. Object Oriented programming is easier to learn and  modify.

Features of Object Oriented Programming 

Following are the features of the object-oriented programming:

Objects: 

Object is an entity that consists of data and functions. OOP provides the facility of programming based on objects.

Classes: 

Classes are design of creating objects. OOP provides the facility to design classes for creating different object. All properties and functions of an object are specified in classes.

Reuasability: 

OOP provides the facility of reusing the data and code. Inheritance is a technique that allows a programmer to use the code of existing program to create a new program.

Polymorphism: 

Polymorphism is an ability of an object to behave in multiple ways.e.g Operator Overloading, Function Overloading.

Real-World Modeling: 

OOP is based on real-world modeling. As in real world, things have properties and working capabilities. Similarly, objects have data and functions. Data represents properties and functions represents working of objects.

Information Hiding: 

OOP allows the programmer to hide important data from the user. It is performed by  encapsulation.

Dynamic Binding: 

Refers to linking of function call with function definition is called binding and when it is  take place at run time called dynamic binding.

Message Passing: 

The process by which one object can interact with other object is called message passing.


Monday 19 December 2016

Difference between Method Overloading and Method Overriding

Method Overloading: 

  • If a class have multiple methods by same name but different parameters(method signature), it is known as Method Overloading
  • Overloading happens at compile-time. The binding of overloaded method call to its definition has happens at compile-time.
  • Static methods can overload which means that a class can have more than one static method of the same name. 
  • Overloading is being done in the same class. 
  • Static binding is used for overloaded methods. 
  • Overloading gives better performance as compared to overriding, the reason is that the binding of overridden methods  is being done at runtime. 
  • Private and final methods can be overloaded. It means a class can have more than one private or final methods of same name.
  • Return type of methods doesn't matter in case of overloading, it can be same or different but argument list should be different.  
  • Java Method overloading example 
                    public  class OverloadingExample {
                           public int sum (int a, int b){
                                    return a+b;
                           }
                          public int sum (int a, int b, int c){
                                     return a+b+c;
                          }
                    }

Method Overriding:

  •  If a subclass (child class) has the same method as declared in the parent class, it is known as method overriding
  • Overriding happens at runtime. The binding of overridden method call to its definition happens at runtime. 
  • Static methods can't be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class. 
  • Overriding is all about  giving a specific implementation to the inherited method of parent class. 
  • Dynamic binding is used for  overridden methods. 
  • A child class cannot override the private and final methods of their base class. 
  • Overridden methods should be more specific return type  and argument list should be same. 
  • Java Method overriding example: 
                    class Animal{
                        void eat(){
                             System.out.println("Eating"); 
                          }
                    }

                    public class Cat extends Animal{
                          void eat(){
                              System.out.println("Eating Meat"); 
                           }
                    }

Wednesday 14 December 2016

Write a Program that inputs a decimal number and convert it to binary digits.



        #include <iostream.h>
        #include <conio.h>

         void binary(unsigned long int decimal)
         {
               int remainder; 
               if (decimal <= 1)
               {
                  cout<< decimal;
                  return
               }
               remainder = decimal % 2
               binary(decimal >> 1);
               cout<<  reminder; 
         }
         
          int main()
         {
               unsigned long int num; 
               clrscr(); 
               cout<< "Enter decimal"
               cin>> num; 
               cout<< endl<< "Binary of  "<< num<< "is " ;
               binary(num);
                return 0;
         }

Output:


Functions in C++

A function is a named block of code that performs some action.The statements are written in a function are executed when it is called by its name. Each function has a unique name.Functions are the building blocks of C++ programs. Functions are  easier to write, modify and maintain. Functions can be reused and reduce programming time. The encapsulate piece of code to perform specific task again and again without writing the same code again. They are used to perform tasks that are repeated many times.
The functions provide a structured programming approach. It is modular way of  writing programs. The whole program logic is  divided into number of smaller modules or functions. The main function calls these functions when they are needed to execute.

Importance of functions:

A program may need to repeat the same piece of code at various places. It may be required to perform certain tasks repeatedly. The program may become very large if  functions are not used. The piece of code that is executed repeatedly is stored in a separate function. The real reason of using functions is to divide a program into different parts, These parts of a program can be managed easily.

Advantages of Functions:

Some advantages of functions are :

1. Easier to Modify:

Each function has a unique name is written as an independent block. If there is any error in the program, the change is made to particular function in which error occurs. A small function is easier to modify than a large program.

2. Easy to Code:

A  lengthy program can be divided into small functions, It is easy to write small functions instead of writing one long program. A function is written to solve a particular problem. It makes programming easy.

3. Easier to Maintain:

Functions are easier to maintain than one long program. Each function contains independent code. A change in the function does not affect other parts of program.

4. Less Programming Time:

A program may consist of many functions. These functions are written as independent program. Different programmers can work on different functions at the same time. It takes far less time to complete the program.

5. Reusability:

The code written in functions can be reused as when required . A function but can be executed many times. A functions is written to solve a particular problem. Whenever that problem has to be solved, the function can be executed.



Thursday 8 December 2016

Structures


A structure is a collection of multiple data types that can be referenced with a single name. It may contain similar or different data  types. The data elements in a structures are called structure elements, members or fields. The structures are used to join simple variables together to form complex variable.

The difference between array and structure is the array consist of a set of variables of same data type , and structure may consist of a set of variables of different data types.

Uses of Structure:

The structures is used to define new data types. The user can define a new data type by combining multiple data types in a structure. A simple variable contain single value at a time, but a structure can stores multiple values at the same time.

Declaring a Structure:

A structure is declared by using struct keyword followed by the structure name. The structure elements are defined  with their type inside the braces. A semi-colon is added after the closing braces in structure.Structure declaration is terminated by the semi-colon(;).The declaration tells the compiler about the details of structure. The compiler doesn't allocate any memory at that time.
Syntax:
 struct Structure_Name
{
     Data_Type identifier1;
     Data_Type identifier2;
     Data_Type identifier3;
                   :
                   :
     Data_Type identifierN;
};

Example:
  struct Student
 {
       int rollNo, marks;
       float Percentage;
       char Grade;
 };

The above structure is declared a new data type named as Student, which contain four member variables. This Student structure can store four values at a time.

Defining Structure Variable:

The process of defining structure variable is same as defining primitive data types such as int, char, float. The definition tells the compiler to allocate memory space for the variable.The compiler automatically allocates sufficient memory according to the elements of the structure.
Syntax:
Structure_Name identifier;
Structure_Name: is the name of the structure used in structure declaration.
Identifier: is the name of the variable to be defined.
Example:
Student Ahmed;

In the above example Ahmed define the structure variable(Student). The variable contain four member variables defined in Student structure. The statement allocates the memory space for all four members of the structure(Student).
The structure variable Ahmed  will occupy 9 bytes in the memory. The member variable rollNo occupies 2 bytes, marks occupies 2 bytes, percentage occupies 4 bytes and variable Grade occupies 1 byte in memory. Total memory occupied by the variable of type Student will be 9 bytes.

Accessing Member of Structure Variable:

Any member of  a  structure variable can be accessed by using dot operator. The name of the structure variable is written on the left side of the dot, and name of the structure member is written on the right side of the dot operator. The dot operator is also known as member access operator.
Syntax:
Structure_Variable.Memeber_Variable;
Example:
 Student Ahmed;
 Ahmed.rollNo = 01;
 Ahmed.marks =  885;
 Ahmed.Percentage = 80.45;
 Ahmed.Grade = 'A';

The above example creates a structure variable Ahmed of type Student . It use four assignment statement to store values in member variable of Ahmed. The same method is used  for input values in member variable and to display them.
Example:
cin>>Ahmed.rollNo;
cin>>Ahmed.marks;
cin>>Ahmed.Percentage;
cin>>Ahmed.Grade;

cout<<Ahmed.rollNo;
cout<<Ahmed.marks;
cout<<Ahmed.Percentage;
cout<<Ahmed.Grade;

Array of Structure:

An array of structure is a type of array in which each element contains a complete structure. It can be used to store many records.
Example:
struct stud{
   char id[6];
   char name[50];
   char gender;
   int age;
};
stud s[3];

The above example declares a structure  stud. It define an array of structures s[3]. The array can store the record of three students.


The array is accessed  by using its index. The structure is  accessed by using dot operator.

Nested Structure:

A structure within a structure is known as nested structure. A nested structure is created when the member of a structure is itself a structure. 
Example:
struct A 
{
    int n; 
    float b;
};
struct B 
{
    char c; 
    A x;
};

The above example declares two structures A and B. The structure A contains simple member variables n and b. The structure B also contains two member variables. The first member is character variable c and the second member is a structure variable x. It is an example of a nested structure. 

Write a program that uses three structures Dimension, Result and Rectangle. The  Dimension structure stores length and width, Result structure stores area and perimeter and Rectangle stores two variables of Dimension and Results. The program declares a variable type Rectangle input length, width, calculate area and perimeter and then display the result.


        #include <iostream.h>
        #include<iomanip.h>
        #include <conio.h>
               
         struct Dimension 
         {
                  double length; 
                  double width: 
         };
         
         struct Result
         {
                  double area; 
                  double perimeter;
         };

         struct Rectangle 
         {
                  Dimension size; 
                  Result stat; 
         };

         int  main ()
         {
                  Rectangle box;
                  cout<< "Enter the length of  a rectangle: " ;
                  cin>>box.size.length;

                  cout<< "Enter the width of  a rectangle: " ;
                  cin>>box.size.width;
                 
                  box.stat.area = box.size.length * box.size.width;
                  box.stat.perimeter =  2 *  box.size.length + 2 *  box.size.width;

                  cout.setf(ios::fixed, ios::floatfield);
                  cout.setf(ios::showpoint);
                  cout<<setprecision(2);

                 cout<< "The area of rectangle is : "  << box.stat.area <<endl;
                 cout<< "The perimeter of rectangle is : "<< box.stat.perimeter << endl;

                 return 0;
         }

Programs:

1.   Write a C++ program that declares a structure to store the distance covered by an player along with the minutes and seconds taken to cover the distance. The program should input the records of two players and then display the record of the winner.

2.  Write a C++ program that declares a structure to store the code number, salary and grade of the employee. The program defines two structure variables, inputs records of two employees and then displays the record of the employee with more salary.

3.  Write a C++ program that declares a structure to store income, tax rate and tax of the person. The program defines an array of structure to store the record of the five persons. It input income and tax rate of five persons and then displays the tax payable.

4.  Write a C++ program that declares a structure Book  to store bookId , Book name and price, it declares another structure Order that contains OrderId and an array of Book of length 5. The program should define a variable of type Order and input the values from the user. The program finally display the values.

Write a program that declares a structure to store the code number, salary and grade of an employee....


Write a program that declares a structure to store the code number, salary and grade of an employee. the program defines two structure variables, inputs records of two employees and the displays the record of the employee with more salary.


        #include <iostream.h>
        #include <conio.h>
         struct employee
        {
            int code_num;
            int salary;
            int grade;
        };

        int main()
        {
            employee e1, e2;
             cout<<" Enter the code number, salary and grade of the first employee:";
             cin>>e1.code_num>>e1.salary>>e1.grade;
             cout<<"Enter the code number, salary and grade of the second employee:";
             cin>>e2.code_num>>e2.salary>>e2.grade;
             cout<<" The Employee with more salary is: "<<endl;

             if (e1.salary > e2.salary)
            {
                   cout<<" Code Number "<<e1.code_num<<endl;
                   cout<<" Salary "<<e1.salary<<endl;
                   cout<< " Grade "<<e1.grade;
            }
            else
            {
                   cout<< " Code Number "<<e2.code_num<<endl;
                   cout<<" Salary "<<e2.salary<<endl;
                   cout<<" Grade "<<e2.grade;
            }
            return 0;
       

Output:


Write a program that declares a structure to store the distance covered by an player along....


Write a program that declares a structure to store the distance covered by an player along with the minutes and seconds taken to cover the distance. The program should input the records of two players and then display the record of the winner. 


        #include <iostream.h>
        #include <conio.h>
        struct player
        {
             int dis;
             int min;
             int sec;
         };

        int  main()
        {
            player p1,p2;
            float t1, t2;
           
             cout<<" Enter distance covered the first player: ";
             cin>>p1.dis;
             cout<<" Enter minutes and seconds covered by the player: ";
             cin>>p1.min>>p1.sec;
             cout<<" Enter the distance covered the second player: ";
             cin>>p2.dis;
             cout<<"Enter minutes and seconds covered by the player: ";
             cin>>p2.min>>p2.sec;

            float time1=(p1.min*60+p1.sec)/p1.dis;
            float time2=(p2.min*60+p2.sec)/p2.dis;
           if(time1<time2)
          {
           cout<<"Player 1 distance: "<<p1.dis<<" miles in "<<p1.min<< " minutes "<<p1.sec<<"  seconds."<<endl;
          }
         else
         {
           cout<<" Player 2 distance: "<<p2.dis<<" miles in "<<p2.min<< " minutes "<<p2.sec<<"  seconds."<<endl;
          }
          return 0;
        }

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...