In: Computer Science
Please submit a Netbeans project with a class called RecursionExercises.java implementing the following methods:
public static int reverse(int number)
This method returns the positive integer obtained when the digits of the parameter is reversed. For example, when called with the parameter 12345, this method would return 54321. (Do this with proper integer arithmetic instead of just simply converting to String, reversing that and then using parseInt to extract the result).
Hint: The number of digits of a number can be obtained by taking the integer part of the logarithm in base 10 of the number + 1 (i.e, num digits = log10(n) + 1).
public static int countPaths(int n, int m)
You are standing at the point (n,m) of the grid of positive integers and want to go to origin (0,0) by taking steps either to left or down: that is, from each point (n,m) you are allowed to move either to the point (n-1,m) or the point (n,m-1). Implement the method that based on recursion counts the number of different paths from the point (n,m) to the origin.
public static String ordString(int number)
Write a recursive method that given an integer number n that returns a String based on the following pattern:
number = 0, method returns {}
number = 1, method returns {{}}
number = 2, method returns {{}, {{}}}
number = 3, method returns {{}, {{}}, {{}, {{}}}}
In other words, given number > 0, the ordString method returns all ordStrings from 1 to number. Write a recursive method that accomplish this.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// RecursionExercises.java
public class RecursionExercises {
// recursive method to reverse a number, assuming number is positive
public static int reverse(int number) {
// if number is 0, returning 0 (base case 1)
if (number == 0) {
return 0;
}
// finding number of digits
int numDigits = (int) (Math.log10(number) + 1);
// if number of digits is 1, returning number (base case 2)
if (numDigits == 1) {
return number;
}
// extracting last digit
int digit = number % 10;
// removing last digit from number
number = number / 10;
// multiplying digit with power of 10^(digits-1) and adding the reverse
// of number to it, returning this value
return (int) ((digit * Math.pow(10, numDigits - 1)) + reverse(number));
}
// method to count all different paths from (n,m) to (0,0)
public static int countPaths(int n, int m) {
// if n or m is outside the bounds, returning 0 (base case 1)
if (n < 0 || m < 0) {
return 0;
}
// if n and m are 0, we reached the destination, returning 1 (base case
// 2)
if (n == 0 && m == 0) {
return 1;
}
// counting number of paths going left and number of paths going down
// from this point, summing and returning the result
return countPaths(n - 1, m) + countPaths(n, m - 1);
}
// returns the pattern based on number, assuming number is always positive
public static String ordString(int number) {
// if number is 0, returning "{}"
if (number == 0) {
return "{}";
}
String temp = "";
// looping from i=0 to i=number-1
for (int i = 0; i < number; i++) {
// if this is not the first value, appending a comma and space to
// separate the ordStrings
if (i > 0) {
temp += ", ";
}
// appending ordString of i to temp
temp += ordString(i);
}
// wrapping temp inside a pair of parenthesis and returning the result
return "{" + temp + "}";
}
public static void main(String[] args) {
// testing
System.out.println("Reverse of 12304 is " + reverse(12304));
System.out.println("Count paths from (3, 2): "+countPaths(3,2));
System.out.println("ordString (3): "+ordString(3));
}
}
/*OUTPUT*/
Reverse of 12304 is 40321
Count paths from (3, 2): 10
ordString (3): {{}, {{}}, {{}, {{}}}}