• We just launched and are currently in beta. Join us as we build and grow the community.

Arrays and Variable Length Parameters List

Lanco

Data Discrepancy Investigator
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
121
Likes
141
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. public

    class

    LargestNumber
  3. {
  4. public

    static

    void

    main(

    String

    [

    ]

    args)
  5. {
  6. double

    [

    ]

    numberList =

    {

    23

    , 45.5

    , 89

    ,34

    , 92.78

    , 36

    , 90

    , 120.89

    ,
  7. 97

    , 23

    , 90

    , 89

    }

    ;

  8. System

    .out

    .println

    (

    "The larger 0f 5.6 and 10.8 is: "
  9. +

    largest(

    5.6

    , 10.8

    )

    )

    ;

  10. System

    .out

    .println

    (

    "The largest of 23, 78, and 56 is: "
  11. +

    largest(

    23

    ,78

    , 56

    )

    )

    ;

  12. System

    .out

    .println

    (

    "The largest number of 93, 28, 83 and 66 is: "
  13. +

    largest(

    93

    , 28

    , 83

    , 66

    )

    )

    ;

  14. System

    .out

    .println

    (

    "The largest of 22.5 , 12.34, 56.34, 78, "
  15. +

    "\n

    "
  16. +

    "98.45, 25, 78, 23 and 36 is: "
  17. +

    largest(

    22.5

    , 12.34

    , 56.34

    , 78

    , 98.45

    , 25

    , 78

    , 23

    , 36

    )

    )

    ;

  18. System

    .out

    .println

    (

    "The largest number in numList is: "
  19. +

    largest(

    numberList)

    )

    ;

  20. System

    .out

    .println

    (

    "A call to the method largest with empty \n

    "
  21. +

    " parameter list returns the value "

    +

    largest(

    )

    )

    ;


  22. }
  23. public

    static

    double

    largest(

    double

    ... numList

    )
  24. {
  25. double

    max;
  26. int

    index;

  27. if

    (

    numList.length

    !=

    0

    )
  28. {
  29. max =

    numList[

    0

    ]

    ;

  30. for

    (

    index =

    1

    ;

    index <

    numList.length

    ;

    index++

    )
  31. {
  32. if

    (

    max <

    numList [

    index]

    )
  33. max =

    numList [

    index]

    ;

  34. }
  35. return

    max;
  36. }
  37. return

    0.0

    ;

  38. }
  39. }

Sample Run:

The larger 0f 5.6 and 10.8 is: 10.8
The largest of 23, 78, and 56 is: 78.0
The largest number of 93, 28, 83 and 66 is: 93.0
The largest of 22.5 , 12.34, 56.34, 78,
98.45, 25, 78, 23 and 36 is: 98.45
The largest number in numList is: 120.89
A call to the method largest with empty
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.

 

452,292

323,341

323,350

Top