Building a workflow

Intermediate Java

Jim White

Java Developer

Double odd numbers, halve even numbers

class DoubleOrHalve {
  public static void main(String[] args) {
    int[] numbers = {23, 78, 2263, 552};
    int[] newNumbers = doubleOddHalveEven(numbers); // Value is 46, 39, 4526, and 276
  }
  static int[] doubleOddHalveEven(int[] arr) {
    for (int i = 0; i < arr.length; i++) { // Loop through the array
      if (arr[i] % 2 == 0) { 
        arr[i] /= 2; // Halve even numbers
      } else {
        arr[i] *= 2; // Double odd numbers
      }
    }
    return arr;
    }
}

Intermediate Java

Method chaining

  • Call methods directly after one another, on the same value
    • E.g., variable.method1().method2()
class CheckStrings {
  public static void main(String[] args) {
    String message = "Hello World!";
    String input = "hello world!";

    // Convert message to lower case, then compare with input 
    System.out.println(message.toLowerCase().equals(input));
    }   
}
true
Intermediate Java

Structure

  • Break code into clear steps
  • Use meaningful names
  • Keep logic together

➡ Makes code easier to understand, update, and maintain

Person going up stairs

Intermediate Java

Let's practice!

Intermediate Java

Preparing Video For Download...