// POSSIBLE SOLUTION #1
public static String doubles(String str) {
// if we're dealing with a string with a single character, the answer
is just that
// doubled
if (str.length() == 1) return str+str;
// otherwise, our answer is the first character doubled + the rest of
the string
// doubled
else return
doubles(str.substring(0,1))+doubles(str.substring(1,str.length()));
}
public static String doubles3(String str) {
// if we're dealing with a string with a single character, the answer
is just that
// doubled
if (str.length() == 1) return str+str;
// otherwise, our answer is the first character doubled + the rest of
the string
// doubled
else return
doubles3(str.substring(0,str.length()-1))+doubles3(str.substring(str.length()-1,str.length()));
}
// POSSIBLE SOLUTION #2
public static String doubles1(String str, int count) {
// if our counter is the same as the length, we're at the end of the
string
// so we don't want to add anything more to the result
if (count == str.length()) return "";
// otherwise, add the current character (doubled) to a recursive call
// where we increment the counter
else return str.substring(count,count+1) + str.substring(count,count+1)+
doubles1(str, count+1);
}
// now give us a helper method to handle the fact we're only supposed
to have
// a single parameter, start our counter at 0
public static String doublehelped(String str) {
return doubles1(str,0);
}
// POSSIBLE SOLUTION #3
public static String doubles2(String str, String result, int count) {
// if our counter is the same as the length, we're done so return the
answer
// we have built up in our tail
if (count == str.length()) return result;
// otherwise call ourself recursively incrementing the counter, and
adding
// the current character (twice) to the result
else return doubles2(str, result+str.substring(count,count+1) +
str.substring(count,count+1), count + 1);
}
// now give us a helper method to handle the fact we're only supposed
to have
// a single parameter, start the counter at 0, and start the result as
an
// empty string
public static String doublehelped2(String str) {
return doubles2(str,"",0);
}
// The other lab problem
public static int sqrt(int number, int valuetotry) {
// if the value we're trying is the squareroot or larger, we're done
if (valuetotry * valuetotry >= number) return valuetotry;
// otherwise, call again while incrementing the valuetotry
else return sqrt(number, valuetotry + 1);
}
// now give us a helper method to handle the fact we're only supposed
to have
// a single parameter, and start by trying 1
public static int squarert(int number) {
return sqrt(number, 1);
}