
CS 149: Introduction to Programming
James Madison University, Spring 2021 Semester
Homework 8: Recursive Methods
Objectives
- Write a recursive function that computes a value.
- Use recursion to solve string and array problems.
Note: CodingBat is a free site of live problems to build skill in Java and/or Python. It was created by Nick Parlante, who is Computer Science lecturer at Stanford. If you create an account, CodingBat will automatically save your progress online.
Your solutions to these exercises must be recursive. Autolab will automatically reject submissions containing words like and . In addition, you may not use multiplication () in place of recursive addition.
Exercise 8.1 Math Functions
Many interesting functions in mathematics can be defined recursively. The good news is, recursive definitions are fairly easy to implement in Java! Download and implement the following functions:
Don't think too hard about these methods; each one should be less than 10 lines of code. After you get them working, step through your code using a debugger or Java Visualizer to see how they work.
Exercise 8.2 Simple Counts
The rest of this homework is based on CodingBat problems. As explained on the Recursion-1 page, "first test for one or two base cases that are so simple, the answer can be returned immediately. Otherwise, make a recursive a call for a smaller case (that is, a case which is a step towards the base case). Assume that the recursive call works correctly, and fix up what it returns to make the answer."
Complete these problems online first, until you get them 100% correct. Then download the source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.
Exercise 8.3 Running Totals
These problems are a little bit more difficult, because they don't always add the same amount. If you use a variable to store how much to add, then you will only need one method call at the end. That way, you'll have two return statements: one for the base case, and one for the recursive case.
Complete these problems online first, until you get them 100% correct. Then download the source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.
Exercise 8.4 String Recursion
In the previous exercises, you made the problem smaller by decreasing the integer each time. With strings, you can make the problem smaller by using substring in the recursive call. Solve the problem for the first character only, and then recursively solve the rest of the problem.
Complete these problems online first, until you get them 100% correct. Then download the source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.
Exercise 8.5 Array Recursion
CodingBat uses a different strategy for array problems than string problems. Rather than make the array smaller each time, you pass the current index as an argument. The index will be 0 the first time the method is called, and the base case is when you reach the end of the array.
Complete these problems online first, until you get them 100% correct. Then download the source file and paste your code into the corresponding method stubs. Resolve any Checkstyle errors, and submit your final code to Autolab.
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
Recursion is neat in theory and commonly leads to very clean code. Some people have a hard time understanding it, though. If you’ve ever encountered a recurrence relation in mathematics, then you already know everything there is to know about the “mind-bending” nature of recursive problems.
Otherwise, try this simple strategy. First, you may be tempted to think recursively about recursive functions. This may work with factorials, but for something more complex like, say, the Ackermann function, it’s a recipe for disaster. So, don’t do it! Instead, figure out what the base case consists of. The base case immediately returns a result. All other conditions eventually have to revert to the base case, which means that you’ll return “something” in addition to recursively calling the function, but with an input that brings you closer to the base case.
The first example, factorial, of CodingBat’s Recursion-1 section illustrates this strategy very well. All subsequent problems are rather similar.
All solutions were successfully tested on 24 March 2013.
factorial:
public int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); }bunnyEars:
public int bunnyEars(int bunnies) { if (bunnies == 0) return 0; return 2 + bunnyEars(bunnies - 1); }fibonacci:
public int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 2) + fibonacci(n - 1); }bunnyEars2:
public int bunnyEars2(int bunnies) { if (bunnies == 0) return 0; if (bunnies % 2 == 1) return 2 + bunnyEars2(bunnies - 1); return 3 + bunnyEars2(bunnies - 1); }triangle:
public int triangle(int rows) { if (rows == 0) return 0; return rows + triangle(rows - 1); }sumDigits:
public int sumDigits(int n) { if (n == 0) return 0; return n % 10 + sumDigits(n / 10); }count7:
public int count7(int n) { if (n == 0) return 0; if (n % 10 == 7) return 1 + count7(n / 10); return count7(n / 10); }count8:
public int count8(int n) { if (n == 0) return 0; if (n >= 88 && n % 100 == 88) return 2 + count8(n / 10); if (n % 10 == 8) return 1 + count8(n / 10); return count8(n / 10); }powerN:
public int powerN(int base, int n) { if (n == 0) return 1; return base * powerN(base, n - 1); }countX:
public int countX(String str) { if (str.length() == 0) return 0; if (str.charAt(0) == 'x') return 1 + countX(str.substring(1)); return countX(str.substring(1)); }For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
Recursion-1
CODING BAT ANSWERS IS MOVING TO A NEW AND IMPROVED SITE, PLEASE CLICK HERE TO VIEW SOLUTIONS TO EVERY JAVABAT PROBLEM AND LEARN FROM MY MISTAKES!!!!
This section includes these questions: factorial, bunnyEars, fibonacci, bunnyEars2, triangle, sumDigits, count7, count8, powerN, countX, countHi, changeXY, changePi, noX, array6, array11, array220, allStar, pairStar, endX countPairs, countAbc, count11, stringClean, countHi2, parenBit, nestParen, strCount, strCopies, strDist.
factorial
public int factorial(int n) { if( n == 1) { return 1; } else { return factorial(n-1) * n; } }bunnyEars
public int bunnyEars(int bunnies) { if( bunnies == 0 ) return 0; else return bunnies + bunnies; }fibonacci
public int fibonacci(int n) { if(n == 0) return 0; if(n == 1) return 1; else { return fibonacci(n - 2) + fibonacci(n - 1); } }bunnyEars2
public int bunnyEars2(int bunnies) { if( bunnies == 0) return 0; else return bunnies + bunnies + (bunnies / 2); }triangle
public int triangle(int rows) { if ( rows == 0) return 0; if (rows == 1) return 1; else return triangle(rows - 1) + rows; }sumDigits
public int sumDigits(int n) { int sum; if ( n == 0) return 0; else { sum = n % 10; n = n / 10; return sumDigits( n) + sum; } }count7
public int count7(int n) { int sum; if( n == 0) return 0; else { sum = n % 10; n = n / 10; if (sum == 7) return 1 + count7(n); else return count7(n); } }count8
public int count8(int n) { int sum; if ( n == 0) return 0; else { sum = n % 10; n = n / 10; if ( sum == 8 && n % 10 == 8 ) return 2 + count8(n); if ( sum == 8) return 1 + count8(n); else return count8(n); } }powerN
public int powerN(int base, int n) { if ( n == 1) return base; if ( n == 0) return 0; else return base * powerN( base, n - 1); }countX
public int countX(String str) { int result = 0; if(str.length() == 0) { return 0; } if(str.charAt(0) == 'x') { result++; } result += countX(str.substring(1, str.length())); return result; }countHi
public int countHi(String str) { int result = 0; if(str.length() <= 2) { if(str.equals("hi")) { return 1; } return 0; } if(str.substring(0, 2).equals("hi")) { result++; str = str.substring(2, str.length()); } else { str = str.substring(1, str.length()); } result += countHi(str); return result; }changeXY
public String changeXY(String str) { String result = ""; if(str.length() == 0) { return ""; } if(str.charAt(0) == 'x') { result = "y"; } else { result = Character.toString(str.charAt(0)); } return result + changeXY(str.substring(1, str.length())); }changePi
public String changePi(String str) { String result = ""; if(str.length() < 2) { return str; } if(str.charAt(0) == 'p' && str.charAt(1) == 'i') { result = "3.14"; str = str.substring(2, str.length()); } else { result = Character.toString(str.charAt(0)); str = str.substring(1, str.length()); } return result + changePi(str); }noX
public String noX(String str) { String result = ""; if(str.length() == 0) { return ""; } if(str.charAt(0) == 'x') { result = ""; } else { result = Character.toString(str.charAt(0)); } return result + noX(str.substring(1, str.length())); }array6
public boolean array6(int[] nums, int index) { if(index >= nums.length) { return false; } else if(nums[index] == 6) { return true; } else { return array6(nums, index + 1); } }array11
public int array11(int[] nums, int index) { int result = 0; if(index >= nums.length) { return 0; } if(nums[index] == 11) { result++; } result += array11(nums, index + 1); return result; }array220
public boolean array220(int[] nums, int index) { if(index >= nums.length - 1) { return false; } else if(nums[index] * 10 == nums[index + 1]) { return true; } else { return array220(nums, index + 1); } }allStar
public String allStar(String str) { String result = ""; if(str.length() == 1 || str.length() == 0) { return str; } else { result += (Character.toString(str.charAt(0)) + "*"); } result += allStar(str.substring(1, str.length())); return result; }pairStar
public String pairStar(String str) { String result = ""; if(str.length() <= 1) { return str; } if(str.charAt(0) == str.charAt(1)) { result += (Character.toString(str.charAt(0)) + "*"); } else { result += Character.toString(str.charAt(0)); } result += pairStar(str.substring(1, str.length())); return result; }endX
public String endX(String str) { String result = ""; int x = countX(str); result = removeX(str); result += addX(x); return result; } public int countX(String str) { int x = 0; if(str.length() <= 0) { return 0; } if(str.charAt(0) == 'x') { x++; } x += countX(str.substring(1, str.length())); return x; } public String removeX(String str) { String result = ""; if(str.length() <= 0) { return ""; } else if(str.charAt(0) == 'x') { result += ""; } else { result += Character.toString(str.charAt(0)); } result += removeX(str.substring(1, str.length())); return result; } public String addX(int num) { if(num < 1) { return ""; } if(num == 1) { return "x"; } return "x" + addX(num - 1); }countPairs
public int countPairs(String str) { int result = 0; if(str.length() <= 2) { return 0; } if(str.charAt(0) == str.charAt(2)) { result++; } result += countPairs(str.substring(1, str.length())); return result; }countAbc
public int countAbc(String str) { int result = 0; if(str.length() <= 2) { return 0; } if(str.substring(0, 3).equals("aba") || str.substring(0, 3).equals("abc")) { result++; } result += countAbc(str.substring(1, str.length())); return result; }count11
public int count11(String str) { int result = 0; if(str.length() <= 1) { return 0; } if(str.substring(0, 2).equals("11") || str.substring(0, 2).equals("11")) { result++; str = str.substring(2, str.length()); } else { str = str.substring(1, str.length()); } result += count11(str); return result; }stringClean
public String stringClean(String str) { if(str.length() <= 1) { return str; } String result = "", c = Character.toString(str.charAt(0)), n = ""; do { if(n.equals(c)) { str = str.substring(1, str.length()); if(str.length() <= 1) { break; } } n = Character.toString(str.charAt(1)); } while(n.equals(c) && str.length() > 1); if(str.length() < 1) { } else { str = str.substring(1, str.length()); } result += c; result += stringClean(str); return result; }countHi2
public int countHi2(String str) { int result = 0; if(str.length() < 2) { return 0; } else if(str.length() == 2) { if(str.equals("hi")) { return 1; } else { return 0; } } else { if(str.charAt(0) == 'x') { if(str.length() <= 3) { return 0; } else if(str.substring(1, 3).equals("hi")) { str = str.substring(3, str.length()); } else { str = str.substring(1, str.length()); } } else { if(str.substring(0, 2).equals("hi")) { result++; str = str.substring(2, str.length()); } else { str = str.substring(1, str.length()); } } } result += countHi2(str); return result; }parenBit
public String parenBit(String str) { String result = ""; if(str.charAt(0) == '(') { if(str.charAt(str.length() - 1) == ')') { return str; } else { return parenBit(str.substring(0, str.length() - 1)); } } else { return parenBit(str.substring(1, str.length())); } }nestParen
public boolean nestParen(String str) { String result = ""; if(str.length() == 0) { return true; } if(str.length() < 1) { return false; } if(str.charAt(0) == '(') { if(str.charAt(str.length() - 1) == ')') { return nestParen(str.substring(1, str.length() - 1)); } return false; } else { return false; } }strCount
public int strCount(String str, String sub) { int result = 0; if(str.length() < sub.length()) { return 0; } else if(str.length() == sub.length() && str.equals(sub)) { return 1; } else if(str.substring(0, sub.length()).equals(sub)) { result++; str = str.substring(sub.length(), str.length()); } else { str = str.substring(1, str.length()); } result += strCount(str, sub); return result; }strCopies
public boolean strCopies(String str, String sub, int n) { if(strCount(str, sub) == n) { return true; } else { return false; } } public int strCount(String str, String sub) { int result = 0; if(str.length() < sub.length()) { return 0; } else if(str.length() == sub.length() && str.equals(sub)) { return 1; } else if(str.substring(0, sub.length()).equals(sub)) { result++; } str = str.substring(1, str.length()); result += strCount(str, sub); return result; }strDist
public int strDist(String str, String sub) { if(str.length() == 1 && str.equals(sub)) { return 1; } else if(str.length() < sub.length() || str.length() <= 1) { return 0; } else { if(str.charAt(0) == sub.charAt(0) && str.charAt(0) == str.charAt(str.length() - sub.length()) && str.substring(str.length() - sub.length(), str.length()).equals(sub) && str.substring(str.length() - sub.length()).equals(sub)) { return str.length(); } else { if(str.substring(0, sub.length()).equals(sub)) { return strDist(str.substring(0, str.length() - 1), sub); } else if(str.substring(str.length() - sub.length(), str.length()).equals(sub)) { return strDist(str.substring(1, str.length()), sub); } return strDist(str.substring(1, str.length() - 1), sub); } } }This section includes these questions: factorial, bunnyEars, fibonacci, bunnyEars2, triangle, sumDigits, count7, count8, powerN, countX, countHi, changeXY, changePi, noX, array6, array11, array220, allStar, pairStar, endX countPairs, countAbc, count11, stringClean, countHi2, parenBit, nestParen, strCount, strCopies, strDist.
Like this:
LikeLoading...
1 solutions recursion codingbat
Something unreal. He began to push his finger in, and I felt that he had long nails that he did not cut. Well, at least he is not offended that the small one does it.
Recursion - 1 (triangle) Java Solution -- Codingbat.comThey quickly climbed the side ramp launched into the water one after another. And wet from the water, and the sun sparkling in the hot Pacific Ocean. Naked in a bikini girlish young twenty-year-old bodies. They ran past their fathers sitting at the table. They, catching up with each other, went down into the hold rooms of the ocean huge and luxurious yacht into the shower room of the.
Similar news:
- Botw captured memories
- Harry styles tumblr 2020
- Pokemon vr quest 2
- Seattle dot traffic cam
- Toyota service new york
- Bad day synonym
- Naruto fan characters
- 1998 suzuki motorcycle
- Klx250s rear fender mod
- Wells fargo mortgage settlement checks
- Lee hyun bts
Before we met, he managed to drink at least a liter, but for the Siberian organism this is a trifle. Every time a silhouette flashes in the distance, reminiscent of a woman, Ed rushes to him. The silhouette either dissolves in the darkness, or disappears in an approaching car, or even turns out to be an optical illusion. In between, Ed tells me stories about how he usually shoots chicks, how many of them are on the streets now, how they like him and what he does with them.
Afterwards.