Command Line Arguments ICS 111 Introduction to Computer

  • Slides: 16
Download presentation
Command Line Arguments • ICS 111: Introduction to Computer Science I – William Albritton

Command Line Arguments • ICS 111: Introduction to Computer Science I – William Albritton • Information and Computer Sciences Department at the University of Hawai‘i at Mānoa • "The wireless music box has no imaginable commercial value. Who would pay for a message sent to nobody in particular? " – David Sarnoff's associates in response to his urgings for investment in the radio in the 1920 s. 1/12/2022 © 2006 William Albritton 1

Array Review • See code at Array. Exercise. java – More questions on this?

Array Review • See code at Array. Exercise. java – More questions on this? • Write loops to do the following: – Make an array of these elements: 1, 2, 3, 4, 5 – Multiply each element by 10 – Assign each element to 100 1/12/2022 © 2006 William Albritton 2

Common Bug • For reference variables, have to initialize them to a specific object,

Common Bug • For reference variables, have to initialize them to a specific object, or else… – Since reference variables are initialized to null, can easily throw a Null. Pointer. Exception • String [] stooges = new String[3]; Integer length = stooges[0]. length(); //bug – Correct way: • String [] stooges = new String[3]; stooges[0] = new String("Larry"); Integer length = stooges[0]. length(); 1/12/2022 © 2006 William Albritton 3

Common Bug II • Determining the length of a String and an array are

Common Bug II • Determining the length of a String and an array are different – Arrays use the public data field length • String [] stooges = new String[3]; Integer array. Length = stooges. length; – Strings use the method length() • String [] stooges = new String[3]; stooges[0] = new String("Larry"); Integer str. Length=stooges[0]. length(); – See Stooges. java 1/12/2022 © 2006 William Albritton 4

Array Parameters • Syntax for a method call that passes an array as the

Array Parameters • Syntax for a method call that passes an array as the actual parameter – method(array. Name); • Just use the name of the array (no brackets) • Syntax for a method definition that passes an array as the formal parameter – public static Return. Type method(Data. Type [] parameter){. . . } • Add a bracket after data type of formal parameter • Note that, in this example, array. Name & parameter reference the same array 1/12/2022 © 2007 William Albritton 5

Array Parameters Example • Java code – Method call • Double average = average(exam.

Array Parameters Example • Java code – Method call • Double average = average(exam. Scores); – Method declaration • public static Double average(Integer [] array){ //code using array } • Since an address to an object is passed, any changes to the object (array) inside the method will be permanent 1/12/2022 © 2007 William Albritton 6

Returning an Array • Syntax for a method call that returns an array –

Returning an Array • Syntax for a method call that returns an array – Data. Type [] array. Name = method(); • Assigns the return value of the method to the array • Syntax for a method definition that returns an array – public static Return. Type [] method(){. . . return array; } • Add a bracket after data type of return type • Note that, in this example, array. Name will be assigned the address of array 1/12/2022 © 2007 William Albritton 7

Array Return Example • This code assigns the address of array letters to the

Array Return Example • This code assigns the address of array letters to the array grades – Method call • String [] grades = convert(final. Grades); – Method definition • public static String[] convert(double[] array){ String[] letters = new String[array. length]; //more code return letters; } 1/12/2022 © 2007 William Albritton 8

Example Code • See Arrays. And. Methods. java – This program shows how to:

Example Code • See Arrays. And. Methods. java – This program shows how to: • Pass an array to a method • Return an array from a method 1/12/2022 © 2007 William Albritton 9

Class Exercise 1 • See Exercise 1. java – What’s the output of the

Class Exercise 1 • See Exercise 1. java – What’s the output of the program? 1/12/2022 © 2007 William Albritton 10

Java Program Implementation • Implement on UH UNIX using the command line interface –

Java Program Implementation • Implement on UH UNIX using the command line interface – The command line is the line at the prompt on the UNIX shell • Commands – Text editor: emacs Program. java – Compiler: javac Program. java – Interpreter: java Program • A demo will be given in class 1/12/2022 © 2006 William Albritton 11

Command line Arguments • Arguments (strings) entered on the command line can be accessed

Command line Arguments • Arguments (strings) entered on the command line can be accessed in your Java program – args is an array of Strings • public static void main(String [] args) – Corresponds to the words listed after the name of the Java program – Below is I/O for program Repeat. java 1/12/2022 • java Repeat one two • args[0] is the String • args[1] is the String • args[2] is the String three "one" "two" "three" © 2006 William Albritton 12

Command line & IDEs • See file Repeat. java – UHUNIX commands • javac

Command line & IDEs • See file Repeat. java – UHUNIX commands • javac Repeat. java • java Repeat one two three – j. GRASP commands • • Click Build, Run Arguments: one two three – Eclipse commands • • 1/12/2022 Click Run, Run. . . , Arguments, Apply, Run Program Arguments: one two three © 2006 William Albritton 13

Command line & j. GRASP • You can also enter command line arguments using

Command line & j. GRASP • You can also enter command line arguments using j. GRASP • Directions – From the top menu, select “Build” – On the drop-down menu, click “Run Arguments” – At the top of the program, you should now have a text box titled “Run Arguments: ” – Type your command-line arguments into this text box 1/12/2022 © 2006 William Albritton 14

Meaning of main • Meaning of each word of the main method • public

Meaning of main • Meaning of each word of the main method • public static void main(String[] args) 1. public: other classes can use this method 2. static: this is a class method, so method call has format Class. Name. method. Name() 3. void: this method does not return anything 4. main: name of this special kind of method which is used to start the program 5. String[]: an array of Strings 6. args: the parameter of this method, which contains each word entered on the command line, stored as an array of Strings 1/12/2022 © 2006 William Albritton 15

Class Exercise 2 • Write a Java program that creates a new word by

Class Exercise 2 • Write a Java program that creates a new word by combining the first letter of each word in the command line arguments – The method’s parameters are an Array of Strings (the command line arguments) & the method returns a String (the new word) 1/12/2022 © 2006 William Albritton 16