1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Bumek [7]
3 years ago
8

Complete the method, longestWord(), below that takes in an array of Strings and returns the longest String in the array. Note: y

ou may assume the Strings in the array are of different lengths. For example, if the given array is: ["cat", "horse", "elephant", "bird"] then the method should return "elephant"

Computers and Technology
1 answer:
beks73 [17]3 years ago
8 0

Answer:

I am writing a JAVA program.

public class StringMethod {

public static String longestWord(String []words) { //method that takes an array  "word" of strings and returns the longest string in the array word

    int pos = 0; // contains index of the longest word of array words

    int longest = words[0].length(); //initially holds the length of the 1st element of words array

    for(int i=1; i< words.length; i++) { // iterates through each word of the array until the end of array

        if(words[i].length() > longest) { // if the length of word at i-th index of words[] is greater than the length of the word that is contained in longest variable

            pos = i; // assigns the index of the longest array element to pos

            longest = words[i].length();    } } //assigns the word in words[] array with the longest length to longest variable

   return words[pos]; } // returns the longest string in the array    

public static void main(String[] args) { //start of main() function body

String[] animals = {"cat", "horse", "elephant", "bird"}; //creates a String type array named animals which has 4 elements/words

   String longest_word= longestWord(animals); //calls longestWord method to find the longest string in the array animals and assign that longest string to longest_word.

  System.out.println(longest_word); }} //displays the longest word in output

Explanation:

Suppose we have the array of these elements: "cat", "horse", "elephant", "bird" These are four words in the array. Now lets see how the method longestWord() works:

int pos = 0; This variable is initialized to 0.

int longest = words[0].length();

longest holds the length of element at 0th index of words[] array. length() method is used to return the length of the first element words[0] array.

As the first element of array is "cat" so length of cat is 3. Hence longest = 3

for(int i=1; i< words.length; i++)

The loop has a variable i which is initialized to 1. The loop checks if the value of i is less than length of words[] array. The length of words array is 4 as there are 4 elements i.e. cat, horse, elephant and bird in this array. So the condition evaluates to true because i<words.length i.e. 1<4. The body of the loop will execute now. The body of loop contains an if condition if(words[i].length() > longest) which checks if the length of element of array at i-th index is greater than length of the element that is stored in longest variable. As i=1 so word[i] is word[1] which is the second element of words[] array. The second element is horse.

words[i].length()  means the length of i-th element of words[] array. So this means words[1].length() i.e. the length of element of words[] at 1st index. The length of horse is 5. So the statement if(words[i].length() > longest) is true because longest=3 and words[1].length() = 5 and 5>3. So the statements of if condition will execute now.

pos = i; this variable pos is used to hold the index position of the longest element in array. So pos = 1 as longest element computed so far is "horse".

longest = words[i].length(); This statement then assigns the length of horse to longest. So now the value of longest = 5

At second iteration i is now 2.  Loop condition evaluates to true because i<words.length i.e. 2<4. If condition is also true as i=2 so word[i] is word[2] which is elephant.

words[i].length() now means words[2].length() i.e. the length of element of words[] at 2nd index. The length of elephant is 8. So the statement if(words[i].length() > longest) is true because longest=5 and words[2].length() = 8 and 8>5. So the statements of if condition will execute now.

pos = i; now becomes pos = 2 as longest element computed so far is "elephant".

longest = words[i].length();  This statement then assigns the length of horse to longest. So now the value of longest = 8

At third iteration i is now 3.  The loop again checks if the value of i is less than length of words[] array.The condition evaluates to true because i<words.length i.e. 3<4. The body of the loop will execute now.  if(words[i].length() > longest) checks if the length of element of array at i-th index is greater than length of the element that is stored in longest variable. As i=3 so word[i] is word[3] which is the fourth element of words[] array. The fourth element is bird.

words[i].length() now means words[3].length() i.e. the length of element of words[] at 3rd index. The length of bird is 4. So the statement if(words[i].length() > longest) is false because longest=8 and words[2].length() = 4 and 4<8. So the statements of if condition will not execute now.

At fourth iteration i is now 4.  The loop again checks if the value of i is less than length of words[] array.The condition evaluates to false because i==words.length i.e. 4=4. So the loop breaks.

 return words[pos]; statement returns the index position of longest word in words[] array. pos = 2 So the array element at 2nd index is basically the third element of array words[] i.e. elephant. So the longest string in the array is elephant.

You might be interested in
Write a program for Horizon Phones, a provider of cellular phone service. Prompt a user for maximum monthly values for talk minu
Anarel [89]

Answer:

mport java.util.Scanner;

public class CellPhoneService

{

    public static void main (String args[])

    {

         Scanner sc=new Scanner(System.in);

         System.out.println("Enter the Talk minutes");

         int talkmin=sc.nextInt();

         System.out.println("Enter the text message");

         int textmsg=sc.nextInt();

         System.out.println("Enter the Gigabyte Data");

         int gbdata=sc.nextInt();

         if(talkmin<500 && textmsg==0 && gbdata==0)

         System.out.println("PLAN A IS RECOMMENDED FOR THIS USER at 49$ per month");

         else if(talkmin<500 && textmsg>0 && gbdata==0)

         {

              System.out.println("PLAN B IS RECOMMENDED FOR THIS USER at 55$ permonth");

         }

         else if(talkmin>=500 && textmsg<100 && gbdata==0)

         {

              System.out.println("PLAN C IS RECOMMENDED FOR THIS USER at 61$ permonth");

         }

         else if(talkmin>=500 && textmsg>=100 && gbdata==0)

         {

              System.out.println("PLAN D IS RECOMMENDED FOR THIS USER at 70$ permonth");

         }

         else if(gbdata>0 && gbdata<2)

         {

              System.out.println("PLAN E IS RECOMMENDED FOR THIS USER at 79$ permonth");

         }

         else if(gbdata>=2)

         {

              System.out.println("PLAN F IS RECOMMENDED FOR THIS USER at 87$ permonth");

         }

    }

}

Explanation:

  • Take the talk minute and text message as input from user.  
  • Apply the conditional statement to check if talk minute is less than 500 along with text message and GB data having a value 0.
  • At last check if GB data is greater than or equal to 2, then print the relevant response.
3 0
3 years ago
If you feel your friend had a negative digital identity, what would you do or tell them in order to help them have a positive di
sladkih [1.3K]
Have them post more positive outcomes and help them understand that what they post stays there forever
4 0
3 years ago
Read 2 more answers
Como hago para poner brainly en español???
Lelu [443]
Mentely es eso espero q te ayude :)
4 0
4 years ago
Read 2 more answers
When does technology become assistive technology?
deff fn [24]
Technology becomes assistive technology C. WHEN SOMEONE WITH A DISABILITY OR LIMITATION USES IT TO HELP HIM OR HER DO SOMETHING.

Assistive technology is defined as any technological item, equipment, software or product system that is used to assist individuals with disabilities to increase, improve, and maintain their functional capabilities.
6 0
3 years ago
Read 2 more answers
Roark has just joined a company and in his role as a lead analyst, he will be responsible for determining which systems developm
hram777 [196]

Answer:

Agile/adaptive methods.

Explanation:

The user has just decided to join a firm and will also be accountable to determine whether that system development technique that group uses to establish the latest software for a significant medical distributor to his position as just a lead analyst.

After that, he consumes a week for the purpose to understand his group members for reason to know their strengths and weakness and how would they work under pressure.

So, the following method is required for him to understand about the disadvantages of each of them.

4 0
3 years ago
Other questions:
  • The EPA requires the use of precise forms called ?
    14·1 answer
  • Why do you think LinkedIn has become so popular?
    6·1 answer
  • USB keys can store terabytes of data. Of the five key factors that contribute to the increasing vulnerability of organizational
    13·1 answer
  • What attracts attention and adds spatial depth to a two-dimensional design.
    9·1 answer
  • Write a program that prompts the user to enter the area of the flat cardboard. The program then outputs the length and width of
    15·1 answer
  • Computer technology has changed our lives write of a least five ways to show how this is so​
    15·1 answer
  • True or False: The major advantage of Arrays over ArrayLists in Java is the fact that while ArrayLists are fixed in size, an Arr
    15·1 answer
  • Need answer Asap!!!! Which file type is the best choice if the image will be made into a billboard?
    12·1 answer
  • How many voltage values can be represented with a 10-bit binary code?
    15·1 answer
  • Does analogue conversation take place in source as transmitter?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!