Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method named largerDigits that accepts two integer parameters a a

ID: 660788 • Letter: W

Question

Write a static method named largerDigits that accepts two integer parameters a and b and returns a new integer c where each digit of c gets its value from the larger of a's and b's digit in the same place. That is, the ones digit of c is the larger of the ones digit of a and the ones digit of b, and the tens digit of c is the larger of the tens digit of a and the tens digit of b, and so on. You may assume that a and b are positive integers (greater than 0).

For example, suppose a is 603452384 and b is 921782. Their digits would be combined as follows to produce c:

Notice that if a particular digit place is absent from one number or the other, such as the 603 at the start of a above, no digit is carried over to c. The following table lists some more calls to your method and their expected return values:

Hint: If you are building a result number, you may need to use Math.pow or accumulate a multiplier with each digit.

You may not use a String to solve this problem.

Method problem: For this problem, you are supposed to write a Java method as described. You should notwrite a complete Java class; just write the method(s) described in the problem statement.

Call Value Returned largerDigits(172, 312) 372 largerDigits(21, 3) 3 largerDigits(90, 38906735) 95 largerDigits(56002, 123321) 56322 largerDigits(11223, 4466) 4466 largerDigits(12345, 12345) 12345 largerDigits(1, 34892) 2

Explanation / Answer

public static void main(String[] args) {
System.out.println(bignum(135, 216));

System.out.println(bignum(172, 312));
System.out.println(bignum(21, 3));
System.out.println(bignum(90, 38906735));
System.out.println(bignum(56002, 123321));
System.out.println(bignum(11223, 4466));
System.out.println(bignum(12345, 12345));
System.out.println(bignum(1, 34892));
}

public static int bignum(int x, int y) {
int sum = 0;
int multiplier = 1;
int digit = 0;

while (x > 0 && y > 0) {
digit = (Math.max(x % 10, y % 10) * multiplier);
sum += digit;
x /= 10;
y /= 10;
multiplier = multiplier * 10;
}
return sum;
}
}

results:
236
372
3
95
56322
4466
12345
2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote