We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Advanced
- Java Lambda Expressions
- Discussions
Java Lambda Expressions
Java Lambda Expressions
Sort by
recency
|
162 Discussions
|
Please Login in order to post a comment
The predefined code for Main class in Java 8 is wrong. Had to switch to Java 15 for my code to work.
import java.io.; import java.util.; interface PerformOperation { boolean check(int a); } class MyMath { public static boolean checker(PerformOperation p, int num) { return p.check(num); } public PerformOperation isOdd(){ PerformOperation isOddOperation = new PerformOperation(){
}
public PerformOperation isPrime(){ PerformOperation isPrimeOperation = new PerformOperation(){
}
public PerformOperation isPalindrome(){ PerformOperation isPalindromeOperation = new PerformOperation(){
} } public class Solution {
public static void main(String[] args) throws IOException { MyMath ob = new MyMath(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); PerformOperation op; boolean ret = false; String ans = null; while (T--> 0) { String s = br.readLine().trim(); StringTokenizer st = new StringTokenizer(s); int ch = Integer.parseInt(st.nextToken()); int num = Integer.parseInt(st.nextToken()); if (ch == 1) { op = ob.isOdd(); ret = ob.checker(op, num); ans = (ret) ? "ODD" : "EVEN"; } else if (ch == 2) { op = ob.isPrime(); ret = ob.checker(op, num); ans = (ret) ? "PRIME" : "COMPOSITE"; } else if (ch == 3) { op = ob.isPalindrome(); ret = ob.checker(op, num); ans = (ret) ? "PALINDROME" : "NOT PALINDROME";
} System.out.println(ans); } } }
"This Java 8 challenge tests your knowledge of Lambda expressions"
No, this ridiculous questions expects implementations of two complex algorithms. What a ridiculous "medium"
where is the locked stub?
unable to submit ,as one test case failed.
`import java.io.; import java.util.; interface PerformOperation { boolean check(int a); } class MyMath { public static boolean checker(PerformOperation p, int num) { return p.check(num); }
// Write your code here public PerformOperation isOdd() { return (int num) -> num % 2 ==1; } public PerformOperation isPrime (){ return (int num) -> { if(num < 2){ return false; } for(int i = 2; i*i <= num; i++) { if(num % i == 0){ return false; } } return true; };
} public PerformOperation isPalindrome(){ return (int num) -> { //343 int revNum = 0; while(num > 0){ num = num / 10; //34 3 int reminder = num % 10; // 3 4 revNum = num * 10 + reminder; } if(revNum == num){ return true; }else return false; }; }
} public c