184k views
4 votes
/* Given two ints, each in the range 10..99, return true if there is a digit

* that appears in both numbers, such as the 2 in 12 and 23.
*/
public boolean shareDigit(int a, int b)

User El Confuso
by
7.9k points

1 Answer

2 votes

Final answer:

The Java method 'shareDigit' checks if two provided integers, both within the range 10 to 99, have at least one digit in common. It utilizes arithmetic operations to isolate digits and compares them, returning true if there's a match.

Step-by-step explanation:

Understanding the shareDigit Function in Java:

The question involves a Java method named shareDigit that aims to determine whether two integers in the range 10 to 99 share a common digit. In the provided method, the digits of both integers are extracted using division and modulus operations.

The left digit (tens place) is obtained by dividing the number by 10, while the right digit (ones place) is obtained by taking the modulus of the number by 10. After extracting the digits, the method checks for any commonality between the digits of the two numbers and returns true if there is at least one match.

The following conditions in the return statement check for common digits: 'al == bl' compares the left digit of 'a' with the left digit of 'b'; 'al == br' compares the left digit of 'a' with the right digit of 'b'; similarly, 'ar == bl' and 'ar == br' compare the right digit of 'a' with both the left and right digits of 'b' respectively. If any of these conditions are true, the method will return true, indicating a shared digit.

User Rmolinamir
by
8.4k points