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
What happens if i unplug my alarm system?
Dafna11 [192]
It loses power and most likely stops working
6 0
3 years ago
When companies charge different prices for the same product, they're using
Maksim231197 [3]

Answer: When companies charge different

prices for the same product, they're using

B.) Dynamic Pricing

Is the most accurate

Explanation: If a firm can find a way to charge every customer the price he/she values a good at, the firm can capture more profits than it could with a single price, in a given market.

7 0
2 years ago
How to convert mkv to avi without losing quality?
Alexus [3.1K]
Ive done this before with success.
You will need <span>FFmpeg.</span>
https://superuser.com/questions/227338/how-to-convert-an-mkv-to-avi-with-minimal-loss
7 0
3 years ago
A(n) __________ is a popular way to describe relationships? (i.e., one-to-one,? one-to-many) in a relational database.
Leto [7]
A link is a popular way to describe relationships in a relational database.
There are three types of relationships (links) between tables:
1. One-to-one <span>relationship , that allows only one record on each side of the relationship.
2. </span>One-to-many <span>relationship, that allows a single record in one table to be related to multiple records in another table.
3. </span>Many-to-many<span> relationship, in which many records in a table can link to many records in another table. F</span>
6 0
3 years ago
Use the single-server drive-up bank teller operation referred to in Problems 1 and 2 to determine the following operating charac
elena-s [515]

Answer:

This question is incomplete, here's the complete question:

1. Willow Brook National Bank operates a drive-up teller window that allows customers to complete bank transactions without getting out of their cars. On weekday mornings, arrivals to the drive-up teller window occur at random, with an arrival rate of 24 customers per hour or 0.4 customers per minute. 3. Use the single-server drive-up bank teller operation referred to in Problems 1 to determine the following operating characteristics for the system: a.) The probability that no customers are in the system. b.) The average number of customers waiting. c.) The average number of customers in the system. d.) The average time a customer spends waiting. e.) The average time a customer spends in the system. f.) The probability that arriving customers will have to wait for service.

Explanation:

Arrival rate \lambda = 24 customers per hour or 0.4 customers per minute

Service rate \mu​ = 36 customers per hour or 0.6 customers per minute (from problem 1)

a.) The probability that no customers are in the system , P0 = 1 - \lambda / \mu

= 1 - (24/36) = 1/3 = 0.3333

b.) The average number of customers waiting

Lq = \lambda^2 / [\mu(\mu - \lambda)] = 242 / [36 * (36 - 24)] = 1.33

c.) The average number of customers in the system.

L = Lq + \lambda / \mu = 1.33 + (24/36) = 2

d.) The average time a customer spends waiting.

Wq = \lambda / [\mu(\mu - \lambda)] = 24 / [36 * (36 - 24)] = 0.0555 hr = 3.33 min

e.) The average time a customer spends in the system

W = Wq + 1/\mu = 0.0555 + (1/36) = 0.0833 hr = 5 min

f.) The probability that arriving customers will have to wait for service.

= 1 - P0 = 1 - 0.3333 = 0.6667

3 0
3 years ago
Other questions:
  • A(n ________ is a program that is used to create, process, and administer a collection of self-describing integrated records.
    14·1 answer
  • Cobbling together elements from the previous definition and whittling away the unnecessary bits leaves us with the following def
    7·1 answer
  • How should students approach the decision to take the SAT or the ACT?
    6·1 answer
  • What would you recommend for data segregation if using cloud software
    12·2 answers
  • Define a function that takes two arguments: a string called strText and a number called intNumber. This function will use a repe
    12·2 answers
  • Which of the following software maintenance steps erases gaps caused by deleting files from the hard drive?
    10·1 answer
  • When you gather primary or secondary data, what part of the market information management process are you participating in?
    8·1 answer
  • Members of the sales team use laptops to connect to the company network. While traveling, they connect their laptops to the inte
    15·1 answer
  • What does an effect allow you to do in<br> EarSketch?
    15·1 answer
  • Help please i will give Brainliest
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!