© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.

Slides:



Advertisements
Similar presentations
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Wecksten, Mattias Arrays Based on templates from Lewis & Loftus.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Chapter 6: Arrays Java Software Solutions Third Edition
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Arrays Chapter 7 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Objects for Organizing Data -- b As our programs get more sophisticated, we need assistance organizing large amounts of data. Chapter 6 focuses on: array.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
© 2004 Pearson Addison-Wesley. All rights reserved October 17, 2007 Searching (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Outline Declaring and Using Arrays Arrays of Objects Variable Length Parameter.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/27 Outline Declaring and Using Arrays Arrays of Objects Variable Length.
Programming in Java (COP 2250) Lecture Chengyong Yang Fall, 2005.
1 Arrays An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 7: Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 7 Arrays: Part 1 of 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of.
Dr. Sahar Shabanah Lecture 3: Arrays. Data Structures Data structures are organized collections of information and a set of operations used to manage.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
More on Arrays Review of Arrays of ints, doubles, chars
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
COS 312 DAY 14 Tony Gauvin.
Parameter Lists & Command Line Arguments
Arrays, 2-D Arrays, and ArrayList
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Chapter 7: Arrays.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

© 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 2007 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Example: Numerical Integration [

© 2004 Pearson Addison-Wesley. All rights reserved Quick review of last lecture

© 2004 Pearson Addison-Wesley. All rights reserved Arrays Another way to depict the scores array: scores

© 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]);

© 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”

© 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"};

© 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“);

© 2004 Pearson Addison-Wesley. All rights reserved Other Stuff

© 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) Tunes.java See CDCollection.java (page 388) CDCollection.java See CD.java (page 391) CD.java

Chapter 7 Sections 7.4 & 7.5

© 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) NameTag.java

© 2004 Pearson Addison-Wesley. All rights reserved

Example: NameTag.java (page 393) NameTag.java

© 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 values mean1 = average (42, 69, 37); // another call to average seven values mean2 = average (35, 43, 93, 23, 40, 21, 75);

© 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

© 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 type array name Indicates a variable length parameter list

© 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; }

© 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); }

© 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 }

© 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

© 2004 Pearson Addison-Wesley. All rights reserved Example: VariableParameters.java (page 396) VariableParameters.java

© 2004 Pearson Addison-Wesley. All rights reserved Example: Family.java (page 397) Family.java

© 2004 Pearson Addison-Wesley. All rights reserved THE END