Wednesday, October 11, 2023

EM-Tirupati Codeathon Series #04

[Question]

Java Advanced — Lambda Expressions [www.techgig.com]

Write the Following Methods that Return a Lambda Expression Performing a Specified Action: Perform Operation is Odd(): The Lambda Expression must return if a Number is Odd or If it is Even. Perform Operation is Prime(): The lambda expression must return if a number is prime or if it is composite. Perform Operation is Palindrome(): The Lambda Expression must return if a number is a Palindrome or if it is not.

[Sample Input]

Input is as Show in the Format Below (Deduce Unknowns!)

Input

3

1 3

2 7

3 7777

Constraints

NA

[Sample output]

Output is as Show in the Format Below (Deduce Unknowns!)

Output

ODD

PRIME

PALINDROME

Code Explaination:

  1. This program uses lambda expressions and functional interfaces to encapsulate different operations for checking numbers, making the code more modular and readable.
  2. The program defines a functional interface called Operation with a single method apply, which takes an integer and returns a boolean.
  3. The ValueChecker class provides methods to create lambda expressions using the Operation functional interface for three specific operations: checking if a number is odd, checking if it's prime, and checking if it's a palindrome.
  4. In the main method of the Question04_Venkata class:
    • An instance of ValueChecker is created.
    • The program reads the number of test cases from the user.
    • For each test case:
      • The program reads two integers from the user, representing a choice and a number.
      • Depending on the choice (1, 2, or 3), it selects the corresponding lambda expression from ValueChecker.
      • The selected lambda expression is applied to the input number using the apply method of the Operation functional interface.
      • The result is stored as a string, indicating whether the input number meets the criteria (e.g., "ODD," "PRIME," "COMPOSITE," "PALINDROME," or "NOT PALINDROME").
      • The results are added to a list.
  5. After processing all test cases, the program displays the results by iterating through the list and printing each result. 

GitHub Repo link: http://surl.li/mpkit

Source Code(Java):

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

// Step 1: Define a functional interface for operations.
interface Operation {
   boolean apply(int number);
}

class ValueChecker {
   public static boolean checkValue(Operation operation, int number) {
      return operation.apply(number);
   }
   // Step 2: Implement methods for operations as lambda expressions.
   public static Operation isOdd() {
      return n -> (n & 1) == 1; // Checks if a number is odd.
   }

   public static Operation isPrime() {
      return n -> {
         if (n < 2) {
            return false;
         }
         int sqrt = (int) Math.sqrt(n);
         for (int i = 2; i <= sqrt; i++) {
            if (n % i == 0) {
               return false;
            }
         }
         return true; // Checks if a number is prime.
      };
   }

   public static Operation isPalindrome() {
      return n -> {
         String original = Integer.toString(n);
         String reversed = new StringBuilder(Integer.toString(n)).reverse().toString();
         return original.equals(reversed); // Checks if a number is a palindrome.
      };
   }
}

public class Question04_Venkata {
   public static void main(String[] args) {
      ValueChecker valueChecker = new ValueChecker();
      Scanner scanner = new Scanner(System.in);
      int testCases = scanner.nextInt();
      Operation operation;
      String result = null;
      scanner.nextLine();
      List<String> results = new ArrayList<>();

      while (testCases-- > 0) {
         String input = scanner.nextLine().trim();
         StringTokenizer tokenizer = new StringTokenizer(input);
         int choice = Integer.parseInt(tokenizer.nextToken());
         int number = Integer.parseInt(tokenizer.nextToken());

         // Step 3: Use the functional interfaces to check values.
         if (choice == 1) {
            operation = valueChecker.isOdd();
            result = valueChecker.checkValue(operation, number) ? "ODD" : "EVEN";
         } else if (choice == 2) {
            operation = valueChecker.isPrime();
            result = valueChecker.checkValue(operation, number) ? "PRIME" : "COMPOSITE";
         } else if (choice == 3) {
            operation = valueChecker.isPalindrome();
            result = valueChecker.checkValue(operation, number) ? "PALINDROME" : "NOT PALINDROME";
         }

         results.add(result);
      }

      scanner.close();

      // Step 4: Display the results.
      for (String output : results) {
         System.out.println(output);
      }
   }
}


Thank you

Venkata kishore T(Intern)

Shield Warriors,

Data Shield Team,

Enterprise Minds.

No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...