Question
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
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.
- You may not use fields in support of any of your methods. Anyone using a field to help write the logic of a method will receive a zero for that problem.
- Each problem has several test cases, where the method is invoked and there is some type of output shown. You are required to test these cases with your submission. I should be able to run your code once and clearly see where you make the method calls and the expected output.
- You may write other private recursive methods to support a method that you are required to write. You may not call these methods directly in your tests. You must call the public method described in each problem when testing.
Requirements
- You should submit a single project with all your work for the six problems you were assigned
- It should be easy to see each method in your code submission. Any messy or disorganized submissions will be penalized.
- Any solutions that include any form of loop will not be accepted.
1 | Write a recursive method that reports whether a number is prime or not. Recall that a number is prime if it is only divisible by itself and the number one. Note: Negative numbers, zero and one are not prime by definition. Your method must recognize these values and return a sensible result. Your method should have the following header: public boolean isPrime(int candidate) { //do something } Test cases: isPrime(1); //false isPrime(22); //false isPrime(59); //true isPrime(99); //false |
2 | Write a recursive method that accepts an array of words and returns the longest word found in the array. If the input array is empty then the method should return null. Your method should have the following header: public String longestWord(String[] words) { //do something } Test cases: longestWord(["and", "as", "abba", "are"]); //abba longestWord([]); //null longestWord(["here"]); //here longestWord(["there", "then", "to", "their", "together"]); //together |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started