Global variables, this keyword and constructor in java

return method

Return method should be the last statement, In method definition if we give any statement after return statement we will get “Unreachable Code” Compile time error.

Private Class

The private access modifier is specified using the keyword private.

  • The methods or data members declared as private are accessible only within the class in which they are declared.

Can we have private class?

No, we cannot have class no private.

why cant we have private class?

No other class can access it.

default – Package level access

having default access modifier are accessible only within the same package.

Only within the same package we can access. any variable/object or method. outside the package we cannot access.

Difference Between Private and Default

Private – Class level access.

default – Package level access.

Public Class

The public access modifier is specified using the keyword public.

Classes, methods or data members which are declared as public are accessible from every where in the program

public is an access modifier. this can be given for variables,objects,methods and classes.

if we declare anything (above) as public, it can be accessed from any where.

Constructor

To stored object specific information

Constructor is called automatically, when the object is created

Rule

It has the same name of our class name

No return type

It looks like method, but not. It does not any return data type

It looks like class, but not. because no class keyword is attached.

There are two type of constructor in Java:

1. Default constructor

A constructor that has no parameter is known as default constructor.

It is invisible.

(e.g) Box pencil = new Box();

2. Parameterized Constructor

A constructor that has parameters is known as parameterized constructor.

Overload constructor.

(e.g)

classGeek {

String name;

int id;

Geek(String name, intid)

{

this.name = name;

this.id = id;

}

}

Global Variables ( field ).

Static Variables – Class specific information

We can access static variables by using their class name

Instance variables – Instance ( object ) specific information for each and every instance, a copy will be maintained.

we can access instance variables by using instances ( Objects ).

this keyword

“this” keyword refers to current class object

“this” keyword can access all global instance variables

(e.g)

class Student{ 

 int rollno;  

String name;  

float fee;  

Student(int rollno,String name,float fee)

 this.rollno=rollno; 

 this.name=name; 

 this.fee=fee; 

 }  

}

Static keyword

The static keyword belongs to the class than an instance of the class.

Static refers to class level memory.

(e.g) main method ( public static void main)

when class is loaded into memory, main method is also getting loaded. thus it gets executed automatically.

Array Programs Pattern Programs

Total array values count

    int[] arr1= {10,20,30,10,20,30,10,20,40,50};
    int[] freq=new int[arr1.length];
    for(int j=0;j<arr1.length;j++)
    {
        int no=arr1[j];
        int count=1;
        for(int i=j+1;i<arr1.length;i++)
        {
            if(no==arr1[i])
            {
                count++;
                freq[i]=-1;
            }
        }
        if(freq[j]!=-1)           //  total count storing in freq array
        {

            freq[j]=count;
        }
    }

    for(int i=0;i<freq.length;i++)
    {
        if(freq[i]>0)                       // unique values....if(freq[i]==1)     ||    if(freq>1).....dublicate values    ||  maximum no...find in freq array        
        {
            System.out.println(arr1[i]+ "count:" + freq[i]);
        }
    }





    int max=Integer.MIN_VALUE;
    int index=0;
    for(int i=0;i<freq.length;i++)
    {
    if(freq[i]>max)
    {
    max=freq[i];
    index=i;
    }
    }

    System.out.println(arr1[index]);

PATTERN PROGRAMS

    /*
for(int no=1;no<3;no++)
{
for(int col=1;col<=3;col++)
{
System.out.print(no);
}
System.out.println();
}
*/

    /*
int no=1;
for(int col=no;col<=3;col++)
{
System.out.println(no);
}
System.out.println();
*/

   /* int no=1;

    for(int col=1;col<=no;col++)
    {
        System.out.println(no);
    }
    System.out.println();

    */
    /*
    int no=2;
    for(int col=1;col<=no;col++)
    {
        System.out.println(no);
    }
    System.out.println();

    */

   /* int row=2;
    for(int col=1;col<=row;col++)
    {
        System.out.print(row);
    }
    System.out.println();

    */

   /* for(int row=1;row<=3;row++)
    {
        for(int col=1;col<=row;col++)
        {
            System.out.print(row);
        }
        System.out.println();
    }

    */

    /* int row=2;
    for(int col=row;col>=1;col--)
    {
        System.out.print(col);
    }
    System.out.println();

    */

    /* for(int row=3;row>=1;row--)
    {
        for(int col=row;col>=1;col--)
        {
            System.out.print(col);
        }
        System.out.println();
    }

    */

    /*
int row=1;
for(int col=row;col>=1;col--)
{
System.out.print(col);
}
System.out.println();    */

    /*   int row=3;
    for(int col=row;col>=1;col--)
    {
        System.out.print(col);
    }
    System.out.println();


    */

   /* int row=1;
    while(row<=3)
    {
        for(int col=1;col<=3;col++)
        {
            System.out.print(row);
        }
        System.out.println();
        row++;
    }

    */

   /*  int row=1;
    while(row<=3)
    {
        for(int col=1;col<=3;col++)
        {
            System.out.print(col);
        }
        System.out.println();
        row++;
    }
     */

   /*
int row=1;
while(row<=3)
{
for(int col=1;col<=3;col++)
{
System.out.print("*");
}
System.out.println();
row++;
}  */

   /* for(int row=3;row>=1;row--)
    {
        for(int col=1;col<=3;col++)
        {
            System.out.print(row);
        }
        System.out.println();
    }

    */

  /*  for(int row=1;row<=3;row++)
    {
        for(int col=3;col>=1;col--)
        {
            System.out.print(col);
        }
        System.out.println();
    }

    */

  /*
for(int row=1;row<=3;row++)
{
for(int col=1;col<=3;col++)
{
if((row==2)&&((col==1)||(col==3)))
{
System.out.print(" ");
}
else if((row==3)&&((col==1)||(col==3)))
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
System.out.println();
}   */

/*   for(int row=1;row<=4;row++)
{
for(int col=1;col<=4;col++)
{
if((row==2)&&((col==1)||(col==3)))
{
System.out.print(" ");
}
else if((row==3)&&((col==1)||(col==3)))
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
System.out.println();
}    */

/*
for(int row=1;row<=5;row++)
{
for(int col=1;col<=3;col++)
{
if((row==2)&&((col==1)||(col==3)))
{
System.out.print(" ");
}
else if((row==3)&&((col==1)||(col==3)))
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
System.out.println();
}      */

for(int row=1;row<=5;row++)
{
for(int col=1;col<=3;col++)
{
if((row==1)||(row==5))
{
if(col==1)
{
System.out.print(" ");
}
else
{
System.out.print(""); } } else if(col==1) { System.out.print("");
}
}
System.out.println();
}     */

for(int row=1;row<=5;row++)
{
for(int col=1;col<=3;col++)
{
if((row==1)||(row==5))
{
if(col==1)
{
System.out.print(” “);
}
else
{
System.out.print(““); } } else if(col==1) { System.out.print(““);
}
}
System.out.println();
}

Array Programs

/* replacing left twice order…. doubt


    int [] a= {15,20,25,20,25};
    int i=0;int temp1=0,temp2=0;
    while(i<3)
    {
        a[i]=a[i+2];
        i++;
    }
    a[3]=temp1;
    a[4]=temp2;
    for(int j=0;j<a.length;j++)
    {
        System.out.println(a[j]);
    }

Reverse order

    int[] arr1= {3,8,2,5,4};
    int len=arr1.length;
    int[] arr2=new int[len];
    int i=0; int j=arr1.length-1;
    while(i<arr1.length)
    {
        arr2[i]=arr1[j];
        i++;
        j--;
    }

    for(int k=0;k<arr2.length;k++)
    {
        System.out.println(arr2[k]);
    }

Adding two integers array

    int[] arr1= {5,8,3,10};
    int[] arr2= {2,3};
    int len1=arr1.length;
    int len2=arr2.length;
    int len=len1>len2?len1:len2;
    int[] result=new int[len];
    int addcount=len1<len2?len1:len2;

    int i=0;
    while(i<addcount)
    {
        result[i]=arr1[i]+arr2[i];
        System.out.println(result[i]);
        i++;
    }
    //System.out.println(i);

    while(i<len)
    {
        result[i]=arr1[i];System.out.println(result[i]);

        i++;
    }
    while(i<result.length)
    {
        System.out.println(result[i]);
        i++;
    }

Storing negative values in result array

    int[] arr1= {-5,8,-3,10};
    int count=0;
    for(int i=0;i<arr1.length;i++)     //  find result array length
    {
        if(arr1[i]<0)
        {
            count++;
        }
    }

    int[] result=new int[count];
    int j=0;                             // i value length increment in result array length. so declaring j variable.
    for(int i=0;i<arr1.length;i++)     //swapping step
    {
        if(arr1[i]<0)
        {
            result[j]=arr1[i];
            System.out.println(result[j]);

            j++;
        }
    }

Merging 2 arrays

    int[] a= {10,20,30};

    int[] b= {5,15};

    int[] result=new int[a.length+b.length];
    int i=0;

    while(i<a.length)
    {
        result[i]=a[i];
        System.out.println(result[i]);
        i++;
    }
    int j=i; int k=0;
    while(j<result.length)
    {
        result[j]=b[k];
        System.out.println(result[j]);
        k++;
        j++;
    }

Merging 2 arrays another model…only one loop using

    int[] a= {10,20,30};
    int[] b= {5,15};
    int[] result=new int[a.length+b.length];
    int i=0,k=0;
    while(i<result.length)
    {
        if(i<a.length)
        {
            result[i]=a[i];
            System.out.println(result[i]);
        }
        else
        {
            result[i]=b[k];
            k++;
            System.out.println(result[i]);
        }
        i++;

    }

Find odd index elements in given array

    int[] arr= {10,20,30,40,50};

    int[] result=new int[arr.length/2];

    int i=0,j=1;

    while(i<result.length)
    {
        result[i]=arr[j];
        System.out.println(result[i]);
        i++;
        j=j+2;

    }  

Array Programs

Find the number

 int [] array= {5,3,2,4,7};
int no=3; int i=0;
while(i<array.length)
{
if(array[i]==40)
{
break;
}
i++;
}
if(i==array.length)
{
System.out.println("No is not present");
}
else
{
System.out.println(i);
}
//System.out.println("3 is present" +i);
for(int j=i;j<array.length-1;j++)
    {
        array[j]=array[j+1];
    }

Placing all the elements in one array to another array

    int[] array1= {5,3,2,4,7};
    int no=60;
    int len=array1.length;   //5
    int[] array2=new int[len+1];
    int len2=array2.length;    //6
    array2[len2-1]=no;      //array2[5]=60
    for(int i=0;i<array1.length;i++)
    {
        array2[i]=array1[i];
    }


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

Count of given particular no

    int[] array= {10,20,10,30,40,10,25};
    int search=10,count=0;
    int i=0;
    while(i<array.length)
    {
        if(array[i]==search)
        {

            count++;
        }
        i++;
    }

Count 10 and 20 nos in the given array

    int[] array= {10,20,10,30,40,10,25};
    int search1=10,search2=20,count=0;
    int i=0;
    while(i<array.length)
    {
        if((array[i]==search1)||(array[i]==search2))
        {

            count++;
        }
        i++;
    }
        System.out.println(count);

Array program

 int[] array= {10,20,10,30,40,10,25};
int search1=10,search2=20,count1=0,count2=0;
int i=0;
while(i<array.length)
{
if(array[i]==search1)
{
  count1++;
        }

    }
        System.out.println(count1);

        else if(array[i]==search2)
        {
            count2++;
        }
        i++;



        int[] arr1= {10,20,10,30,40,10,25,20};
        int[] search= {10,20};
        int i=0,count=0;
        while(i<arr.length) 

Count 10 and 20 in given Array


int [] arr1= {10,20,10,30,40,10,25,20};
int[] search= {10,20}; int j=0;
while(j<search.length)
{
int i=0,count=0;
while(i<arr1.length)
{
if(arr1[i]==search[j])
{
count++;
}
i++;
}
System.out.println(count);
j++;
}

Max number Program
int[] no= {3,7,2,8,5};
int max=0; int i=0;
while(i<no.length)
{
if(max<no[i])
{
max=no[i];
}
i++;
}
System.out.println(max);

Max and Min Program

     int[] no= {3,7,-2,8,-5};
        int max=Integer.MIN_VALUE; int i=0; int min=Integer.MAX_VALUE;
        while(i<no.length)
        {
            if(max<no[i])
            {
                max=no[i];
            }
            else if(min>no[i])
            {
                min=no[i];
            }
            i++;
        }
        System.out.println(max);
        System.out.println(min);

Find first maximum and second maximum
int[] no= {3,7,2,8,5};
int max=Integer.MIN_VALUE; // min max_value
int max2=Integer.MIN_VALUE; //min max_value
int i=0;
while(imax) // less than
{
max2=max;
max=no[i];
}
else if(no[i]>max2) // less than
{
max2=no[i];
}
i++;
}
System.out.println(max);
System.out.println(max2);

Max Min Program e.g 2

int[] no= {3,7,2,8,5};
int min=Integer.MAX_VALUE;
int min2=Integer.MAX_VALUE;
int i=0;
while(i<no.length)
{
if(no[i]<min)
{
min2=min;
min=no[i];
}
else if(no[i]<min2)
{
min2=no[i];
}
i++;
}
System.out.println(min);
System.out.println(min2);

Pre defined names

DataType Class names

byte – Byte

short – Short

int – Integer

long – Long

char – Charecter

boolean – Boolean

float – Float

double – Double

ARRAY

What is ARRAY?

  • Array is an object
  • Collection of similar elements
  • Stored continuously
  • It is index based
  • Index starts from 0 (ZERO)
  • Index ends with length-1.
  • Storing multiple values in single variable

Declaration of Array

    float[] marks=new float[10];
    System.out.println(marks[0]);

    byte[] b=new byte[5];
    System.out.println(b[0]);

    short[] s=new short[5];
    System.out.println(s[0]);

    int [] i=new int[5];
    System.out.println(i[0]);

    long [] l=new long[5];
    System.out.println(l[0]);

    String [] st=new String[5];
    System.out.println(st[0]);

    double [] d=new double[5];
    System.out.println(d[0]);

    boolean [] bo=new boolean[5];
    System.out.println(bo[0]);

    char [] ch=new char[5];
    System.out.println(ch[0]);

Syntax:

String [] names= new int [20];

char [] name= new char [20];

boolean [] pass= new boolean [20];

float [] names= new float [20];

2 Types Of Array Declaration

(i) int [] marks= new int[5];

marks[0]=80;

marks[1]=50;

marks[2]=40;

marks[3]=84;

marks[4]=70;

(ii) int [] mark2= {50,30,60,80,90};

Array Basics

 int [] marks1= {90,56,85,78,92}; int i=0;
while(i<marks1.length)
{
System.out.println(marks1[i]);
i++;
}

 for(int ii=0;ii<marks1.length;ii++)
    {
     System.out.println(marks1[ii]);
    }

Using scanner class

   System.out.println("subject nos");
Scanner scannerObj=new Scanner(System.in);
int len=scannerObj.nextInt();
 int[] mark=new int[len];

    for(int i=0;i<len;i++)
    {
        System.out.println("enter marks");
        mark[i]=scannerObj.nextInt();

    }
    for(int i=0;i<len;i++)
    {
        System.out.println(mark[i]);
    }

GCD Program

rule – find the smallest.

smallest—-> big % smallest==0.

int no1=30,no2=15;
int small=no1<no2?no1:no2; // condition true ah iruntha no1 small….false ah iruntha no2 small.
int big=no1>no2?no1:no2; // condition true ah iruntha no1 big….false ah iruntha no2 big

if(big%small==0)
{
System.out.println(“GCD”+ small);
}

divisible of greatest no of the given 2 nos

int no1=100,no2=50;
int small=no1=2)
{
if((no1%small==0)&&(no2%small==0))
{
System.out.println(“GCD”+ small);
break;
}
small–;
}

divisible of greatest no of the given 2 nos………NO GCD PROGRAM

int no1=15,no2=16;
int small=no1=2)
{
if((no1%small==0)&&(no2%small==0))
{
common=false;
System.out.println(“GCD”+ small);
break;
}
small–;
}
if(common==true)
{
System.out.println(“no GCD”);
}

LCM————-> mee peru pogu vaguthi

int no1=49,no2=81;
int big=no1>no2?no1:no2;
int big2=big;
int small=no1<no2?no1:no2;
while(true)
{
if(big%small==0)
{
System.out.println(big);
break;
}
big+=big2;
}

decimal to binary number

int no=4; String rem=””;
while(no>0)
{
rem=no%2+rem; //rem+no%2 ah no%2+rem ah change panna
no=no/2;
}
System.out.println(rem);

binary to decimal number

int no=1001,power=0; int dec=0,rem=0;
while(no>0)
{
rem=no%10;
dec=dec+(int) (rem*Math.pow(2, power));
no=no/10;
power++;
}
System.out.println(dec);

print sum of sum in the given numbers

int no=12345,sum;
do
{
sum=0;
while(no>0)
{
int rem=no%10;
sum=sum+rem;
no=no/10;
}
no=sum;
}
while(sum>9);
System.out.println(sum);

armstrong no

int no=153; int no2=no;
int arm=0,rem=0;
while(no>0)
{
rem=no%10;
arm=arm+(remremrem);
no=no/10;
}
if(no2==arm)
{
System.out.println(“arm”);
}

Neon program

int no=9,sum=0,rem=0;
int square=no*no;
while(square>0)
{
rem=square%10;
sum=sum+rem;
square=square/10;
}
if(sum==no)
{
System.out.println(“neon”);
}

Perfect number program

            int sum=0;
int no=6;
for(int i=1;i<no;i++)
{
if(no%i==0)
sum=sum+i;
}
if(sum==no)
System.out.println("PERFECT");

fibonacci program

int first=0,second=1;
System.out.println(first);
System.out.println(second);
for(int i=2;i<=10;i++)
{
int third=first+second;
System.out.println(third);
first=second;
second=third;
}

fibonacci no entha place la irukku


int first=0,second=1;
int count=0;
while(true)
{
int third=first+second;
count++;
if(count==4)
{
System.out.println(third);
break;
}
first=second;
second=third;
}

New JavaPrograms

PRINTING DIVICES OF 3……USING FOR LOOP

for(int no=3;no<=100;no+=3)
{      
  System.out.println(no);

    }

PRINTING 3 MULTIPLES

int no=3,no2=1;
while(no<=21)
{
System.out.println(no2+”*3=”+no);
no+=3;
no2++;
}

PRINTING 1,3,5,7,9,2,4,6,8

int no=1;
while(no<=10)
{
System.out.println(no);
no+=2;
if(no==11)
no=2;
}

CUBE NO’S PROGRAM

int no=1,count=1;
while(count<=4)
{
System.out.println(no*no*no);
no++;
count++;
}

MATH.POW….

       (e.g1)
       int no=1;
       while(no<=10)
       {
       System.out.println(no*no);
       no++;
       }
       System.out.println(Math.pow(3,3));


      (e.g2)
       int no=1;
      while(no<=5)
      {
      System.out.println((int)Math.pow(no,no));
     no++;

FIZZ….BUZZ…FIZZBUZZ PROGRAM

      int no=1;
while(no<=50)
{
if((no%3==0)&&(no%5==0))
{
System.out.println("fizzbuzz");
}
else if(no%3==0)
{
System.out.println("fizz");
}
else if(no%5==0)
{
System.out.println("buzz");
}
  no++;

    }

INTERVIEW PROGRAMS

int no1=1;
int no2=10;
while(no1<no2)
{
System.out.println(no1*no2);
no1++;
no2–;
}

PRIME OR NOT PRIME NUMBERS

int no=7;
int count=0;
for(int i=1;i<=no;i++)
{
if(no%i==0)
{
count++;
}
}
if(count<3)
{
System.out.println(“the given no is prime”);
}
else
{
System.out.println(“the given no is not prime”);
}

PRIME NO PROGRAM

int no=19,div=3; boolean check=true;
if(no%2!=0)
{
while(div<no)
{
if(no%div==0)
{
System.out.println(“not prime”);
check=false;
break;
}
div+=2;
}
if(check==true)
{
System.out.println(“prime”);
}
}
else
{
System.out.println(“not prime no”);
}

ADDITION OF FIRST N NUMBERS

int no=1,total=0;
while(no<=10)
{
total=total+no;
no++;
}
System.out.println(total);

FACTORIAL

int fact=1,no=1;
while(no<=5)
{
fact=fact*no;
no++;
}
System.out.println(fact);

PRINTING MULTIPLE NUMBERS FACTORIAL

 int given=5;
do
{
int fact=1,no=1;
while(no<=given)
{
fact=fact*no;
        no++;
    }

      System.out.println(fact);
      given--;
    }
    while(given>0);

SQUARE ROOT PROGRAM

int no=81;
int div=2;
while(div<no/2)
{
if(no/div==div)
{
System.out.println(div+”root”);
break;
}
div++;
}

PRINTING REVERSE NUMBERS

int no=123456789; int rem=0;
while(no>0)
{
rem=no%10;
System.out.println(rem);
no=no/10;
}

FIND NoOFDigits

int no=123456789; int rem=0; int noOfDigits=0;
while(no>0)
{
rem=no%10;
System.out.println(rem);
no=no/10;
noOfDigits++;
}
System.out.println(noOfDigits);

PALINDROME NO PROGRAM

    int no=121;
    int rev=0;
    int no1=no;
    while(no>0)
    {
        int rem=no%10;
        rev=(rev*10)+rem;
        no=no/10;
    }
    System.out.println(rev);
    if(no1==rev)
    {
        System.out.println("the given no is palindrome");
    }

SUM OF DIGITS

int no=1234;
int sumOfDigits=0;
while(no>0)
{
int rem=no%10;
sumOfDigits=sumOfDigits+rem;
no=no/10;
}
System.out.println(sumOfDigits);

DIVISIBLE SMALLEST NO OF THE GIVEN NO

int no=105;
int div=2;
while(div<=no)
{
if(no%div==0)
{
System.out.println(div);
break;
}
div++;
}

While Loop and Scanner in Java

While loop flowchart

{

int count=0;

while(count<=5)

{

int mark=90;

count=count+1;

}

}

  1. Initially count is zero
  2. When my count is less than or equal to 5, do the below

2.1 Get mark

2.2 Increment count by 1

2.3 Go back to the step 2.

3. Stop

Scanner

The Scanner class is used to get user input, and it is found in the java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings

How Scanner is created ?

import java.util.Scanner;

Scanner scanner=new Scanner(System.in);
int mark=scanner.nextInt();

Methods in java…. if conditional class

Methods

Sequence of instruction with a name.

Modularity

Module- smallest logical unit ( multiple methods inside main method ).

Main

  • Program starting area
  • Triggering area

Inside main method

Object creation, Objects method accessing.

*** Main method ulla object creation and method calling mattum than irukka vendum ***

explained the below program…

E.g public class School()

public static void main (string [] args) – main method

{

School registrationno = new School(); <—– Object creation

registrationno.count(); <—— Method calling

}

*** Method definition main method ku veliya kodukka vendum ***

*** count ku method definition below mentioned ***

public void count()

{

int a=10;

int b=30;

int total=a+b;

system.out.println(total);

}

Code Reusability

Already typed program

It is also predifined method ( e.g – println ).

if condition syntax

initialize

if(condition)

{

print statement;

}

else

{

print statement;

}

Static keyword of java

public static void main ( string [] args)

This is a main method…..

public static void main ( string [] args)

{

method body (or)

method definition….

}

What is static keyword ?

  • Static is a class behaviour
  • Static refers to class level memory.
  • Data will be store only once.
  • It is not at all constant

Methods ——> behaviour

Methods are also called behaviour.

Sequence of instruction with a name.

e.g program.

int tamil=90; english;

total = tamil+english;

System.out.println(total);

Error occurred in above program..

total cannot be resolved to a variable.

int tamil = 85; // initialization

int english; // declaration

Note: If type mismatch error comes, change or add datatype.

Console – – – – – – > displaying output

***Static keyword iruntha main method la irunthu start agum. program execution um anga irunthu than start agum…***