Recursion Exercises
These exercises provide practice with recursion in Java. Please add notes if possible.
Objectives
Module:
Reminders during development
Requirements
1 |
Write a recursive method that given an array of numbers will return the smallest element in the array. If given an array of length 0 the method will throw an IllegalArgumentException. Your method should have the following header: public int smallest(int[] smallest) { //do something } Test cases: smallest([16, 4, 12, 2, 10]); //2 |
2 |
Write a recursive method that when given a positive integer will report whether the number is odd or even. You may not use the modulus operator in this case. Instead you should use the other arithmetic operators and recursion to discover whether the number is odd or even. Hint: Try using the difference operator repeatedly. Your method should have the following header: public boolean oddOrEven(int num) { //do something } Test cases: oddOrEven(673); //true (odd) |
Here is the completed code for these methods. 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
//method to find the smallest value in an array
public int smallest(int[] smallest) {
// if array length is 0, throwing an exception
if (smallest.length == 0) {
throw new IllegalArgumentException();
}
// otherwise calling private helper method to do the recursion and find
// the smallest value
return smallest(smallest, 0);
}
// helper method for above method to find the smallest value in an array,
// starting from pos position
private int smallest(int[] array, int pos) {
// if pos reached array end, returning last element
if (pos == array.length - 1) {
return array[pos];
}
// storing element at pos
int value = array[pos];
// finding and storing the smallest element starting from next position,
// recursively
int minRemaining = smallest(array, pos + 1);
// returning the minimum value among value and minRemaining
if (value < minRemaining) {
return value;
} else {
return minRemaining;
}
}
// recursive method to check if num is odd or even. returns true if num is
// odd, else false
public boolean oddOrEven(int num) {
// if num is negative, throwing an exception
if (num < 0) {
throw new IllegalArgumentException();
}
// if num is 0, returning false (even)
if (num == 0) {
return false;
}
// if num is 1, returning true (odd)
if (num == 1) {
return true;
}
// calling the method recursively, passing num-2, which will eventually
// end up in either 0 or 1, so that any one of the base cases will be
// executed
return oddOrEven(num - 2);
}
Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives...
Recursion Exercises These exercises provide practice with recursion in Java. Objectives Module: To write recursive solutions for basic problems that require iteration. To identify and address base cases in a recursive method. Reminders during development Each of your solutions to the problems below should be in their own method. If a method header is provided for a problem, then your solution should have that exact method header. Any changes to the method header will receive a zero for that problem....
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header: void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...
4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...
Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, compute the largest number among the numbers that are divisible by 2, count even numbers, and compute the...
Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with alternating signs. // For example, sumTerms(7) returns the following: 1/1 – 1/2 + 1/3 – 1/4 + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign. ...
Arizona State University - CSE205 Assignment #9 Due Date Friday, March April 3rd, 5:30pm Important: This is an individual assignment. Please do not collaborate. No late assignment will be accepted. Make sure that you write every line of your code. Using code written by someone else will be considered a violation of the academic integrity and will result in a report to the Dean's office. It must be submitted on-line (Course website). Go to "GradeScope" tab on Canvas -> CSE205...
A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...
JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...
Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...