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

Write a method called makeLine . The method receives an int parameter that is gu

ID: 3653797 • Letter: W

Question

Write a method called makeLine . The method receives an int parameter that is guaranteed not to be negative and a character. The method returns a String whose length equals the parameter and contains no characters other than the character passed. Thus, if the makeLine(5,':') will return ::::: (5 colons). 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 the specified character with the string returned by an appropriately formulated recursive call to itself.

Explanation / Answer

public class RecusiveCalls { public static void main(String[] args) { String str = ""; str = MakeLine(5, "#"); System.out.println(str); } public static String MakeLine(int size, String str) { if (size == 1) { return str; } else { return str + MakeLine(size-1, str); } } }
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