Answer:
// Method twoWords with two arguments
public static List<String> twoWords(int length, char firstLetter) {
// Create an object of the Scanner class to allow for user inputs
Scanner input = new Scanner(System.in);
// Create a list to hold the two words
List<String> words = new ArrayList<>();
// Create a loop that asks user to enter a particular word with length equal to
// the one specified as first argument.
// If the user enters the correct word,
// insert the word in the created list and break out of this loop
while (true) {
System.out.print("A-" + length + " letter word please: ");
String firstword = input.next();
if (firstword.length() == length) {
words.add(firstword);
break;
}
}
// Create a loop that asks user to enter a particular word whose first character is
// the one specified as second argument.
// If the user enters the correct word,
// insert the word in the created list and break out of this loop
while (true) {
System.out.print("A word beginning with " + firstLetter + " please: ");
String secondword = input.next();
if (secondword.charAt(0) == firstLetter) {
words.add(secondword);
break;
}
}
// Return the list of words
return words;
}
Explanation:
Please go through the comments in the code to get more understanding of the code. The source code file has been attached to this response. Please download it and go through it. You can also run the code on your machine as it contains a full implementation of the method.
Hope this helps!