Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 accepts a source string and a search string. The method should find all occurrences of the search string within the source string and replace them with an asterisk. The method should then return the new string.

Note: You may not use the replace(), replaceFirst() or replaceAll() methods from the string class. You should be identifying the position of the element using methods like indexOf() and then removing the search value using the substring() method. Note: You may use a loop, in this case only, to generate the sequence of asterisks that will be used to replace the search string. You will still need to use recursion to identify each matching search value.

Your method should have the following header:

public String asterisks(String source, String search) { //do something }

Test cases:

asterisks("beatrice eats each plum", "ea"); //b**trice **ts **ch plum asterisks("catch 22", "22"); //catch ** asterisks("bee been in your bonnet?", "be"); //**e **en in your bonnet? asterisks("are we there yet?", "yes"); //are we there yet?

2

Write a recursive method that accepts a positive number, called height, and prints out a pyramid to the Java console according to the input height.

Note: You may use a loop, in this case only, to generate the current line of asterisks for any level in the pyramid. You will still need to use recursion to move level by level in the pyramid.

Your method should have the following header:

public void pyramid(int height) { //do something }

Test cases:

Inputs Outputs
pyramid(2); * ***
pyramid(3); * *** *****
pyramid(5); * *** ***** ******* *********

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

Step: 3

blur-text-image

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

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions