230 likes | 357 Views
CSCI S-1 Section 9. Coming Soon. Problem Set Four (72 + 5/15 points) Tuesday, July 21, 17:00 EST Problem Set Five (30 + 5 points) Friday, July 24, 17:00 EST Term Project Proposal ALSO Friday, July 24, 17:00 EST. Java Documentation. java.sun.com/j2se/1.5.0/docs/ api / . Arrays.
E N D
Coming Soon • Problem Set Four (72 + 5/15 points) • Tuesday, July 21, 17:00 EST • Problem Set Five (30 + 5 points) • Friday, July 24, 17:00 EST • Term Project Proposal • ALSO Friday, July 24, 17:00 EST
Java Documentation java.sun.com/j2se/1.5.0/docs/api/
Arrays • fixed-size container • direct access to each element • 0-based index • all objects of the same type
Creating Arrays • double [ ] temps = new double [ 5 ]; • int [ ] numbers = new int [10]; • boolean [ ] flags = new boolean [20]; • String [100] names = new String [100];
Filling Arrays //temps [0] is the first element in the array of length 5
Filling Arrays //temps [0] is the first element in the array of length 5 for ( inti = 0; I < 5; i++ ) temps[i] = 98 + Math.random() * 6;
Filling Arrays //temps [0] is the first element in the array of length 5 for ( inti = 0; I < temps.length; i++ ) temps[i] = 98 + Math.random() * 6;
Filling Arrays /* print out temperature and index when temperature is greater than 104 */
Filling Arrays /* print out temperature and index when temperature is greater than 104 */ for ( inti = 0; I < temps.length; i++ ) if ( temps[i] > 104 ) System.out.println(“temps: “ + temps[i] + “ at index: “ + i);
Array Methods System.out.println(tems); //don’t import java.util.Arrays; System.out.println(Arrays.toString(temps));
Quick Snips • First item in array A • Last item in array A • Make A 100 strings long • Length of first string in A • A has strings in even positions and doubles in odd positions • Append one additional element to end of array • A [0] • A [ A.length - 1 ] • String [] A = new String [100]; • A [0].length() • no can do • no can do
Compare Arrays booleanisEqualTo( int [] arr1, int [] arr2) public static void main(String[] args) { int [] arr1 = {13, 14, 15}; int [] arr2 = {13, 14, 16, 17}; System.out.println(isEqualTo(arr1, arr2)); }
Compare Arrays private static booleanisEqualTo(int [] a1, int [] a2) { } }
Compare Arrays private static booleanisEqualTo(int [] a1, int [] a2) { if (a1.length != a2.length) return false; else { for (inti=0; i<a1.length; i++) if (a1[i]!=a2[i]) return false; return true; } }
Command-Line Arguments //print out command-line arguments public static void main(String[] args) { }
Command-Line Arguments //print out command-line arguments public static void main(String[] args) { for (inti = 0; i < args.length; i++) System.out.println(args[i]); }
Command-Line Arguments // convert first command-line arg to int using Integer.parseInt() public static void main(String [] args) { }
Command-Line Arguments // convert first command-line arg to int using Integer.parseInt() public static void main(String [] args) { if (args.length != 0) intfirstArg = Integer.parseInt(args[0]) ; }
Exceptions: Try & Catch public static void main(String [] args) { if (args.length != 0) { try { inti= Integer.parseInt(args[0]) ; } catch (NumberFormatException e) { System.exit(1); } } }
Recursion What is 2 to the power of 5? 2 * 2 * 2 * 2 * 2 What is 2 to the power of 4? 2 * 2 * 2 * 2 In other words,
Recursion //Does this work? private static int power(int a, int b) { return (a * power(a, b-1)); }
Recursion //Add the “base case" private static int power(int a, int b) { if (b==0) return 1; return (a * power(a, b-1)); }