

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


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]
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 –