Write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter. The function twoWords takes two parameters:

i. an integer, length, that is the length of the first word and

ii. a character, firstLetter, that is the first letter of the second word. The second word may begin with either an upper or lower case instance of firstLetter. The function twoWords should return the two words in a list. Use a while True loop and a break statement in the implementation of twoWords.

The following is an example of the execution of twoWords:

print(twoWords(4, 'B')) A 4-letter word please :two

A 4-letter word please: one

A 4-letter word please : four

A word beginning with B please : apple

A word beginning with B please: pear

A word beginning with B please: banana

['four', 'banana']

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!

Ver imagen stigawithfun

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

ACCESS MORE
EDU ACCESS
Universidad de Mexico