Write a recursive method body for the smallestDigit method whose contract is giv
ID: 3802468 • Letter: W
Question
Write a recursive method body for the smallestDigit method whose contract is given below. As an example, if n - 23141, the method should return 1. You may only use NaturalNumberkernel methods: multiplyBy 10, divideBy 10, and isZero. (see API doc) Do not restore the parameter n by making a copy. Do not use magic numbers, breaks or multiple returns./** * Reports the minimum (i.e., smallest) single digit in n when it * is written in ordinary decimal notation. * ... * @ ensures * smallestDigit = the smallest digits in n] */private static int smallestDigit (NaturalNumber n)Explanation / Answer
This may work, but actually, still there is a minute issue here....:
class SmallestDigitNumberUsingNaturalNumberKernel
{
/**
* Reports the minimum (i.e., smallest) single digit in n when it
* is written in ordinary decimal notation.
* ...
* @ensures
* smallestDigit = [the smallest digit in n]
*/
private static int smallestDigit(NaturalNumber n)
{
if(!n.isZero())
return n.divideBy10() > smallestDigit(n/10) ? smallestDigit(n/10) : n.divideBy10();
return 0;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.