You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
interface PerformOperation{ boolean doThings(int n); }
public class Solution {
public static void main(String[] args) { Scanner s = new Scanner(System.in); int val= s.nextInt(); for (int j=0; j<val;j++){ PerformOperation isOdd = (n)->{ return n%2==0?false:true; }; PerformOperation isPrime = (n)->{ if (n <= 1) return false; for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; }; PerformOperation isPalindrome = (n)->{ int remainder; int reversedNum=0; int originalNum=n; while (n != 0) { remainder = n % 10; reversedNum = reversedNum * 10 + remainder; n /= 10; } if (originalNum == reversedNum) { return true; } else { return false; } }; switch(s.nextInt()){ case(1): if(doStuff(isOdd,s.nextInt())) System.out.println("ODD"); else System.out.println("EVEN"); break; case(2): if(doStuff(isPrime,s.nextInt())) System.out.println("PRIME"); else System.out.println("COMPOSITE"); break; case(3): if(doStuff(isPalindrome,s.nextInt())) System.out.println("PALINDROME"); else System.out.println("NOT PALINDROME"); break; } } s.close(); } static boolean doStuff(PerformOperation p, int num){ return p.doThings(num); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Java Lambda Expressions
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
interface PerformOperation{ boolean doThings(int n); }
public class Solution {
}