Friday, 22 January 2016

Java Code stmts

1. Loops

public class Loops {
  public static void main(String [] args) throws Throwable {

loop:
    while (true) {
      for (int i = 0; i < 100; i++) {
        int c = System.in.read();
        if (c == 'x') {
          continue loop;
        } else if (c == 'y') {
          break loop;
        } else {
          System.out.print((char)c);
          //System.out.flush();
        }
      }
      System.out.println(">");
    }
    System.out.println();
  }
}

2.  ScopeExample

public class ScopeExample {
  private int i=1;

  public void firstMethod() {
    int i=4, j=5;

    this.i = i + j;
    secondMethod(7);
  }
  public void secondMethod(int i) {
    this.i = i;
  }
}

3. StmtLabels

/*
 * This file tests a few variations on labelled break/continue statements.
 */

public class StmtLabels {
  public static void main(String[] args) {
    int count = 0;
    
    // test break label on compound statement
  lab1:
    {
      System.out.println("before lab1");
      if ( count == 0 ) 
{ break lab1; }
      System.out.println("after lab1");
    }

    // test break label on an if statement
    System.out.println("before lab1a");
  lab1a:
    if ( count == 0 ) {
break lab1a;
    } else {
System.out.println("else clause; not break to lab1a");
    }
    System.out.println("after lab1a");

    // test continue label on compound statement
  lab2:
    {
      System.out.println("before lab2");
      //if ( count == 0 ) 
      //{ continue lab2; }  CAN NOT continue A COMPOUND STMT
      System.out.println("after lab2");
    }
  }
}


4. TestForLoop

public class TestForLoop {
  public static void main(String[] args) {
    for ( int i = 0; i < 10; i++ )
      System.out.println(i + " squared is " + (i*i));
  }
}


5. TestInitBeforeUse

public class TestInitBeforeUse {

  public void doComputation() {
    int x = (int)(Math.random() * 100);
    int y;
    int z;
    if (x > 50) {
      y = 9;
    }
    z = y + x;  // Possible use before initialization
  }

}

6.  TestScoping

public class TestScoping {
  public static void main(String[] args) {
    ScopeExample  scope = new ScopeExample();

    scope.firstMethod();
  }
}

7.  TestShift

public class TestShift {

  public static String bitPattern(int value) {
    int BIT1_MASK = 1;
    final char[] bits = new char[32];

    for ( int i = 31; i >= 0; i-- ) {
      if ( (value & BIT1_MASK) == 1 ) {
bits[i] = '1';
      } else {
bits[i] = '0';
      }
      value >>>= 1;
    }

    return new String(bits);
  }

  public static void main(String[] args) {
    int num1 = 1357;
    int num2 = -1357;

    System.out.println("num1 (" + num1 + ") as a bit pattern: " + bitPattern(num1));
    System.out.println("num2 (" + num2 + ") as a bit pattern: " + bitPattern(num2));
    System.out.println("" + num1 + " >> 5 = (" + (num1 >> 5) + ") " + bitPattern(num1 >> 5));
    System.out.println("" + num2 + " >> 5 = (" + (num2 >> 5) + ") " + bitPattern(num2 >> 5));
    System.out.println("" + num1 + " >>> 5 = (" + (num1 >>> 5) + ") " + bitPattern(num1 >>> 5));
    System.out.println("" + num2 + " >>> 5 = (" + (num2 >>> 5) + ") " + bitPattern(num2 >>> 5));
    System.out.println("" + num1 + " << 5 = (" + (num1 << 5) + ") " + bitPattern(num1 << 5));
    System.out.println("" + num2 + " << 5 = (" + (num2 << 5) + ") " + bitPattern(num2 << 5));
  }
}

/*
num1 (1357) as a bit pattern:  00000000000000000000010101001101
num2 (-1357) as a bit pattern: 11111111111111111111101010110011
1357   >> 1 = 00000000000000000000001010100110
-1357  >> 1 = 11111111111111111111110101011001
1357   >> 5 = 00000000000000000000000000101010
-1357  >> 5 = 11111111111111111111111111010101
1357  >>> 5 = 00000000000000000000000000101010
-1357 >>> 5 = 00000111111111111111111111010101
1357   << 1 = 00000000000000000000101010011010
-1357  << 1 = 11111111111111111111010101100110
1357   << 5 = 00000000000000001010100110100000
-1357  << 5 = 11111111111111110101011001100000
*/


8.  TestWhileLoops

public class TestWhileLoops {

  public static void main(String[] args) {
    test1();
    test2();
    test3();
    test4();
  }

  private static void test1() {
    System.out.println("Test #1 - while loop with block");
    int i = 0;
    while ( i < 10 ) {
      System.out.println(i + " squared is " + (i*i));
      i++;
    }
  }

  private static void test2() {
    System.out.println("Test #2 - while loop with single statement");
    int i = 0;
    while ( i < 10 )
      System.out.println(++i + " squared is " + (i*i));
  }

  private static void test3() {
    System.out.println("Test #3 - do-while loop with block");
    int i = 0;
    do {
      System.out.println(i + " squared is " + (i*i));
      i++;
    } while ( i < 10 );
  }

  private static void test4() {
    System.out.println("Test #4 - do-while loop with single statement");
    int i = 0;
    do
      System.out.println(++i + " squared is " + (i*i));
    while ( i < 10 );
  }
}


No comments:

Post a Comment