Arrays and Variable Length Parameters List

Arrays and Variable Length Parameters List The following Java program application uses Arrays and Variable Length Parameters List. I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
  1. import java.util.*;
  2.  
  3. public class LargestNumber
  4. {
  5. public static void main(String[] args)
  6. {
  7. double[] numberList = {23, 45.5, 89,34, 92.78, 36, 90, 120.89,
  8. 97, 23, 90, 89};
  9.  
  10. System.out.println("The larger 0f 5.6 and 10.8 is: "
  11. + largest(5.6, 10.8));
  12.  
  13. System.out.println("The largest of 23, 78, and 56 is: "
  14. + largest(23,78, 56));
  15.  
  16. System.out.println("The largest number of 93, 28, 83 and 66 is: "
  17. + largest(93, 28, 83, 66));
  18.  
  19. System.out.println("The largest of 22.5 , 12.34, 56.34, 78, "
  20. + "\n"
  21. + "98.45, 25, 78, 23 and 36 is: "
  22. + largest(22.5, 12.34, 56.34, 78, 98.45, 25, 78, 23, 36));
  23.  
  24. System.out.println("The largest number in numList is: "
  25. + largest(numberList));
  26.  
  27. System.out.println("A call to the method largest with empty \n"
  28. + " parameter list returns the value " + largest());
  29.  
  30.  
  31. }
  32. public static double largest(double ... numList)
  33. {
  34. double max;
  35. int index;
  36.  
  37. if (numList.length != 0)
  38. {
  39. max = numList[0];
  40.  
  41. for (index = 1; index < numList.length; index++)
  42. {
  43. if (max < numList [index])
  44. max = numList [index];
  45.  
  46. }
  47. return max;
  48. }
  49. return 0.0;
  50.  
  51. }
  52. }
Sample Run:
  1. The larger 0f 5.6 and 10.8 is: 10.8
  2. The largest of 23, 78, and 56 is: 78.0
  3. The largest number of 93, 28, 83 and 66 is: 93.0
  4. The largest of 22.5 , 12.34, 56.34, 78,
  5. 98.45, 25, 78, 23 and 36 is: 98.45
  6. The largest number in numList is: 120.89
  7. A call to the method largest with empty
  8. parameter list returns the value 0.0
The preceding program works as follows: The method
  1. System.out.println("The larger 0f 5.6 and 10.8 is: "
  2. + largest(5.6, 10.8));
is called with two parameters; Three parameters
  1. System.out.println("The largest of 23, 78, and 56 is: "
  2. + largest(23,78, 56));
Four parameters
  1. System.out.println("The largest number of 93, 28, 83 and 66 is: "
  2. + largest(93, 28, 83, 66));
And
  1. System.out.println("The largest of 22.5 , 12.34, 56.34, 78, "
  2. + "\n"
  3. + "98.45, 25, 78, 23 and 36 is: "
  4. + largest(22.5, 12.34, 56.34, 78, 98.45, 25, 78, 23, 36));
nine parameters. The statement
  1. System.out.println("The largest number in numList is: "
  2. + largest(numberList));
the method largest is called using an array of numbers. The statement
  1. System.out.println("A call to the method largest with empty \n"
  2. + " parameter list returns the value " + largest());
is called no parameters.

Add new comment