Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive method named repeatString that accepts a string and an integer

ID: 3718193 • Letter: W

Question

Write a recursive method named repeatString that accepts a string and an integer n as parameters and that returns the string concatenated n times. For example, the call of repeatString("hello", 3) returns "hellohellohello". Do not use any loops; you must use recursion Constraints: Your method should throw an IllegalArgumentException if passed any negative value for n. You are not allowed to construct any structured objects other than Strings (no array, List, Scanner, etc.) and you may not use any loops to solve this problem; you must use recursion Type your Java solution code here: This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above. | 4 | Indent Sound F/X Highlighting Submit

Explanation / Answer

public String repeatString(String str , int n) throws IllegalArgumentException

{

    // if n is negative

    if( n < 0 )

        throw new IllegalArgumentException();

   

    if( n == 0 )

        return "";

   

    // recursively concatenate (n-1) string

    return str + repeatString( str , n - 1 );

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote