Write a java program that has three static recursive methods and a main method.
ID: 3658862 • Letter: W
Question
Write a java program that has three static recursive methods and a main method. In your main method prompt for user input and display the results of each of your recursive methods as shown in the sample output below. Write a method that uses recursion to compute the value of an, where a and n are both arguments to the method. If n = 0, the method should return 1 as a0 = 1. If n = 1, the method should return a as a1 = a. If n is any other number ... that's for you to determine but remember, an = a times an-1. Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. Hint: For base cases, consider a string that has 1 or fewer characters...how much work is there to reverse them? Otherwise a reversed string is the last letter of the original string plus the reverse of the rest. Try it out on paper first. Write a recursive method that determines the Greatest Common Divisor (GCD) of two integers (passed in as arguments). Here is a pseudo-code version of a non-recursive algorithm to help you: For both of these questions and with recursion in general, the trick is to determine 1) the base case(s) that will cause the function to simply return and end the recursion and 2) the recursive case(s) that will cause the function to call itself with a smaller version of the same problem. All input and output should take place in your main method. *** Don't forget to "eat the new line" if you use your scanner between numbers and strings.Explanation / Answer
/*if you face any problem in reading the code, then you may also download my code from.....http://www.2shared.com/file/HTAoF_A3/ThreeRecursions.html*/ import java.util.Scanner; public class ThreeRecursions { public static void main(String[] args) { int base, exp; int answer; String normal; String rev = ""; int first; int second; Scanner scan = new Scanner(System.in); System.out.println("*******Demostration Of Power Program!******* "); System.out.println("Enter the base you would like raised to a power: "); base = scan.nextInt(); System.out.println("Enter the power you would like it raised to: "); exp = scan.nextInt(); answer = power(base, exp); System.out.println(base + " raised to the " + exp + " is " + answer); System.out .println(" *******Demostration Of Reverse of String******* "); System.out.println("Enter the string to be reversed :"); normal = scan.next(); System.out.println("Reverse Of the string is :" + revString(normal, rev)); System.out.println(" *******Demostration Of GCD******* "); System.out.println("Enter the first number :"); first = scan.nextInt(); System.out.println("Enter the second number :"); second = scan.nextInt(); answer = calGCF(first, second); System.out.println("GCD of two number is :" + answer); System.out.println(); } public static int power(int base, int exp) { if (exp == 0) { return 1; } else { base = base * power(base, --exp); } return base; } static String revString(String str, String rev) { if (str.length() == 0) { return rev; } else { rev = rev + str.charAt(str.length() - 1); str = str.substring(0, str.length() - 1); return revString(str, rev); } } static int calGCF(int i, int j) { int temp; int rem; if (j > i) { temp = i; i = j; j = temp; } rem = i % j; i = j; j = rem; if (rem == 0) return i; else return calGCF(i, j); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.