- Prepare
- Java
- Advanced
- Java Lambda Expressions
- Discussions
Java Lambda Expressions
Java Lambda Expressions
+ 0 comments class MyMath { public static boolean checker(PerformOperation p, int num) { return p.check(num); } public PerformOperation isOdd(){ return x->x%2!=0; } public PerformOperation isPrime(){ return x->{ if (x < 0){ x = x*(-1); } if (x < 2){ return false; } if (x == 2){ return true; } if (x%2==0){ return false; } for (int i = 2; i*i <= x; i++){ if (x%i==0){ return false; } } return true; }; } public PerformOperation isPalindrome(){ return x ->{ String s = String.valueOf(x); if (s.length()<=1){ return true; } char[] c = s.toCharArray(); int i = 0; int j = c.length-1; while (i++ <= j--){ if (c[i]!=c[j]){ return false; } } return true; }; } }
+ 0 comments I found this article on Java Lambda Expressions to be incredibly insightful. It's like unraveling the intricacies of programming, much like the precision of toiletsadviser. Explaining complex concepts in such a clear and concise manner truly aids in understanding. Great work on simplifying Java Lambda Expressions!
+ 1 comment public class Solution {
public static void checkOddEven(int n) { System.out.println(n%2 == 0?"EVEN":"ODD"); }
public static void checkPrime(int n) { BigInteger bi = BigInteger.valueOf(n); System.out.println(bi.isProbablePrime(10)?"PRIME":"COMPOSITE"); } public static void checkPalindrome(int n) { int revNum=0; int oriNum=n; while(n>0) { revNum =(revNum*10)+(n%10); n=n/10; } System.out.println(revNum==oriNum?"PALINDROME":"NOT PALINDROME");
}public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T =sc.nextInt(); while(T-- >0 ) { int code = sc.nextInt(); int n = sc.nextInt(); if(code==1) { checkOddEven(n); } else if(code==2) { checkPrime(n); } else { checkPalindrome(n); } } }
}
+ 0 comments //Java 7
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++){ int condition = sc.nextInt(); int num = sc.nextInt(); if (condition == 1){ if (num % 2 == 0){ System.out.println("EVEN"); } else { System.out.println("ODD"); } } else if (condition == 2) { if (isPrime(num)) { System.out.println("PRIME"); } else { System.out.println("COMPOSITE"); } } else if (condition == 3) { if (isPalindrome(num)) { System.out.println("PALINDROME"); } else { System.out.println("NOT PALINDROME"); } } } } static boolean isPrime(int num){ if (num <= 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } static boolean isPalindrome(int num) { int reversed = 0; int original = num; while(num > 0) { int digit = num % 10; reversed = reversed * 10 + digit; num /= 10; } return original == reversed; } }
+ 0 comments import java.util.Scanner; interface PerformOperation { boolean check(int number); } public class JavaChallenge { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); PerformOperation isOdd = new PerformOperation() { @Override public boolean check(int number) { return isOddNumber(number); } }; PerformOperation isPrime = new PerformOperation() { @Override public boolean check(int number) { return isPrimeNumber(number); } }; PerformOperation isPalindrome = new PerformOperation() { @Override public boolean check(int number) { return isPalindromeNumber(number); } }; for (int i = 0; i < t; i++) { int type = scanner.nextInt(); int num = scanner.nextInt(); if (type == 1) { System.out.println(isOdd.check(num) ? "ODD" : "EVEN"); } else if (type == 2) { System.out.println(isPrime.check(num) ? "PRIME" : "COMPOSITE"); } else if (type == 3) { System.out.println(isPalindrome.check(num) ? "PALINDROME" : "NOT PALINDROME"); } } scanner.close(); } private static boolean isOddNumber(int number) { return number % 2 != 0; } private static boolean isPrimeNumber(int number) { if (number < 2) return false; for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) return false; } return true; } private static boolean isPalindromeNumber(int number) { String numStr = String.valueOf(number); int i = 0, j = numStr.length() - 1; while (i < j) { if (numStr.charAt(i) != numStr.charAt(j)) return false; i++; j--; } return true; } }
Sort 143 Discussions, By:
Please Login in order to post a comment