eg 1

Hello Program
DataTypes (Primitive and Reference/Object)


int intVal;
float floatVal;
double doubleVal;
boolean booleanVal;
char charVal;
String StrVal;


Operators
Arithmetic (+, -, *, /, ++, --)
Relational (==, !=, >, <, >=, <=)
Bitwise (&, |, !, ~, ^, <<, >>, >>>)
Logical (&&, ||, !)
Assigment (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
Conditional Operator ( ? : )

Condtional Statements
Loop Types



int[] intArray = new int[10];
float[] floatArray = new float[10];
double[] doubleArray = new double[10];
boolean[] booleanArray = new boolean[10];
char[] charArray = new char[10];
String[] StrArray = new String[10];




public class QuotesTest {

   public static void main(String args[]) {
      System.out.println("She said \"Hello!\" to me.");
   }
}

public class ArithmeticOperatorsTest {

  public static void main(String args[]) {
     int a = 10;
     int b = 20;
     int c = 25;
     int d = 25;
     System.out.println("a + b = " + (a + b) );
     System.out.println("a - b = " + (a - b) );
     System.out.println("a * b = " + (a * b) );
     System.out.println("b / a = " + (b / a) );
     System.out.println("b % a = " + (b % a) );
     System.out.println("c % a = " + (c % a) );
     System.out.println("a++   = " +  (a++) );
     System.out.println("b--   = " +  (a--) );
     // Check the difference in d++ and ++d
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
}

public class RelationalOperatorsTest {

  public static void main(String args[]) {
     int a = 10;
     int b = 20;
     System.out.println("a == b = " + (a == b) );
     System.out.println("a != b = " + (a != b) );
     System.out.println("a > b = " + (a > b) );
     System.out.println("a < b = " + (a < b) );
     System.out.println("b >= a = " + (b >= a) );
     System.out.println("b <= a = " + (b <= a) );
  }
}

class BitwiseOperatorsTest {

  public static void main(String args[]) {
     int a = 60; /* 60 = 0011 1100 */
     int b = 13; /* 13 = 0000 1101 */
     int c = 0;

     c = a & b;       /* 12 = 0000 1100 */
     System.out.println("a & b = " + c );

     c = a | b;       /* 61 = 0011 1101 */
     System.out.println("a | b = " + c );

     c = a ^ b;       /* 49 = 0011 0001 */
     System.out.println("a ^ b = " + c );

     c = ~a;          /*-61 = 1100 0011 */
     System.out.println("~a = " + c );

     c = a << 2;     /* 240 = 1111 0000 */
     System.out.println("a << 2 = " + c );

     c = a >> 2;     /* 15 = 1111 */
     System.out.println("a >> 2  = " + c );

     c = a >>> 2;     /* 15 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
}

public class LogicalOperatorsTest {

  public static void main(String args[]) {
     boolean a = true;
     boolean b = false;

     System.out.println("a && b = " + (a&&b));

     System.out.println("a || b = " + (a||b) );

     System.out.println("!(a && b) = " + !(a && b));
  }
}


public class AssignmentOperatorsTest {

  public static void main(String args[]) {
     int a = 10;
     int b = 20;
     int c = 0;

     c = a + b;
System.out.println("c = 10 + 20 = " + c );

c += a ;
System.out.println("c += 10  = " + c );

c -= a ;
System.out.println("c -= 10 = " + c );

c *= a ;
System.out.println("c *= 10 = " + c );

a = 10;
c = 15;
c /= a ;
System.out.println("c /= 10 = " + c );

a = 10;
c = 15;
c %= a ;
System.out.println("c %= 10  = " + c );

c <<= 2 ;
System.out.println("c <<= 2 = " + c );

c >>= 2 ;
System.out.println("c >>= 2 = " + c );

c >>= 2 ;
System.out.println("c >>= 2 = " + c );

c &= a ;
System.out.println("c &= 2  = " + c );
 
c ^= a ;
System.out.println("c ^= 10   = " + c );

c |= a ;
System.out.println("c |= 10  = " + c );
  }
}


public class ConditionalOperatorTest {

   public static void main(String args[]){
      int a, b;
      a = 10;
      b = (a == 1) ? 20: 30;
      System.out.println( "Value of b is : " +  b );

      b = (a == 10) ? 20: 30;
      System.out.println( "Value of b is : " + b );
   }
}


public class InstanceofOperatorTest {

   public static void main(String args[]){
      String name = "Computers";
      // following will return true since name is type of String
      boolean result = name instanceof String;
      System.out.println( result );
   }
}

CONDITIONAL Statements

public class SimpleIfTest {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("This is if statement");
      }
   }
}

public class SimpleIfElseTest {

   public static void main(String args[]){
      int x = 30;

      if( x < 20 ){
         System.out.print("This is if statement");
      }else{
         System.out.print("This is else statement");
      }
   }
}


public class IfElseLadderTest {

   public static void main(String args[]){
      int x = 30;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("This is else statement");
      }
   }
}


public class NestedIfTest {

   public static void main(String args[]){
      int x = 30;
      int y = 10;

      if( x == 30 ){
         if( y == 10 ){
             System.out.print("X = 30 and Y = 10");
          }
       }
    }
}


public class SwitchStatementTest {

   public static void main(String args[]){
      //char grade = args[0].charAt(0);
      char grade = 'C';

      switch(grade)
      {
         case 'A' :
            System.out.println("Excellent!");
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}




LOOPING

public class WhileLoopTest {

   public static void main(String args[]) {
      int x = 10;

      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

public class ForLoopTest {

   public static void main(String args[]) {

      for(int x = 10; x < 20; x = x+1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}


public class DoWhileTest {

   public static void main(String args[]){
      int x = 10;

      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

Comments

Popular posts from this blog

commenting in java

Running a java program