Monday, October 9, 2023

Codeathon04_Haritha


[Question]

Java Advanced - Lambda Expressions

Write the Following Methods that Return a Lambda Expression Performing a Specified Action: Perform Operationis Odd(): The Lambda Expression must return  if a Number is Odd or  If it is Even. Perform Operationis Prime(): The lambda expression must return  if a number is prime or  if it is composite. Perform Operationis 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


Explanation:

The input consists of multiple lines, each containing a pair of integers. The first integer in each pair represents the type of operation to be performed, where:

1 indicates checking if the number is odd,

2 indicates checking if the number is prime,

3 indicates checking if the number is a palindrome.

The second integer in each pair is the number on which the specified operation should be applied.

Lambda expressions are used to create small, reusable pieces of code that carry out these operations on the input numbers. The output format is tailored to the result of each operation. For example, if the number is odd, the output will be "ODD." If the number is prime, the output will be "PRIME." And if the number is a palindrome, the output will be "PALINDROME."

In summary, the program takes pairs of integers as input, uses lambda expressions to perform specific operations on these numbers, and provides output that corresponds to the results of these operations, such as "ODD," "PRIME," or "PALINDROME."


Program

package src;

import java.util.Scanner;

interface LambdaExpression {

  boolean perform(int x);

}

public class Codeathon04_Haritha {

  public static void main(String args[]) {

    LambdaExpression isOdd = n -> n % 2 != 0;

    LambdaExpression isPrime = n -> {

      if (n < 2) return false;

      for (int i = 2; i <= Math.sqrt(n); i++) {

        if (n % i == 0) return false;

      }

      return true;

    };

    LambdaExpression isPalindrome = n -> {

      int reversed = 0, original = n;

      while (n != 0) {

        int digit = n % 10;

        reversed = reversed * 10 + digit;

        n /= 10;

      }

      return original == reversed;

    };

    Scanner scanner = new Scanner(System.in);

    System.out.println("Please enter value :");

    int num = scanner.nextInt();

    String[] output = new String[num];

    System.out.println("Please choose options :");

    for (int i = 0; i < num; i++) {

      int ch = scanner.nextInt();

      int no = scanner.nextInt();

      switch (ch) {

        case 1:

          output[i] = isOdd.perform(no) ? "ODD" : "EVEN";

          break;

        case 2:

          output[i] = isPrime.perform(no) ? "PRIME" : "COMPOSITE";

          break;

        case 3:

          output[i] = isPalindrome.perform(no) ? "PALINDROME" : "NOT A PALINDROME";

          break;

      }

    }

    System.out.println("Output :");

    for (int i = 0; i < num; i++)

      System.out.println(output[i]);

  }

}



[Explanation of Solution]

 The program defines a functional interface called LambdaExpression with a single method perform(int x) that returns a boolean.

Main method:

 1. Three lambda expressions (isOdd, isPrime, and isPalindrome) are defined, each implementing the     LambdaExpression interface. These lambda expressions represent different operations to be performed   on an integer.

2. The isOdd lambda expression checks if a given integer is odd or even.

3. The isPrime lambda expression checks if a given integer is prime or composite.

4. The isPalindrome lambda expression checks if a given integer is a palindrome (reads the same forwards and backwards).

5. The program prompts the user to enter an integer value (num) representing the number of test cases they want to perform.

6. An array of strings called output is created to store the results of the test cases.

7. The program then prompts the user to choose options for each test case, where each option corresponds to one of the lambda expressions (isOdd, isPrime, or isPalindrome). The user selects an option (ch) and provides an integer input (no) for each test case.

8. Inside a switch statement, the selected option (ch) is used to determine which lambda expression to apply to the input integer (no). Depending on the option, the result is assigned to the output array as a string ("ODD," "EVEN," "PRIME," "COMPOSITE," "PALINDROME," or "NOT A PALINDROME").

Finally, the program prints the results for each test case by iterating through the output array.

This program demonstrates the use of lambda expressions to perform different operations on integers based on user input. It allows the user to select an operation for each test case and displays the results accordingly.



 Haritha .P(Intern),
 Guard Ninjas,
 Data Shield Team,
 Enterprise Minds.   

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home