Presentation is loading. Please wait.

Presentation is loading. Please wait.

Command Line Arguments

Similar presentations


Presentation on theme: "Command Line Arguments"— Presentation transcript:

1 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 1920s. 11/30/2018 © 2006 William Albritton

2 Array Review See code at ArrayExercise.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 11/30/2018 © 2006 William Albritton

3 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 NullPointerException String [] stooges = new String[3]; Integer length = stooges[0].length(); //bug Correct way: stooges[0] = new String("Larry"); Integer length = stooges[0].length(); 11/30/2018 © 2006 William Albritton

4 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 arrayLength = stooges.length; Strings use the method length() String [] stooges = new String[3]; stooges[0] = new String("Larry"); Integer strLength=stooges[0].length(); See Stooges.java 11/30/2018 © 2006 William Albritton

5 Array Parameters Syntax for a method call that passes an array as the actual parameter method(arrayName); Just use the name of the array (no brackets) Syntax for a method definition that passes an array as the formal parameter public static ReturnType method(DataType [] parameter){ ... } Add a bracket after data type of formal parameter Note that, in this example, arrayName & parameter reference the same array 11/30/2018 © 2007 William Albritton

6 Array Parameters Example
Java code Method call Double average = average(examScores); 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 11/30/2018 © 2007 William Albritton

7 Returning an Array Syntax for a method call that returns an array
DataType [] arrayName = method(); Assigns the return value of the method to the array Syntax for a method definition that returns an array public static ReturnType [] method(){ ... return array; } Add a bracket after data type of return type Note that, in this example, arrayName will be assigned the address of array 11/30/2018 © 2007 William Albritton

8 Array Return Example This code assigns the address of array letters to the array grades Method call String [] grades = convert(finalGrades); Method definition public static String[] convert(double[] array){ String[] letters = new String[array.length]; //more code return letters; } 11/30/2018 © 2007 William Albritton

9 Example Code See ArraysAndMethods.java This program shows how to:
Pass an array to a method Return an array from a method 11/30/2018 © 2007 William Albritton

10 Class Exercise 1 See Exercise1.java What’s the output of the program?
11/30/2018 © 2007 William Albritton

11 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 11/30/2018 © 2006 William Albritton

12 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 java Repeat one two three args[0] is the String "one" args[1] is the String "two" args[2] is the String "three" 11/30/2018 © 2006 William Albritton

13 Command line & IDEs See file Repeat.java UHUNIX commands
javac Repeat.java java Repeat one two three jGRASP commands Click Build, Run Arguments Run Arguments: one two three Eclipse commands Click Run, Run..., Arguments, Apply, Run Program Arguments: one two three 11/30/2018 © 2006 William Albritton

14 Command line & jGRASP You can also enter command line arguments using jGRASP 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 11/30/2018 © 2006 William Albritton

15 Meaning of main Meaning of each word of the main method
public static void main(String[] args) public: other classes can use this method static: this is a class method, so method call has format ClassName.methodName() void: this method does not return anything main: name of this special kind of method which is used to start the program String[]: an array of Strings args: the parameter of this method, which contains each word entered on the command line, stored as an array of Strings 11/30/2018 © 2006 William Albritton

16 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) 11/30/2018 © 2006 William Albritton


Download ppt "Command Line Arguments"

Similar presentations


Ads by Google