Respuesta :
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!
The function is an illustration of loops.
Loops are used to perform repetitive operations.
The program in Python, where comments are used to explain each line is as follows:
#This defines the function
def twoWords(length, firstLetter):
#This creates an empty list
myList = []
#This gets input for the first word
word = input("A "+str(length)+"-letter word please: ")
#This loop ensures that the input is valid
while(True):
#This breaks the loop when the input is valid
if(len(word) == length):
break;
word = input("A "+str(length)+"-letter word please: ")
#This appends the valid word to the list
myList.append(word)
#This gets input for the second word
word = input("A word beginning with "+firstLetter+" please :")
#This loop ensures that the input is valid
while(True):
#This breaks the loop when the input is valid
if word[0].lower() == firstLetter[0].lower():
break;
word = input("A word beginning with "+firstLetter+" please :")
#This appends the valid word to the list
myList.append(word)
#This returns the list
return myList
At the end of the program, only the valid inputs are appended to the list.
Read more about similar programs at:
https://brainly.com/question/14054848