1 / 23

CSCI S-1 Section 9

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.

bond
Download Presentation

CSCI S-1 Section 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI S-1Section 9

  2. 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

  3. Java Documentation java.sun.com/j2se/1.5.0/docs/api/

  4. Arrays • fixed-size container • direct access to each element • 0-based index • all objects of the same type

  5. Creating Arrays • double [ ] temps = new double [ 5 ]; • int [ ] numbers = new int [10]; • boolean [ ] flags = new boolean [20]; • String [100] names = new String [100];

  6. Filling Arrays //temps [0] is the first element in the array of length 5

  7. 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;

  8. 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;

  9. Filling Arrays /* print out temperature and index when temperature is greater than 104 */

  10. 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);

  11. Array Methods System.out.println(tems); //don’t import java.util.Arrays; System.out.println(Arrays.toString(temps));

  12. 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

  13. 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)); }

  14. Compare Arrays private static booleanisEqualTo(int [] a1, int [] a2) { } }

  15. 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; } }

  16. Command-Line Arguments //print out command-line arguments public static void main(String[] args) { }

  17. 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]); }

  18. Command-Line Arguments // convert first command-line arg to int using Integer.parseInt() public static void main(String [] args) { }

  19. 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]) ; }

  20. 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); } } }

  21. 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,

  22. Recursion //Does this work? private static int power(int a, int b) { return (a * power(a, b-1)); }

  23. Recursion //Add the “base case" private static int power(int a, int b) { if (b==0) return 1; return (a * power(a, b-1)); }

More Related