arrays, 

conditional statements 

& control flow

class overview

  • Homework Solution
  • JUnit Testing
  • Arrays
  • Conditional Statements
  • Control Flow

create a junit test case

  • In Eclipse:
    • New > JUnit Test Case
    • Select "OK" to add JUnit to the build path
  • Test naming convention is to name the test case "MyClassTest.java" where "MyClass" is the Class you're testing.

Junit test cases

  • @Test tag designates a Test Unit
    • Inside each test, use assert functions to compare an expected and actual value
 @Test
 public void addTest() {
     assertEquals(6, MyClass.add(3, 3);
 }




arrays

  • Arrays contain an ordered set of values
  • int [] arr = {1, 2, 3, 4, 5};
    • An array created with values
  • int [] arr2 = new int[50];
    • An array declared with space for 50 ints.
    • What is the default value of each upon initialization?

array creation

  • Once created, the size of an array cannot be changed.
  • To grow an array, copy values to a larger array.
  • Example method to create an int array of dynamic size:
 public int [] createIntArray (int size) {
     return new int [size];
 }



String array example

  • Typical Initialization and usage:
 String [] sa = new String[2];
 sa[0] = "String 0"; 
 sa[1] = "String 1";
  • Inline Initialization:
 String [] sa = {"String 0", "String 1"};

multi-dimensional arrays

  • To achieve a 2 or 3 dimensional array, create arrays inside of arrays.
 int [] [] a = {{1,2}, {3,4}, {5,6}};
 System.out.println(a[1][0]);  //prints 3
int [] [] b = new int [5][3]; b[0][2] = 5;



array pointers

  • Arrays are not primitive types.
  • An array variable points to the array.
  • An array argument in a method points to the passed array.
 public class Arrays {
     public static void main(String [] args) {
          int [] a = {10, 15, 0};
          add(a);
          System.out.println(a[2]);
     }
     public static void add(int [] arr) {
          arr[2] = arr[0] + arr[1];
     }
 } //a[2] prints as 25


array pointers

  • Array argument can be pointed to a new Array which does not affect original array.
 public class Arrays {
     public static void main(String [] args) {
          int [] a = {10, 15, 99};
          add(a);
          System.out.println(a[2]);
     }
     public void add(int [] a) {
          a = new int[3];
          a[2] = a[0] + a[1];
     }
 }
 //a[2] prints out 99, unaffected by the add function

conditional statements

  • Runs certain code based on a set of true/false evaluations
  • Common Conditional statements:
    • if / else
    • switch
    • for
    • while
    • for each



if / else

  • Only one path is executed
  • Statement in parentheses must evaluate to true or false
 int a = 5;
 int b = 10;

 if (a == b) {
     System.out.println("same");
 } else if (b == a*2) {
     System.out.println("double");
 } else {
     System.out.println("different");
 }

switch

  • Switch statement tests a variable against several pre-defined cases, executing code if a match is found.
  • Switches work with bytes, shorts, chars, ints, Enums, Strings and a few more.
  • Break statement must be included after each case to stop the code execution
 int num = 4;
 switch(num) {
     case 3:
        System.out.println("Three");
        break;
     case 4:
        System.out.println("Four");
        break;
     default:
        System.out.println("Different");
 }

for loop

  • Loops until a certain case evaluates to false
  • Built in code execution at the end of each loop
 //count up 
 for(int i = 0; i < 10; i++) {
     System.out.println(i);
 }

 //count down
 for(int i = 10; i > 0; i--) {
     System.out.println(i);
 }



foreach loop

  • Loops through an array or Collection
  • More convenient than a full-blown for loop
  • Left side of the colon is a declared variable for each element
  • Right side is the collection
 int [] numArray = {1, 2, 3, 4, 5};
 for(int number : numArray) {
     System.out.println(number);
 }



while loop

  • Loops until a statement evaluates to false
 //loops forever
 while(true) {
     System.out.println("Forever");
 }

 //loops 10 times
 int i = 0;
 while(i < 10) {
     System.out.println(i);
     i++;
 }


control flow

  • Control flow statements change the execution of your program
  • break
    • Used to break out of a for/while loop or switch
  • continue
    • Used to skip to the next iteration of a for/while loop
  • return
    • Used to return from a method. 
    • return can take a value to return back to the calling method.

break

  • The break statement terminates the innermost for/while or switch statement.
 int [] numbers = {1, 2, 3, 4, 5};
 for(int num : numbers) {
     if(num ==3) {
          break;
     }
 }


return

  • The return statement halts execution of the current method and returns control to the code where it was called.
 public static boolean hasNumber(int number, int [] numArray) {
     for(int arrNum : numArray) {
          if(arrNum == number) {
               return true;
          }
     }
     return false;
 }


continue

  • The continue statement skips the current iteration of a loop and continues to the next, for a for/while loop.
 int [] numbers = {1, 2, 3, 4, 5};
 for(int num : numbers) {
     if(num % 2 > 0) 
         continue;
     System.out.println(num + " is an even number");
 }


Made with Slides.com