Arrays and Methods, Command Line Parameters | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-9cq-1
koofers-user-9cq-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 36
Chapter 8 - Arrays
Reading: 8.1 to 8.2.3 (pages 381 – 389)
8.4 to 8.6 (pages 396 – 417)
Arrays and Methods
Assigning values to array elements in methods
Last time we looked what happens when you modify the values of array elements inside
of a method. We saw that because an array is a reference type, changes to the values of
the array elements affect the original array.
Let's look at ArrayCount.java
Let's look at ArrayCopy.java again.
[Draw pix]
Command Line Parameters
We've seen this since the first week of class: public static void main( String[] args )
String[] args is an array of Strings that is passed to the main method when you start a
program. The Strings are set to the command line parameters. For example:
java MyProgram one two three
java MyProgram hello world
java MyProgram junk "stuff things" objects
Look at MyProgram.java. Make sure to show a quoted string as a command line
parameter.
Look at Roots2. java
Returning an array from a method
We can pass arrays to methods as parameters. A method's return type can also be an array
type.
[public] [static] <returnType> <name> ( <parameterList> )
We've seen returnTypes such as int, float, boolean, double, void, Point, String,...
How about int[], double[], Point[], ...
pf2

Partial preview of the text

Download Arrays and Methods, Command Line Parameters | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 36

Chapter 8 - Arrays Reading: 8.1 to 8.2.3 (pages 381 – 389) 8.4 to 8.6 (pages 396 – 417) Arrays and Methods Assigning values to array elements in methods Last time we looked what happens when you modify the values of array elements inside of a method. We saw that because an array is a reference type, changes to the values of the array elements affect the original array. Let's look at ArrayCount.java Let's look at ArrayCopy.java again. [Draw pix] Command Line Parameters We've seen this since the first week of class: public static void main( String[] args ) String[] args is an array of Strings that is passed to the main method when you start a program. The Strings are set to the command line parameters. For example: java MyProgram one two three java MyProgram hello world java MyProgram junk "stuff things" objects Look at MyProgram.java. Make sure to show a quoted string as a command line parameter. Look at Roots2. java Returning an array from a method We can pass arrays to methods as parameters. A method's return type can also be an array type. [public] [static] ( ) We've seen returnTypes such as int, float, boolean, double, void, Point, String,... How about int[], double[], Point[], ...

public int[] add( int[] a, int[] b ) {...} public double[] reverse( double[] a ) {...} public int[] round( double[] a ) {...} public double[] roots( int first, int last ) {...} Look at ArrayAdd.java and Roots.java. Get students to try to write reverse method. Lab 21 a. Get an int value as the first command line parameter. b. Create an array of int whose size is the value of the first command line parameter. Use Integer.parseInt( String ). c. Initialize the array elements so that they have the values 0, 1, 2, 3, … d. Pass the array to the method getAlternateElements. int[] example = { 3, 7, 6, 4, 2, 0, 9 }; int[] result = getAlternateElements( example ); e. Print the array returned by getAlternateElements GetAlternateElements –

  • has one formal parameter, which is an array of int.
  • It returns an array of int.
  • The returned array should only contain every other element of the original array. In the above example, getAlternateElements should return the array {3, 6, 2, 9} Still to discuss
  • sorting
  • two dimensional arrays