Question: Write a recursive method called parenthesize that takes a String and an integer n as parameters and that prints the string inside n sets of

Write a recursive method called parenthesize that takes a String and an integer n as parameters and that prints the string inside n sets of parentheses. For example, this code:
parenthesize("Joe",2);
System.out.println(); // to complete line of output
parenthesize("The University of Washington", 6);
System.out.println(); // to complete line of output
parenthesize("midterm",1);
System.out.println(); // to complete line of output
should produce these 3 lines of output:
((Joe))
((((((The University of Washington))))))
(midterm)
Your method should throw an IllegalArgumentException if passed a negative number. It could be passed 0, as in:
parenthesize("CS143, Spring 2010",0);
System.out.println(); // to complete line of output
In this case the output would have no (i.e.,0) parentheses:
CS143, Spring 2010
You are not allowed to construct any structured objects (no array, ArrayList, String, StringBuilder, etc) and you may not use a while loop, for loop, or do/while loop to solve this problem; you must use recursion.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!