Write a recursive method body for the method whose contract is given below. Note that n is a restores-mode parameter. As an example, if n = 314, the method should return 1. You can only use the NaturalNumber kernel methods multiplyBy10, divideBy10, and isZero.
/**
* Reports the minimum (i.e., smallest) digit of n when it is
* written in ordinary decimal notation.
* ...
* @ensures
* minDigit = [the minimum digit of n]
*/
private static int minDigit(NaturalNumber n) {
// Write code for here.
}
public class Nat {
private static int minDigit(NaturalNumber n){
if(n.isZero())return 0; // checks if n is zero or not;
int min = n.divideBy10(); // min stores the remainder when n is divided by 10 and updates n = n/10
int temp = minDigit(n); // get the minimum digit for updated n i.e n/10
if(temp < min){ // compare both digit and find the minimum;
min = temp;
}
return min; // return the minimum
}
}
Get Answers For Free
Most questions answered within 1 hours.