Methods & Conditionals

What if...

Quick reminder

  1. What is Java & Android Studio?
     
  2. What is a variable? A variable has... ?
     
  3. Variable data types in Java?
     
  4. "4.0" + "4.0" = ? "4" + '4' ? 
     
  5. int a = "4.0" + 4? String a = "4.0" + 4?

Weekly test time

Today's content

  1. What are conditionals?
  2. The If operator
  3. Comparison & Boolean operators
  4. IF & Else & Else If
  5. The switch operator
  6. What are methods?
  7. Methods structure
  8. Summary

Conditionals

IF operator

boolean isLectureDay = true;

if(isLectureDay == true) {
    goToLecture();
    writeSomeCode();
}
if(CONDITION) {
    STATEMENT1;
    STATEMENT2;
        ...
    STATEMENTN;
}

Comparison operators

x > y: x is greater than y

x < y: x is less than y

 

int a = 10;
if(a = 10) {
// "=" Really !?
}
if("Test" == "Test") { // !?
// "Test".equals("Test")
}

x >= y: x is greater than or equal to x

x <= y: x is less than or equal to y

x == y: x equals y 

Boolean operators

&&: logical AND

||: logical OR 

boolean isProgrammerAtHome = true;
int workingHours = 3;
if(isProgrammerAtHome) {
    if(workingHours < 8) {
        // You are fired :(
    }
}
boolean isProgrammerAtHome = true;
int workingHours = 3;
if(isProgrammerAtHome && workingHours < 8) {
    // You are fired :(
}

IF & ELSE

boolean isProgrammerHome = true, 
    shouldFireProgrammer = false;

if(!isProgrammerHome == true) {
    System.out.println("WORK HARDER!");
} else {
    shouldFireProgrammer = true;
}
if(CONDITION) {
    STATEMENT1;
} else {
    STATEMENT2;
}

IF & ELSE IF

if (CONDITION) {
    STATEMENTS
} else if (CONDITION) {
    STATEMENTS
} else if (CONDITION) {
    STATEMENTS
} else {
    STATEMENTS
}
boolean isProgrammerAtHome = false;
int workingHours = 8;
if(isProgrammerAtHome) {
    // Fire
} else if(workingHours < 8) {
    // Fire
} else {
    // WORK HARDER!
}

SWITCH

int workingHours = 8;
// Default value is ...?
boolean shouldReceiveBonus; 
switch(workingHours) {
    case 8:
        shouldReceiveBonus = true;
        break;
    case 9:
        shouldReceiveBonus = true;
        break;
    default:
        shouldReceiveBonus = false;
}
int workingHours = 8;
boolean shouldReceiveBonus;
switch(workingHours) {
    case 8:
    case 9:
        shouldReceiveBonus = true;
        break;
    default:
        shouldReceiveBonus = false;
}
switch(CONDITION) {
    case CONDITION1:
        break;
    case CONDITIONN:
        break;
    default:
}
switch(1) {
    case 1:
        print("No break !?")
    case 2:
        print("I am leaving")
}
// Result: 
// No break !? 
// I am leaving

Task time

Questions?

Methods

  • A subprogram - a set of code
  • Must have return type
  • May or may not have input
  • May or may not return result
  • Can be called by name

When the method execution is finished the program returns to the area of the code from which it was called and continues on to the next line of code.

Method signature

public static void method(String[] arguments) {
    System.out.println("I am a method");
}
public static void NAME() {
    STATEMENTS;
}
NAME();

To call a method:

Parameters

public static void method(TYPE NAME) {
    STATEMENTS;
}
public static void printHi() {
    print("Hello!")
}
public static void printNumbers() {
    print(42); // 42 + "" ?
}
public static void print(String message) {
    System.out.println(message);
}

Multiple Parameters

public static int calculateDDS(int a, int b) {
    return 0.2 * (a + b);
}

calculateDDS(50, 60);
public static void NAME(TYPE NAME, TYPE NAME) {
    STATEMENTS;
}

Return Values

public static TYPE NAME() {
    STATEMENTS
    return EXPRESSION;
}

void is the word for "no type"

void a = ""; // !?

Methods example

public class Math {
    public static int multiply(int  a, int b) {
        return a * b;
    }

    public static int pow(int  number, int degree) {
        int result = 0;
        switch(degree) {
            case 2:
                result = multiply(number, number);
                break;
            case 3:
                result = multiply(multiply(number, number), 
                             number);
                break;
        }
    }
}

Methods Summary

  • They are a block of code

  • Programs are built of small methods
  • Methods can be reused and tested individually
  • Keeping methods small will make your life better
  • The caller of the method does not need to know how it works
  • This is called "abstraction"

Math Wars 2

Questions?

Daily reminder test

Methods & Conditionals

By Georgi Mirchev

Methods & Conditionals

  • 586