Question

Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns...

Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns the number of digits that match between them. Two digits match if they are equal and have the same relative position starting from the end of the number (i.e., starting with the ones digit). In other words, the method should compare the last digits of each number, the second-to-last digits of each number, the third-to-last digits of each number, and so forth, counting how many pairs match. For example, for the call digitMatch(1072503891, 62530841), the method should return 4 in this case because 4 of these pairs match (2-2, 5-5, 8-8, and 1-1).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class DigitMatch {

    public static int digitMatch(int n1, int n2) {
        if(n1 == 0 || n2 == 0) {
            return 0;
        } else {
            if(n1 % 10 == n2 % 10) {
                return 1 + digitMatch(n1/10, n2/10);
            } else {
                return digitMatch(n1/10, n2/10);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(digitMatch(1072503891, 62530841));
    }

}

Add a comment
Know the answer?
Add Answer to:
Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java method called compare that takes two integers as input parameters and returns 1...

    Write a Java method called compare that takes two integers as input parameters and returns 1 if the first parameter is larger than the second parameter returns – 1 if the second parameter is larger than the first parameter, and returns 0 if the two parameters are equal.

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Write a recursive function named multiply that takes two positive integers as parameters and returns the...

    Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...

  • please write in java 5.10. sameContents: this method takes one input parameters of type ThingSortedArrayBag. The...

    please write in java 5.10. sameContents: this method takes one input parameters of type ThingSortedArrayBag. The method then returns true if the input bag includes the same elements as the current bag even if the count of each element may be different from one bag to the other. For example, the method should return true for the following two bags of integers. The method returns false otherwise. [1,1,2,2,3] and [1,2,3]

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • Intro to Java Programming Write a method named isDivisible that takes two integers, n and m,...

    Intro to Java Programming Write a method named isDivisible that takes two integers, n and m, and that returns true if n is divisible by m, and false otherwise. Include your isDivisible() function as a method in Functions. In the main static method, demonstrate isDivisible() works by testing that it returns both possibilities correctly for a few different pairs of numbers. Include comments before your test code describing what's going on. The definition of divisibility is "One whole number is...

  • c++ problem Write a recursive method int mul(int a, int b) that computes the product of...

    c++ problem Write a recursive method int mul(int a, int b) that computes the product of two nonnegative integers a and b. You are not allowed to use multiplication () Hint: use addition (+) and recursion. Write a method evenDigits that accepts an integer parameter n and that returns the integer formed by removing the odd digits from n. The following table shows several calls and their expected return values: 1- 2- Call Valued Returned evenDigits (8342116); 8426 evenDigits(4109); 4...

  • Java. Can’t use for or while loops, thank you! Practice by writing a recursive method that...

    Java. Can’t use for or while loops, thank you! Practice by writing a recursive method that returns the number of digits in the integer passed to it as an argument of type int. Allow for both positive and negative arguments. For example, -120 has three digits. Hint: Dividing a number by 10 removes the last digit from the number

  • Write a Java program that has the following methods: findSum - a method that takes in...

    Write a Java program that has the following methods: findSum - a method that takes in three integers and returns the sum of these three integers; findAverage - a method that takes in three integers and returns the average of these three integers (as a double value) Within the main method read in 3 integers from the user and call above two methods by passing the three integers. Your program should print the user provided three integers and results of...

  • *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...

    *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a decimal number to a different base number system. Print out the results after testing your method on a few different inputs. Task 1 – Recursive Method Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT