Wednesday 28 June 2017

Strings in Java

Strings in Java
===========================================================
String represents group of characters.

In C, C++ languages, a string represents an array of characters, where the last character being '\0' represents the end of the string. But this is not valid

in Java, a string is an object of String class.  It is not a character array.  In java, we got character array also, but strings are given a different treatment

because of their extensive use on internet.  JavaSoft people have created a class separately with the name 'String' in java.lang(language) package with

all necessary methods to work with strings.

Even though, String is a class, it is used often in the form of a data type
eg: String s="java";

Is String is class or datatype?

String is a class in java.lang package.   But in java, all classes are also considered as data types.  So we can take String as a data type also.

Can we call a class as datatype?

Yes, a class is also called 'user-defined' data type.  This is because a user can create a class.

Creating Strings:
-----------------------
1.String S;
S="Hello";

2.String S="Hello";

3.String S=new String("Hello");

Character Array( to String object):
--------------------------------------------
char arr[]={'A','P','T','E','C','H'};

String S=new String(arr);

String S=new String(arr,2,4);


Example:I
----------------

//Using charAt()
//It returns character at given index
class A56
{
public static void main(String args[])
{
//charAt()
String empname=new String();
empname = "aptech computers";
System.out.println(empname.charAt(10));
}
}

//output is---   p

---------------------------------------------------------------------------------
Example II
-------------------------
//Using concat()
//It is used to concatinate  two given strings

class A57
{
public static void main(String args[])
{
String empname=new String();
empname = "Aptech";
System.out.println(empname.concat(" Grafx"));


}
}

--------------------------------------------------------------------------------
Example III
-------------------------------
//Using compareTo()
//It returns the difference between two given strins based on ASCII values

class A58
{
public static void main(String args[])
{

String empname=new String();
empname = "Aptech";

System.out.println(empname.compareTo("aptech"));


}
}

--------------------------------------------------------------------------------
Example IV
------------------------------------
class StrDemo1
{
public static void main(String a[])
{
String S1=new String("Hello");
String S2=new String("hello");

//compareTo()
int S=S1.compareTo(S2);
if(S==0)
{
System.out.println("Both are Same..Difference is: "+S);
}
else
{
System.out.println("Both not are Same..The difference is: "+S);
}


//compareToIgnoreCase()
int SS=S1.compareToIgnoreCase(S2);
System.out.println("The Result of compareToIgnoreCase() is Both are Same: "+SS);

//indexOf()

int x=S1.indexOf('l');
System.out.println("Index position of 'l' is: "+x);



}
}
---------------------------------------------------------------------------
Example V
-----------------------------------

class StrDemo3
{
public static void main(String a[])
{
String S1=new String("Hello");
String S2=new String("   Hello World    ");
System.out.println(S1.replace('H','Y'));
System.out.print(S2);
System.out.println(S1);
System.out.print(S2.trim());
System.out.println(S1);

}

}

---------------------------------------------------------------------------------
Example VI
-------------------------------------------

//Copying String to Character Array using getChars() method


class Strcpy
{
public static void main(String a[])
{
String str="Aptech Computer Education";
char arr[]=new char[20];

// copy from string to arr
str.getChars(7,15,arr,0);

System.out.println(arr);
}
}

//output is --   computer

---------------------------------------------------------------------------------
Example VII
-------------------------------------------
//using equals() method and == operator

class StrCmp
{
public static void main(String a[])
{
String s1="Hello";
//String s2="Hello";

String s2=new String("Hello");

//if(s1==s2)
if(s1.equals(s2))
{
System.out.println("Both are Same");
}
else
{
System.out.println("Not Same");
}


}
}


/* object reference:
-------------------------
Object reference is a unique hexadecimal number representing the memory adress of the object.  It is useful to access the members of the object.

Difference between == and equals():
------------------------------------------------
== operator compares the references of the string objects.  It does not compare the contents of the objects.  equals() method compares the contents.

While comparing the strings, equals() method should be used as it yields the correct result.

String constant pool:
----------------------------
String constant pool is a separate block of memory where the string objects are held by JVM.  If string object is created directly, using assignment

operator as: String s1="Hello", then is is stored in string constant pool.



*/

---------------------------------------------------------------------------------
Example VIII
-------------------------------------------
//Using split()
//It Converts the string into pieces

class Strsplit
{
public static void main(String a[])
{
String str="Aptech Computer Education";
String s[];
s=str.split(" ");

for(int i=0; i<s.length; i++)
System.out.println(s[i]);
}
}

Output
-------------
Aptech
Computer
Education

---------------------------------------------------------------------------------
Example IX
-------------------------------------------
/*
Objects are divided into mutable and immutable.
Mutable Objects are those objects whose contents can be modified.

Immutable Objects are those objects, once created  can not be modified.

String class objects are immutable.*/

class StrImmut
{
public static void main(String a[])
{
String S1="Data";
String S2="Base";

S1=S1+S2;
System.out.println(S1);
}
}

---------------------------------------------------------------------------------
Example X
-------------------------------------------

/*
Differences between String and StringBuffer Class
------------------------------------------------------------------
String class objects are immutable, their contents cannot be modified.  StringBuffer class objects are mutable, so they can modified.
*/
class StrBuff
{
public static void main(String a[])
{
StringBuffer sb=new StringBuffer("Data");
System.out.println(sb);

//append()
System.out.println(sb.append("Base"));

//indexOf()
int m=sb.indexOf("a");
System.out.println("The index position of 'a' is "+m);

//lastIndexOf()
int n=sb.lastIndexOf("a");
System.out.println("The last index position of 'a' is "+n);

//length()
System.out.println("The Length is " + sb.length());


//delete()
System.out.println("After Deleting: " + sb.delete(0,4));


//reverse()
System.out.println("The Reverse is " + sb.reverse());

//replace()
StringBuffer sb1=new StringBuffer("High Cost");
System.out.println(sb1.replace(0,4, "Low"));

//substring()
System.out.println("Substring of sb1 is: "+sb1.substring(4));

System.out.println("Substring of sb1 is: "+sb1.substring(0,3));
}
}

---------------------------------------------------------------------------------
Example XI
-------------------------------------------
//Palindrome or not

import java.io.*;

class StrBuff1
{
public static void main(String a[])throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String: ");
String str=br.readLine();
String temp=str;
StringBuffer sb=new StringBuffer(str);
sb.reverse();
System.out.println(sb);
str=sb.toString();
if(temp.equalsIgnoreCase(str))
{
System.out.println(temp+" is Palindrome");
}
else
{
System.out.println(temp+" is not a Palindorme");
}
}
}

---------------------------------------------------------------------------------
Example XII
-------------------------------------------
import java.io.*;

class StrBuff
{
public static void main(String a[]) throws Exception
{
StringBuffer sb=new StringBuffer();

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Surname: ");
String sur=br.readLine();
System.out.print("Enter middle name: ");
String mid=br.readLine();
System.out.print("Enter Last Name: ");
String last=br.readLine();

//append
sb.append(sur +" ");
sb.append(last+ " ");
System.out.println("Name: "+sb);

//insert
int n=sur.length();
sb.insert(n,mid+" ");

System.out.println("Full Name: "+sb);
System.out.println(" In reverse: "+sb.reverse());



}
}

Tuesday 13 June 2017

Object Oriented Programming

OBJECT ORIENTED PROGRAMMING SYSTEM
=================================================

OOPS:- Object Oriented Programming Language
---------------------------------------------------------------
It is very important  concept in Java  Language.  OOPS have three parts based on their process.

a.OOD:-Object Oriented Designing
b.OOA:-Object Oriented Approach
c.OOP:-Object Oriented Programming
The combination of above three parts are called "OOPS".

Above all process is used to develop any application systematically by following set of predefined standards.  All the standards comes under "Unified Modiling Language"(UML)

a.Object Oriented Designing(OOD):-
---------------------------------------------
It stands for object oriented design.  It determines to develop the application by phasewise designing of each module and each module will be designed later by following set of methods, principles and applications.

b.Object Oriented Approach(OOA):-
----------------------------------------------
It follows the object oriented designing based on its (OOD) phase.

c.Object Oriented Programming(OOP):-
--------------------------------------------------
It determines the real implementation of program(coding) by following set of functions libraries,header files and other technical tools by performing a suitable syntaxes.  Any real implementation of the program in OOP which follows a standard structure of program.

In Java we have a standard structure of program.  If you write program we should clear about object.

* Standard Structure of Program in OOPS:-

class
Object
objective
1.Data Members
2.Member Functions



Class:-
In a Java program the "class"  keyword denotes that the Java program follows this standard of class structure.
(or)
Class is nothing but collection of data and functions.
(or)
Collection of data members and member functions.



2.Objective:-
It is a invisible and it is the end result of an application which can be achieved using data members and member functions. Objective means end result of the programme.

3.Data Members:-
Data members are the elements which can be participated in a prog to achieve the objective of an application.

4.Member Function:-
It is a process of a technic in a program which can take a parameter and returns a value.  Member function can be called methods in OOPS Programming.

5.Object:-
It is a instance of a class which can be gives the reference to its.  The data member and the member functions which can be used outside of the class.
(or)
Object is nothing but instance of class.

Datatypes:-
Datatypes which are denotes what type of data that your processing to our program.

Variables:-
It is a location memory where we can store the values into the memory those values in the memory called "constants".

   OOPS:- Object Oriented Programming System
--------------------------------------------------------------
1.Encapsulation
2.Data Abstraction
3.Polymorphism
4.Inheritance

1.Data Encapsulation:-
The union of data and functions into objects is known as Data Encapsulation.
(or)

Details of what a class contains need not be visible to other classes and objects that use it.  Instead, only specific information can be made visible and the others can be hidden.  This is achieved through encapsulation, also called data hiding.  Both abstraction and encapsulation are complementary to each other.

Data Abstraction:-
-----------------------
We can give restricting the data, to set a number of functions.
(or)
It gives security.
(or)

Abstraction is the feature of extracting only the required information from objects.  For example, consider a television as an object.  It has a manual stating how to use the television.  However, this manual does not show all the technical details of television, thus giving only an abstraction to the user.

Polymorphism:-
-------------------
It is ability to take more than one form.
(or)
Use of something for different purposes.
(or)

Polymorphism is the ability to behave differently in different situations.  It is basically seen in programs where you have multiple method declared with the same name but with diferent parameters and different behavior.

Inheritance:-
----------------
Relation between two classes.
(or)
Object of one class can acquire the properties of  another class.
(or)

Inheritance is the process of creating a new class based on the attributes and methods of an existing class.  The existing class is called the base class whereas the new calss created is called the derived class.  This is very important concept of objet-oriented programming as it helps to reuse the inherited attributes and methods.

Super Class:-
---------------
It is a old class or parent class
Sub Class:-
--------------------
It is a new class or child class which can acquire the properties from base class.



PHP Notes

The Characteristics of PHP:- ----------------------------------- 1.PHP is a high level programming language. 2.It is a server-side scrip...