142k views
7 votes
Fill in this function that takes three parameters, two Strings and an int. 4- // Write some test function calls here! The Strings are the message that should be printed. You should alternate printMessage("Hi", "Karel", 5); between the Strings after each line. The int represents the total number of lines that should be printed. public void printMessage(String lineOne, String lineTwo, in For example, if you were to call 19.

printMessage("Hi", "Karel", 5);
// Start here! 12 the function should produce the following output:
Hi
Karel
Hi
Kare

User Tolani
by
3.5k points

1 Answer

8 votes

Answer:

The function in Java is as follows:

public static void printMessage(String lineOne, String lineTwo, int lines){

for(int i = 1;i<=lines;i++){

if(i%2 == 1){

System.out.println(lineOne);

}

else{

System.out.println(lineTwo);

}

}

}

Step-by-step explanation:

This defines the function

public static void printMessage(String lineOne, String lineTwo, int lines){

This iterates through the number of lines

for(int i = 1;i<=lines;i++){

String lineOne is printed on odd lines i.e. 1,3,5....

if(i%2 == 1){

System.out.println(lineOne);

}

String lineTwo is printed on even lines i.e. 2,4,6....

else{

System.out.println(lineTwo);

}

}

}

To call the function from main, use:

printMessage("Hi", "Karel", 5);

User JefferyRPrice
by
3.4k points