Question

Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced...

Intro to JAVA see below:

-----------------------------------------------

Rewrite the algorithm for computing the maximum using an enhanced for loop. Complete the following code:

CODE:

import java.util.ArrayList;

public class ArrayListUtil
{
   /**
      Computes the maximum value of a nonempty array list.
      @param values a non-empty array list
      @return the largest value in the array list
   */
   public static double maximum(ArrayList<Double> values)
   {
      // Compute the largest value, using an enhanced for loop
      . . .
      for (. . . : . . .)
      {
         . . .
      }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

import java.util.Random;

public class ArrayListUtil {

public static void main(String[] args) {

// create instance of Random class

Random r = new Random();

  

ArrayList<Double> arrayList=new ArrayList<>();

//Creating ArrayList of 10 elements with random values

for(int i=0;i<10;i++)

{

arrayList.add((r.nextDouble()*100)); //multiply with 100 because it is generating too small values

}

//printing the arrayList

System.out.println(arrayList);

  

//printing the maximum number by calling the method maximum()

System.out.println("Maximum nuumber is : "maximum(arrayList));

}

public static double maximum(ArrayList<Double> values)

{

double max= values.get(0); //storing first element of ArrayList

for(double var :values) //enhanced for loop

{

if(var>max) //comparing the values

{

max=var; //saving the largest value in max variable

}

}

return max;

}

}

Output:

[62.687960432641695, 37.9760416525705, 64.93375388379197, 62.36734779294045, 84.4402779196424, 79.81372841881966, 78.46949647816666, 14.467514688378348, 46.86527059244102, 56.290937510586545]
Maximum nuumber is : 84.4402779196424

Add a comment
Know the answer?
Add Answer to:
Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT