Posts

Showing posts from October, 2021

Parametrized Constructor in Java

Image
 Parametrized Constructor in Java PROGRAM: public class ParametrizedConstructor {       String language ;     ParametrizedConstructor ( String lang )     {         language = lang ; //JAVA         System . out . println ( language + " is High level programming language" ) ;     }     public static void main ( String args [])     {         ParametrizedConstructor obj = new ParametrizedConstructor ( "Java" ) ;     } } /* OUTPUT Java is High level programming language */

Non parametrized Constructor in Java(OOPS)

Image
 Non parametrized Constructor in Java (OOPS) PROGRAM: /*   modifier ClassName()   {       //body of the constructor;   } */ public class Constructor {     String name ;     public Constructor (){         System . out . println ( "Constructor is called" ) ;         name = "Java Constructor" ;     }     public static void main ( String args [])     {         Constructor obj = new Constructor () ;         System . out . println ( "Constructor name is " + obj . name ) ;     } } /* OUTPUT Constructor is called Constructor name is Java Constructor */

Java Methods (OOPS)

Image
 Java Methods (OOPS) PROGRAM: //UserDifined Methods /*  Syntax for declaring method:  returntype methodName()  {    method body  }  */ /*  Calling a method in java  syntax:  objName.methodName();  */   public class JavaMethods {       int add ( int a ,int b ) //20 30     {         int c = a + b ; //20+30=50         return c ;     }     public static void main ( String args [])     {         int a = 20 ;         int b = 30 ;         JavaMethods obj = new JavaMethods () ;         int result = obj . add ( a , b ) ; //50         System . out . println ( "The sum is " + result ) ;     } } //The sum is 50

Class and Object in Java Programming (OOPS)

Image
 Class and Object in Java Programming (OOPS) PROGRAM: /* Syntax for class class className{     fields(variables)     methods } Syntax for Object className objectName=new className(); */ class Bike {     boolean lightswitch ;     public void turnOn ()     {         lightswitch = true ;         System . out . println ( "Light is On ? " + lightswitch ) ;     }     public void turnOff ()     {         lightswitch = false ;         System . out . println ( "Light is On ? " + lightswitch ) ;     } } public class JavaClass {   public static void main ( String args [])  {       Bike light = new Bike () ;       light . turnOn () ;       System . out . println ( "After 5 mins" ) ;       light . turnOff () ;  }     }

Program to Copy One Array to Another using Assignment operator

Image
Program to Copy One Array to Another using Assignment operator Program: public class CopyArrayAssignment {     public static void main ( String args [])     {         int arr []={ 1 , 2 , 3 , 4 , 5 } ;         int copyarr []= arr ;         for ( int i : copyarr )         {             System . out . println ( i ) ;         }     } } // 1 // 2 // 3 // 4 // 5

How to Iterate Two Dimensional Array

Image
 How to Iterate Two Dimensional Array PROGRAM: import java . util .* ; class Input2DArray {     public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in ) ;         int row = sc . nextInt () ; //2         int col = sc . nextInt () ; //2         int arr [][]= new int [ row ][ col ] ;         for ( int i = 0 ; i < row ; i ++)         {             for ( int j = 0 ; j < col ; j ++)             {                 arr [ i ][ j ]= sc . nextInt () ;             }         }         for ( int i = 0 ; i < row ; i ++)         {             for ( int j = 0 ; j < col ; j ++) ...

How to get 2D Array as Input from the user

Image
 How to get 2D Array as Input from the user PROGRAM: import java.util.* ; class Input2DArray {     public static void main ( String args[])     {         Scanner sc= new Scanner (System.in) ;         int row=sc.nextInt() ;         int col=sc.nextInt() ;         int arr[][]= new int [row][col] ;         for ( int i= 0 ; i<row ; i++)         {             for ( int j= 0 ; j<col ; j++)             {                 arr[i][j]=sc.nextInt() ;             }         }             } } /* ij    00 01 == 1  2    10 11    3  4    00 01 02      1  2  3    10 11 12  == ...

How to Initialize Two Dimensional Arrays in java

Image
 How to Initialize Two Dimensional Arrays in Java PROGRAM: public class InitializationOf2dArray {         public static void main ( String args [])     {         int arr [][]={{ 1 , 2 } , { 3 , 4 }} ; //2x2         int a2 [][]={{ 1 , 2 , 3 } , { 3 , 4 , 5 } , { 5 , 6 , 7 }} ; //3x3         System . out . println ( "arr[0][0] = " + arr [ 0 ][ 0 ]) ; //1         System . out . println ( "arr[0][1] = " + arr [ 0 ][ 1 ]) ; //2         System . out . println ( "arr[1][0] = " + arr [ 1 ][ 0 ]) ; //3         System . out . println ( "arr[1][1] = " + arr [ 1 ][ 1 ]) ; //4     } } /* 00 01 == 1  2    10 11    3  4    00 01 02      1  2  3    10 11 12  ==  3  4  5    20 21 22      5  6  7   ...

How to get array of elements as input from the user

Image
 How to get array of elements as input from the user PROGRAM: import java.util.* ; class LoopingThroughArray {     public static void main ( String args[])     {         Scan ner s c= n ew Scan ner (System.in) ;         int siz e=sc.nextIn t() ;         int arr[ ] = ne w int [ s i ze] ;         for ( int i= 0 ; i<size ; i++)         {             arr[i]=sc.nextInt() ;         }         System.o u t.p r i ntln ( " Elements in the array are:" ) ;         for ( int i= 0 ; i<size ; i++)         {             System.out.print(arr[i]+ " " ) ;         }     } } // 5 // 1 2 3 4 5 // Elements in the array are: // 1 2 3 4 5

How to Initialize and Accessing elements in an array

Image
 Initialize and Accessing elements in an array PROGRAM: class ArrayIntializ {     public static void main ( String args [])     {         int arr []={ 2 , 4 , 6 } ;         int arr2 []= new int [ 3 ] ;         arr2 [ 0 ]= 1 ;         arr2 [ 1 ]= 3 ;         arr2 [ 2 ]= 5 ;         System . out . println ( "Accessing element in first array" ) ;         System . out . println ( "First element is " + arr [ 0 ]) ;         System . out . println ( "second element is " + arr [ 1 ]) ;         System . out . println ( "third element is " + arr [ 2 ]) ;         System . out . println ( "Accessing element in second array" ) ;         System . out . println ( "First element is " + arr2 [ 0 ]) ;         System . out . println ( "s...

Enhanced for loop (for-each loop)

Image
 Enhanced for loop (for-each loop) PROGRAM: /* Syntax     for(datatype item:array)     {     } */ class forEachloop {     public static void main ( String args [])     {         int arr []={ 10 , 5 , 4 , 2 } ;         for ( int i : arr )         {             System . out . println ( i ) ; //10 5 4 2         }     } } /* O/P: 10 5 4 2 */

continue statement

Image
 continue statement PROGRAM: public class Continue {     public static void main ( String args [])     {         for ( int i = 1 ; i <= 10 ; i ++)         {             if ( i > 5 && i < 9 )             {                 continue ;             }             System . out . println ( i );         }             }     } // 1 // 2 // 3 // 4 // 5 // 9 // 10

Program to print number from 1 to N using break statement

Image
 Program to print number from 1 to N using break statement PROGRAM: import java . util .*; public class Break {     public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter a number from 1 to 10" );         int iNum = sc . nextInt ();         for ( int i = 1 ; i <= 10 ; i ++)         {             if ( i > iNum )             {                 break ;             }             else {                 System . out . println ( i );             }         }     }     } // Enter a number from 1 to 10 // 5 // ...

Sum of positive numbers using do while loop

Image
 Sum of positive numbers using do while loop PROGRAM: /*  Syntax    do{    }while(condition); */ import java . util .*; public class doWhile {     public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in );         int sum = 0 , n = 0 ;         do {             sum += n ;             System . out . println ( "Enter a number" );             n = sc . nextInt ();         } while ( n >= 0 );         System . out . println ( "The total sum is " + sum );     }     } /* Enter a number 5 Enter a number 10 Enter a number -5 The total sum is 15 */

Sum of Digits using while loop

Image
Sum of Digits using while loop PROGRAM: /*Syntax  while(condition)  {  } */ import java . util .*; public class While {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int a = sc . nextInt ();         int sum = 0 ;         while ( a > 0 )          {             int b = a % 10 ;             sum += b ;             a /= 10 ;         }         System . out . println ( sum );     } } /* I/P: 523 O/P: 10 */

Program to print numbers from 1 to N using for loop

Image
 Program to print numbers from 1 to N using for loop PROGRAM: /*Syntax for(initial expression;condition;update expression) { } */ //PROGRAM TO PRINT THE NUMBERS FROM 1 TO N: import java . util .*; public class forloop {         public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in );         int n = sc . nextInt ();         for ( int i = 1 ; i <= n ; i ++)         {             System . out . println ( i );         }     } } //I/p: // 5 //O/P: // 1 // 2 // 3 // 4 // 5

Switch Statements

Image
Switch Statements  PROGRAM: import java . util .*; class Switch {         public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in );         System . out . println ( "Enter a number between 1 to 5" );         int a = sc . nextInt (); //7         switch ( a )         {             case 1 :                 System . out . println ( "One" );                 break ;             case 2 :                 System . out . println ( "Two" );                 break ;             case 3 :                 System . out . println ( "Th...

Greatest of three numbers using else if conditions

Image
 Greatest of three numbers using else if  conditions PROGRAM: import java . util .*; class elseif {     public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in );         int a = sc . nextInt (); //5         int b = sc . nextInt (); //7         int c = sc . nextInt (); //2         if ( a > b && a > c )         {             System . out . println ( "A is greatest" );         }         else if ( b > a && b > c ) //7>5 && 7>2         {             System . out . println ( "B is greatest" );         }         else {             System . out . println ( "C is grea...

if-else conditions in java

Image
 if-else conditions in java PROGRAM: import java . util .*; class ifelse {     public static void main ( String args [])     {         Scanner sc = new Scanner ( System . in );         int a = sc . nextInt (); //5         if ( a < 0 )         {             System . out . println ( "The number is negative" );         }         else {                       System . out . println ( "The number is positive" );         }     } } // -2 //The number is negative //5 //The number is positive

Ternary Operator

Image
 Ternary Operator PROGRAM: class TernaryOperator {     public static void main ( String args [])     {         int a = 10 ;         int b = 20 ;         String c =( a > b ) ? "A is Greater" : "B is Greater" ;         System . out . println ( c ); //B is Greater     }     } //Syntax: //variable =condition?statement 1:statement 2

Instanceof Operator

Image
 Instanceof Operator PROGRAM: public class InstanceOfOperator {         public static void main ( String args [])     {     String s1 = "Hello World" ;     boolean b1 = s1 instanceof String ;     System . out . println ( "Is s1 an object of String ? " + b1 );     } } //OP: //Is s1 an object of String ? true

Right Shift Operator

Image
 Right Shift Operator PROGRAM: public class RightShiftOperator {     public static void main ( String args [])     {         int a = 8 ;         int b = a >> 2 ;         // 1 0 0 0         // 0 1 0 0         // 0 0 1 0==> o/p:2         // 8 4 2 1         //Another method         //a>>2==>         //first shift         // 8/2=4         //second shift         // 4/2=2         System . out . println ( b ); //2     }     }

Left Shift Operator

Image
 Left Shift Operator PROGRAM: public class LeftShiftOperator {     public static void main ( String args [])     {         int a = 2 ;         int b = a << 2 ;         // 0 0 1 0         // 0 1 0 0         // 1 0 0 0==> o/p:8         // 8 4 2 1         //Another method         //a<<2==>         //first shift         //2*2=4         //second shift         //4*2=8         System . out . println ( b ); //8     }     }

Bitwise Complement Operator

Image
 Bitwise Complement Operator PROGRAM: public   class   BitwiseComplement  {           public   static   void   main ( String   args [])     {              int   a = 35 ; //00100100          // 1's comp=>  11011011          //2's  comp=>         1          //-----------------------          ///            11011100=> 220 | -36          //Formula:-(N+1);              int   b =~ a ; //-(35+1)=>-36   ...

Bitwise XOR Operator

Image
 Bitwise XOR Operator PROGRAM: class   BitwiseOperator_XOR {      public   static   void   main ( String   args [])     {         int   a = 12 ; // 16 8 4 2 1                  //  0 1 1 0 0 ===>12         int   b = 25 ; // 16 8 4 2 1                  //  1 1 0 0 1 ===>25         int   c =  a ^ b ;         //TRUTH TABLE OR         // A    B         A^B   ...

Bitwise AND Operator

Image
 Bitwise AND Operator PROGRAM: public   class   BitwiseOperators_AND_  {           public   static   void   main ( String   args [])     {         int   a = 12 ; // 16 8 4 2 1                  //  0 1 1 0 0 ===>12         int   b = 25 ; // 16 8 4 2 1                  //  1 1 0 0 1 ===>25         int   c =  a  &  b ;         //TRUTH TABLE AND         // A    B ...

Bitwise OR Operator

Image
 Bitwise OR Operator PROGRAM: public   class   BitwiseOperators_OR_  {           public   static   void   main ( String   args [])     {         int   a = 12 ; // 16 8 4 2 1                  //  0 1 1 0 0 ===>12         int   b = 25 ; // 16 8 4 2 1                  //  1 1 0 0 1 ===>25         int   c =  a | b ;         //TRUTH TABLE OR         // A    B    ...

Increment & Decrement Operators

Image
 Increment & Decrement Operators PROGRAM: import   java . util .*; class   IncDec_Operators {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   a = sc . nextInt (); //5          //Pre Increment          int   c =++ a ; //5+1=6;c=6,a=6          System . out . println ( "c is " + c ); //6          System . out . println ( "a is " + a ); //6           //Post Increment           int   d = a ++; //d=6;a++=a=a+1=6+1=7  d=6 a=7   ...