Monday, June 26, 2017

Lamda Expression 2

package calculator;

public class Calculator {
  
    interface IntegerMath {
        boolean check(int a); 
    }
  
     public boolean operateBinary(IntegerMath op, int x) {
        //return op.operation(a, b);
        //return op.operation(x, y, z);
        return op.check(x);
    }

    public static void main(String... args) {
    
        Calculator myApp = new Calculator();
        

        IntegerMath mycheck = (a) -> a == 90;
        
        System.out.println("is a equal to 90?= " +
            myApp.operateBinary(mycheck, 40));
        
    }
}




Generic Interface




http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

The interface Predicate is an example of a generic interface. 

Generic types (such as generic interfaces) specify one or more type parameters within angle brackets (<>). 
This interface contains only one type parameter, T

When you declare or instantiate a generic type with actual type arguments, you have a parameterized type.


Lambda Expression

public class Calculator {
  
    interface IntegerMath {
        int operation(int a, int b);   
    }
  
    public int operateBinary(int a, int b, IntegerMath op) {
        return op.operation(a, b);
    }
 
    public static void main(String... args) {
    
        Calculator myApp = new Calculator();
        IntegerMath addition = (a, b) -> a + b;
        IntegerMath subtraction = (a, b) -> a - b;
        System.out.println("40 + 2 = " +
            myApp.operateBinary(40, 2, addition));
        System.out.println("20 - 10 = " +
            myApp.operateBinary(20, 10, subtraction));    
    }
}

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax



package calculator;

public class Calculator {
  
    interface IntegerMath {
        int operation(int a, int b, int c); 
    }
  
    public int operateBinary(IntegerMath op, int x, int y, int z) {
        //return op.operation(a, b);
        return op.operation(x, y, z);
    }

    public static void main(String... args) {
    
        Calculator myApp = new Calculator();
        
        IntegerMath addition = (a, b, c) -> a + b + c;
        IntegerMath subtraction = (a, b, c) -> a - b - c;
        
        System.out.println("40 + 2 = " +
            myApp.operateBinary(addition, 40, 20, 10));
        
        System.out.println("20 - 10 = " +
            myApp.operateBinary(subtraction, 20, 10, 5));    
    }
}



Simple Java Code - calling another method

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helloworldapp;

/**
 *
 * @author Kent Lau
 */
public class HelloWorldApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Hello World!");
       
        int testscore = 76;
        char grade;
       
       
        System.out.println("Grade = " + testMyScore(testscore));
    }
   
    public static char testMyScore(int score) {
    //do the calculation here
   
        if (score >= 90) {
            return 'A';
        } else if (score >= 80) {
            return 'B';
        } else if (score >= 70) {
            return 'C';
        } else if (score >= 60) {
            return 'D';
        } else {
            return 'F';
        }
    }
   
}