Answer:
// Scanner import to allow user enter input
import java.util.Scanner;
// Class definition
public class ShampooMethod {
// The beginning of program execution which is the main method
public static void main(String args[]) {
//Scanner object to receive user input via the keyboard
Scanner scan = new Scanner(System.in);
//The received input is assigned to userCycles variable
int userCycles = scan.nextInt();
//printShampooInstructions method is called with userCycles as argument
printShampooInstructions(userCycles);
}
// The printShampooInstructions is defined
public static void printShampooInstructions(int numCycles){
// Check if numCycles is less than 1 and output "too few"
if(numCycles < 1){
System.out.println("Too few...");
} else if (numCycles > 4){
System.out.println("Too many...");
} else{
// for-loop which repeat for the range of numCycles and display the instruction
for(int i = 1; i <= numCycles; i++){
if(i == numCycles){
System.out.println(i + ":" + " Lather and rinse. Done.");
} else{
System.out.println(i + ":" + " Lather and rinse.");
}
}
}
}
}
Explanation:
The code has been commented to be explanatory. First the Scanner class is imported, then the class is defined. Then, the main method is also declared. Inside the main method, the user input is accepted and assigned to userCycles. The userCycles is passed as argument to the printShampooInstructions.
The printShampooInstructions is created with a parameter of numCycle and void return type. If the numberCycle is less than 1; "Too few..." is outputted. If numCycle is greater than 4; "Too many" is displayed. Else the shampoo instuction "Lather and rinse" is repeated for the numCycle. During the last repetition, "Done" is attached to the shampoo instruction.