Day 16 - Java

Brick Distribution at Ram Mandir, Ayodhya

Problem Statement:

In preparation for the construction and renovation of the sacred Ram Mandir in Ayodhya, various devotees and benefactors are sending bricks as offerings. Your task is to categorize these bricks into different containers based on certain conditions. There are four types of containers: A, B, C, and D.

Conditions for each container type:

Container A: If the number of bricks sent is a perfect square.

Container B: If the number of bricks sent is an Armstrong number.

Container C: If the number of bricks sent is a palindrome number.

Container D: If the number of bricks sent does not meet any of the above conditions.

Write a Java program utilizing lambda expressions to determine which container the bricks should be placed into based on the given number of bricks.

Constraints:

The number of bricks (input) is an integer.

1 <= Number of bricks <= 10^9

Input Format:

A single integer representing the number of bricks sent.

Output Format:

A single line containing the container type where the bricks should be placed (A, B, C, or D).

Sample Input:

10000

Sample Output:

Container A

Explanation:

For the given input, 10000 is a perfect square (100 * 100 = 10000), so the bricks should be placed in Container A.

Test Cases:

Test Case 1:

Input:

144

Output:

Container A

Explanation: 144 is a perfect square (12 * 12 = 144), so the bricks should be placed in Container A.

Test Case 2:

Input:

370

Output:

Container B

Explanation: 370 is an Armstrong number, so the bricks should be placed in Container B.

Test Case 3:

Input:

181

Output:

Container C

Explanation: 181 is a palindrome number, so the bricks should be placed in Container C.

Test Case 4:

Input:

12345

Output:

Container D

Explanation: 12345 does not meet any of the conditions, so the bricks should be placed in Container D.


SOLUTION:

import java.util.Scanner;

import java.util.function.Predicate;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        Predicate<Integer> isPerfectSquare = (n) -> {

            int sqrt = (int) Math.sqrt(n);

            return (sqrt * sqrt == n);

        };

        Predicate<Integer> isArmstrongNumber = (n) -> {

            int num = n;

            int sum = 0;

            while (n > 0) {

                int digit = n % 10;

                sum += Math.pow(digit, 3);

                n /= 10;

            }

            return (sum == num);

        };

        Predicate<Integer> isPalindromeNumber = (n) -> {

            int rev = 0, num = n;

            while (n != 0) {

                int remainder = n % 10;

                rev = rev * 10 + remainder;

                n /= 10;

            }

            return num == rev;

        };

        int n = sc.nextInt();

        String containerType = 

                isPerfectSquare.test(n) ? "Container A" :

                isArmstrongNumber.test(n) ? "Container B" :

                isPalindromeNumber.test(n) ? "Container C" :

                "Container D";

        System.out.println(containerType);

    }

}


Insights:

  • The code utilizes the functional programming paradigm in Java through the use of lambda expressions. It employs the Predicate functional interface to define and use boolean-valued functions.
  • Three predicate functions are defined: isPerfectSquare, isArmstrongNumber, and isPalindromeNumber. Each function checks a specific condition based on the input integer.
  • The isPerfectSquare predicate checks if the given number is a perfect square by calculating its square root and squaring it again for comparison.
  • The isArmstrongNumber predicate checks if the given number is an Armstrong number by summing the cubes of its digits and comparing the result with the original number.
  • The isPalindromeNumber predicate checks if the given number is a palindrome by reversing its digits and comparing the reversed number with the original.
  • Based on the results of the predicate tests, the code determines which container (A, B, C, or D) the input integer should be assigned to.
  • Finally, the code prints the container assignment based on the results of the predicate tests.
Let our code echo the harmony of Ayodhya's Ram Mandir, inspiring unity and progress in our coding journey! 
Happy coding, aspiring developers!!:)

Comments