Write a method called makeStars. The method receives an int parameter that is guaranteed not to be negative. The method returns a 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.

Write a method called makeStars. The method receives an int parameter that is guaranteed not to be negative. The method returns a 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.

LANGUAGE: JAVA

CHALLENGE:

Write a method called makeStars. The method receives an int parameter that is guaranteed not to be negative. The method returns a 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.

SOLUTION:


public String makeStars(int numberOfStars){
   if (numberOfStars == 0) return "";
   else return "*" + makeStars(numberOfStars - 1);
}

Add comment