Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, 6 January 2017

TO CHECK WHETHER TWO STRINGS ARE ANAGRAM IN JAVA

1. when total number of alphabets in both strings are same.

2. when all the alphabets of 1st string are present in 2nd string , here alphabets are same but their sequence can varry.

For Example :

"base " & "seba " are  anagram as no. of alphabets are 4 in both strings & alphabets are also same.

 
PROGRAM EXAMPLE IN JAVA :


import java.util.*;

public class der {
public static boolean IsAnagram(String arg1,String arg2){
char a[] = arg1.toCharArray();
char b[] = arg2.toCharArray();
int index[] = new int[a.length];
int c=0;
while(c<index.length){
index[c] =0;
c++;
}
int i = 0;
while(i<arg1.length()){
int j=0 , check =0;
while(j<arg2.length()){
if(a[i]==b[j]&&index[j]==0){
index[j]=1;
check=1;
break;
}j++;
}
if(check==0){
return false;
}
i++;
}
return true;
}

public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.println("enter two strings");
String a=inp.next();
String b = inp.next();
 
boolean o =IsAnagram(a,b);
if(o){
System.out.println(a+" is anagram  with "+b);
}
else{
System.out.println("not an anagram");
}
}

}


OUTPUT :

enter two strings
base
seba
base is anagram  with seba








Saturday, 17 December 2016

USE OF FINAL KEYWORD IN JAVA

      Its 3 uses are :                                          


1.To prevent method overriding

2.To prevent overloading

3. To declare final variable
______________________________________________________

Examples :

1.       public class base {
                final void get(){
                                    System.out.println("get called");
                                        }

                             }


public class der extends base{

void get(){                          //error , cannot override final method from base//
}
public static void main(String[] args) {
}

}

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

2.              final public class base {
                                      }

public class der extends base{            // the type final base class can't have der as subclass//

public static void main(String[] args) { 
}

}

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

3.    *  to declare a final variable in main use this  :    final int sum =100

       * note : value declaration is necessary for final constant variable
      
        * to declare a final variable out of class (can be accessed without creating object) and visible to              every class in package use this :  
                                                                public static final int sum = 200;






Saturday, 5 November 2016

DOWNLOAD BOOK FOR JAVA

Complete pdf book download for Computer Science Students                                                                                            (BE, B.TECH,MCA,BCA,PGDCA,AND OTHER COMPUTER ORIENTED SUBJECTS)

Java™
The Complete Reference,
Seventh Edition               BY Herbert Schildt

FEATURES OF THIS BOOK

*  Covering all academic material for study purpose
* Simple programs and hundreds of examples
* easy to understand language

download from the link given below from high speed google server:



Tuesday, 1 November 2016

SUPER KEYWORD IN JAVA

Usage of java super Keyword
  1. super is used to refer  parent class instance variable.        in 1st example
  2. super() is used to invoke parent class constructor.      using super(); in 2nd example
  3. super is used to invoke parent class method. using super.methodname();in 2nd example.

1st example

class Vehicle{                                     
int speed = 50;

class bike extends vehicle{
int speed  = 100;
void display(){
System.out.println(super.speed);// it will print speed of vehicle
}
}

public static void main(String args[]){
bike b1 = new bike();
b1.display();
}

}

OUTPUT :
50

2nd example

public class base {

base(){
System.out.println("constructor of base !");
}

void eat(){
System.out.println(" base is eating !");
}

}

--

public class der extends base {

der(){
super(); // super(); call constructor of base class
System.out.println("constructor of derieved ");
}
void eat(){
super.eat();// super.methodname call method of base class//
System.out.println(" 2nd  derieved is eating !");
}
public static void main(String[] args) {
der d1 = new der();
d1.eat();
}
}

OUTPUT :



QUICKSORT IN JAVA

Diagram explaination and example is below

Program : 


import java.util.Arrays;

public class base {
public static void main(String[] args) {
int[] x = { 9, 2, 4, 7, 3, 7, 10 };
System.out.println(Arrays.toString(x));

int low = 0;
int high = x.length - 1;

quickSort(x, low, high);
System.out.println(Arrays.toString(x));
}

public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;

if (low >= high)
return;

// pick the pivot
int middle = low + (high - low) / 2;
int pivot = arr[middle];

// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}

while (arr[j] > pivot) {
j--;
}

if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}

// recursively sort two sub parts
if (low < j)
quickSort(arr, low, j);

if (high > i)
quickSort(arr, i, high);
}
}

OUTPUT :













Sunday, 30 October 2016

NESTED AND INNER CLASSES

NESTED CLASS : CLASS CREATED WITHIN A CLASS

PROGRAM FOR NESTED CLASS

public class outer {
      int x =10;
     
      void fun(){
            inner i1 = new inner();
            i1.display();
      }
     
     
      class inner{
            int y =200;
           
            void display(){
                  System.out.println("x = "+x);
            }
      }
     
      void showy(){
            System.out.println("y ="+y);     // error //
      }

      public static void main(String[] args) {
           
            outer o1 = new outer();
            o1.fun();
           

      }

}
OUTPUT : X=10

BUT Y CAN'T BE ACCESSED AS OUTER CLASS CANNOT ACCESS INNER CLASS DE VARIABLES


ADVANTAGES OF NESTED CLASSES : 

1. HERE, INNER CLASS CAN ACCESS ALL MEMBERS OF OUTER CLASS INCLUDING OUTER                                                           CLASS VARIABLES AND METHODS.
2. IT MAKES CODE MORE READABLE
3. IT MAKES CODE MORE MAINTAINABLE
4. CODE OPTIMIZATION : MEANS IT NEED LESS CODE TO WRITE


DIFFERENCE BETWEEN INNER CLASS AND NESTED CLASS

* NESTED CLASS CAN BE STATIC AS WELL AS NON - STATIC.BUT INNER CLASS IS ALWAYS NON - STATIC

TYPES OF INNER CLASSES:  3 TYPES

1. MEMBER INNER CLASS : CLASS CREATED WITHIN CLASS AND OUTSIDE OUTER CLASS        METHOD.

2.ANNONYMOUS INNER CLASS : CLASS CREATED FOR IMPLEMENTING INTERFACE OR EXTENDING CLASSES


3. LOCAL INNER CLASS : INNER  CLASS CREATED WITHIN  OUTER  CLASS METHOD

LOCAL INNER CLASSES IN JAVA

WHAT IS LOCAL INNER CLASS:         CLASS CREATED WITHIN A METHOD OF CLASS

PROGRAM TO ILLUSTRATE CONCEPT OF LOCAL INNER CLASSES

public class outer {

      int x =100;
     
      void get(){
           
            class inner{
                 
                  void display(){
                        System.out.println("x = "+x);
                  }
            }
            inner i1 = new inner();
            i1.display();
           
           
      }

      public static void main(String[] args) {
           
            outer o1 = new outer();
            o1.get();

      }

}


OUTPUT : x = 100

DATATYPES IN JAVA

There are two data types available in Java −
·        Primitive Data Types
·        Reference/Object Data Types
Primitive Data Types

 
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.

byte

·        Byte data type is an 8-bit signed two's complement integer
·        Minimum value is -128
·        Maximum value is 127
·        Default value is 0
·        Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
·        Example: byte a = 100, byte b = -50
Short

·        Short data type is a 16-bit signed two's complement integer
·        Minimum value is -32,768
·        Maximum value is 32,767 (inclusive)
·        Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer

·        Default value is 0.
·        Example: short s = 10000, short r = -20000
int
·        Int data type is a 32-bit signed two's complement integer.
·        Minimum value is - 2,147,483,648
·        Maximum value is 2,147,483,647(inclusive)
·        Integer is generally used as the default data type for integral values unless there is a concern about memory.
·        The default value is 0
·        Example: int a = 100000, int b = -200000
long
·        Long data type is a 64-bit signed two's complement integer
·        Minimum value is -9,223,372,036,854,775,808
·        Maximum value is 9,223,372,036,854,775,807 (inclusive)
·        This type is used when a wider range than int is needed
·        Default value is 0L
·        Example: long a = 100000L, long b = -200000L
float
·        Float data type is a single-precision 32-bit  floating point
·        Float is mainly used to save memory in large arrays of floating point numbers
·        Default value is 0.0f
·        Float data type is never used for precise values such as currency
·        Example: float f1 = 234.5f
double
·        double data type is a double-precision 64-bit  floating point
·        This data type is generally used as the default data type for decimal values, generally the default choice
·        Double data type should never be used for precise values such as currency
·        Default value is 0.0d
·        Example: double d1 = 123.4
boolean
·        boolean data type represents one bit of information
·        There are only two possible values: true and false
·        This data type is used for simple flags that track true/false conditions
·        Default value is false
·        Example: boolean one = true
char
·        char data type is a single 16-bit Unicode character
·        Minimum value is '\u0000' (or 0)
·        Maximum value is '\uffff' (or 65,535 inclusive)
·        Char data type is used to store any character
·        Example: char letterA = 'A'
Reference Datatypes
·        Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy, etc.
·        Class objects and various type of array variables come under reference datatype.
·        Default value of any reference variable is null.
·        A reference variable can be used to refer any object of the declared type or any compatible type.
·        Example: Animal animal = new Animal("giraffe");
Java Literals
A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.
Literals can be assigned to any primitive type variable. For example −
byte a = 68;
char a = 'A'
byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well.
Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For example 
String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a pair of double quotes.
Java language supports few special escape sequences for String and char literals as well. They are −
Notation
Character represented
\n
Newline 
\r
Carriage return
\f
Formfeed
\b
Backspace
\s
Space
\t
tab
\"
Double quote
\'
Single quote
\\
backslash
\ddd
Octal character 
\uxxxx
Hexadecimal UNICODE character