24.3k views
0 votes
Assume the availability of a function called printStars. The function receives an int argument. If the argument is positive, the function prints (to standard output) the given number of asterisks. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed. Assume further that the variable starCount has been declared and initialized to a some integer, possibly negative or zero. Write some code that does nothing if starCount is not positive but that otherwise prints starCount asterisks to standard output by: first printing a single asterisk (and no other characters) then calls printStars to print the remaining asterisks.

User Tanzmaus
by
3.3k points

1 Answer

7 votes

Final answer:

To print asterisks based on the value of 'starCount', you must first check if it is positive. If so, print one asterisk and then call 'printStars(starCount - 1)' to print the rest.

Step-by-step explanation:

To address the scenario wherein the function printStars needs to be called only if starCount is a positive number, we must first check the value of starCount. If starCount is greater than zero, we print a single asterisk and then call printStars with starCount - 1 to print the remaining asterisks. Here's how this could be coded:

if (starCount > 0) {
System.out.print('*'); // Prints a single asterisk
printStars(starCount - 1); // Calls the function to print the rest
}

This code snippet checks if starCount is positive and acts accordingly by printing an initial asterisk, followed by the remaining asterisks using the printStars function.

User Formentz
by
2.7k points