Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Recursion Exercises These exercises provide practice with recursion in Java. Please add notes if possible. Objectives Module: To write recursive solutions for basic problems that

Recursion Exercises

These exercises provide practice with recursion in Java. Please add notes if possible.

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

  • 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 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 smallest([-2, 0, 3, -3, 2, -3, -1]); //-3 smallest([]); //results in an exception

  • 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) oddOrEven(18); //false (even) oddOrEven(19); //true (odd)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. Describe the relationship between history and identity.

Answered: 1 week ago