Write the following Scheme functions in Java A digitinc4 ta
Write the following Scheme functions in Java: A. \"digitinc4\" - takes as input a 4-digit integer, and returns another 4-digit integer constructed of the original input integer\'s digits, each incremented. For example, (digitinc4 4833) would return 5944. When a digit is a 9, the corresponding output digit should be a 0. For example, (digitinc4 4892) would return 5903. You can assume that the input number is at least 1000 - that is, there are no leading zeros. (*** HINT: note that simply adding \"1111\" is not a correct solution, because when one of the numbers is a \"9\", it would result in a carry, and the digit to its left would get incremented twice.) B. Extend your answer in problem (A), above, to handle input integers with an arbitrary number of digits. This function should be named \"digitinc\" (without the \"4\" in the name). For example, (digitinc 83) would return 94, and (digitinc 22897) would return 33908.
Solution
public static final int[] addOne(int[] digits) {
int carry = 1;
int[] result = new int[digits.length];
for (int i = digits.length - 1; i >= 0; i--) {
int val = digits[i] + carry;
result[i] = val % 10;
carry = val / 10;
}
if (carry == 1) {
result = new int[digits.length + 1];
result[0] = 1;
}
return result;
}
public int[] plusOne(int[] digits) {
int[] toRet=new int[digits.length+1];
int[] temp=null;
for(int i=toRet.length-1; i>=0; i--){
if(i-1>=0){
toRet[i]=digits[i-1];
}
}
for(int i=toRet.length-1; i>=0; i--){
if(toRet[i]+1 == 10){
toRet[i]=0;
}
else{
toRet[i] = toRet[i]+1;
break;
}
}
if(toRet[0]==0){
temp = new int[toRet.length-1];
for(int i=1; i<toRet.length;i++){
temp[i-1] = toRet[i];
}
return temp;
}
else{
return toRet;
}
}
