a) write a recursive Java method writeLine that writes a character repeatedly to
ID: 3621608 • Letter: A
Question
a) write a recursive Java method writeLine that writes a character repeatedly to form a line of n characters. For example, writeLine (' * ',5,3) produces the line*****
b) now write a recursive method writeBlock that uses writeLine to write m lines of n characters each. For example, writeBlock(' * ',5,3) produces the output
*****
*****
*****
Explanation / Answer
public class Recurse { public static void writeLine(char letter, int amount) { if (amount > 0) { System.out.print(letter); writeLine(letter, amount -1); } } public static void writeBlock(char letter, int amount, int lines) { if (lines > 0) { writeLine(letter, amount); System.out.println(" "); writeBlock(letter, amount, lines - 1); } } public static void main(String[] args) { writeBlock('*', 10, 3); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.