© 2004 pearson addison-wesley. all rights reserved october 10, 2007 parameter lists & command...
TRANSCRIPT
![Page 1: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/1.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
October 10, 2007
Parameter Lists & Command Line Arguments
ComS 207: Programming I (in Java)Iowa State University, FALL 2007Instructor: Alexander Stoytchev
![Page 2: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/2.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example: Numerical Integration
[http://www.cae.tntech.edu/~mwr/Lecture%202004-08-31/numerical-integration.png]
![Page 3: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/3.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Quick review of last lecture
![Page 4: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/4.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Arrays
• Another way to depict the scores array:
scores 79
87
94
82
67
98
87
81
74
91
![Page 5: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/5.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Arrays of Objects
• The words array when initially declared:
words -
-
-
-
-
• At this point, the following reference would throw a NullPointerException:
System.out.println (words[0]);
![Page 6: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/6.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Arrays of Objects
• After some String objects are created and stored in the array:
“friendship”
words
-
-
“loyalty”
“honor”
![Page 7: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/7.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Initial Values for Arrays of Objects
• Keep in mind that String objects can be created using literals
• The following declaration creates an array object called verbs and fills it with four String objects created using string literals
String[] verbs = {"play", "work", "eat", "sleep"};
![Page 8: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/8.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Another Way to do the Same Thing
• Initialize each element separately
String[] verbs;verbs=newString[4];
verbs[0] = new String("play“);verbs[1] = new String("work“);verbs[2] = new String("eat“);verbs[3] = new String("sleep“);
![Page 9: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/9.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Other Stuff
![Page 10: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/10.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
CD Collection Example
• Now let's look at an example that manages a collection of CD objects
• See Tunes.java (page 387)
• See CDCollection.java (page 388)
• See CD.java (page 391)
![Page 11: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/11.jpg)
Chapter 7
Sections 7.4 & 7.5
![Page 12: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/12.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Command-Line Arguments
• The signature of the main method indicates that it takes an array of String objects as a parameter
• These values come from command-line arguments that are provided when the interpreter is invoked
• For example, the following invocation of the interpreter passes three String objects into main:
> java StateEval pennsylvania texas arizona
• These strings are stored at indexes 0-2 of the array parameter of the main method
• See NameTag.java (page 393)
![Page 13: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/13.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
![Page 14: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/14.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example: NameTag.java (page 393)
![Page 15: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/15.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
• Suppose we wanted to create a method that processed a different amount of data from one invocation to the next
• For example, let's define a method called average that returns the average of a set of integer parameters
// one call to average three valuesmean1 = average (42, 69, 37);
// another call to average seven valuesmean2 = average (35, 43, 93, 23, 40, 21, 75);
![Page 16: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/16.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
• We could define overloaded versions of the average method
Downside: we'd need a separate version of the method for each parameter count
• We could define the method to accept an array of integers
Downside: we'd have to create the array and store the integers prior to calling the method each time
• Instead, Java provides a convenient way to create variable length parameter lists
![Page 17: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/17.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
• Using special syntax in the formal parameter list, we can define a method to accept any number of parameters of the same type
• For each call, the parameters are automatically put into an array for easy processing in the method
public double average (int ... list){ // whatever} element
typearrayname
Indicates a variable length parameter list
![Page 18: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/18.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
public double average (int ... list){ double result = 0.0;
if (list.length != 0) { int sum = 0; for (int num : list) sum += num; result = (double)sum / list.length; }
return result;}
![Page 19: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/19.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
• The type of the parameter can be any primitive or object type
public void printGrades (Grade ... grades){ for (Grade letterGrade : grades) System.out.println (letterGrade);}
![Page 20: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/20.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
• A method that accepts a variable number of parameters can also accept other parameters
• The following method accepts an int, a String object, and a variable number of double values into an array called nums
public void test (int count, String name, double ... nums){ // whatever}
![Page 21: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/21.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Variable Length Parameter Lists
• The varying number of parameters must come last in the formal arguments
• A single method cannot accept two sets of varying parameters
• Constructors can also be set up to accept a variable number of parameters
![Page 22: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/22.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example: VariableParameters.java (page 396)
![Page 23: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/23.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
Example: Family.java (page 397)
![Page 24: © 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State](https://reader030.vdocuments.mx/reader030/viewer/2022032702/56649f4c5503460f94c6cfde/html5/thumbnails/24.jpg)
© 2004 Pearson Addison-Wesley. All rights reserved
THE END