Quiz 18.4: Problem Solving Using Recursion (string processing) Question 1 of 3 W
ID: 3920408 • Letter: Q
Question
Quiz 18.4: Problem Solving Using Recursion (string processing) Question 1 of 3 Worth 3 Points Write a method called makestars. The method receives an int parameter that is guaranteed not to be negative. The method returnsa String whose length equals the parameter and contains no characters other than asterisks. Thus, makeStars(8) will return(8 asterisks). The method must not use a loop of any kind (for, while, do-while) nor use any String methods other than concatenation. Instead, it gets the job done by examining its parameter, and if zero returns an empty string otherwise returns the concatenation of an asterisk with the string returned by an appropriately formulated recursive call to itself. oExplanation / Answer
public static String makeStars(int n) { if(n == 0) { return ""; } else { return makeStars(n-1) + "*"; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.