BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms days until the AP Computer Science test • Opportunity for teenagers to contribute towards open source projects. • Achievable tasks of different difficulties. • http://www.google-melange.com At the end of this class, you will be able to • You will be able to describe the difference between sequential and random access. • You will be able to change the contents of an array by passing it as a parameter to a method. • You will be able to use a for-each loop to examine each value in an array. Sequential versus random access • Sequential Access - manipulating values in a sequential manner from first to last. • Random Access - manipulating values in any order to allow quick access to each value. • Arrays are allocated as contiguous blocks of memory. • As a result, the computer can quickly determine where particular values are stored. • Can access elements out of order or that are stored far apart. double[] temperatures = new double[5000]; < array is initialized > System.out.println(“#2345: ” + temperatures[2345]); System.out.println(“#22: ” + temperatures[22]); Arrays and methods When you pass an array as a parameter to a method, the method can change the contents of the array without needing to return the modified array. Create a new class called ArrayPractice. 2) In your main method: 1) a) b) c) d) Create an int array of length 5 and initialize the elements to some non-default values. Print out each element of the array. Traverse the array and multiply each element by 2. Print out each element of the array. Now, move the logic to multiply the array elements by 2 to a separate method that takes an array of integers as a parameter. Do not return anything from this method. 4) Call this method from your main method and ensure that the output remains the same. 3) Traversing an array • Array traversal - Processing each array element sequentially from the first to the last. for (int i = 0; i < <array>.length; i++)) { <do something with array[i]>; } • Example: for (int i = 0; i < temperatures.length; i++)) { System.out.println(temperatures[i]); } For-each loops • Also known as the enhanced for loop. • You can use it whenever you want to examine each value in an array. for (<type> <name> : <array>) { <statements> } int daysBelowFreezing = 0; for (double x : temperatures) { if (x < 32.0) { daysBelowFreezing++; } } Syntax Yoda For-each loops • Useful for examining each value in an array. • Not useful for when you want to modify elements in an array or keep track of the current index. for (double x : temperatures) { x *= 2; } Only doubles the variable x – does not change the elements in temperatures! int i = 0; for (double x : temperatures) { System.out.println(“Element i : ” + x); i++; } Now we have to use extra code to keep track of i! For-each loops Modify your ArrayPractice class to use a for-each loop when it prints out each element of your array. Even though you are printing out the array twice, you should not have two foreach loops in your code! if/else review public static void mystery(int n) { System.out.print(n + “ ”); if (n > 10) { What output is produced by: n = n / 2; mystery(40); } else { mystery(8); n = n + 7; mystery(0); } mystery(12); if (n * 2 < 25) { mysterm(20); n = n + 10; } System.out.println(n); } What is wrong with this code? public class AgeCheck { public static void main(String[] args) { int myAge = 19; // I am 19; let me see if I can drive message(myAge); } // Displays message about driving to user based on given age public static void message(int age) { if (myAge >= 16) { System.out.println("I'm old enough to drive!"); } if (myAge <= 16) { System.out.println("Not old enough yet... :*("); } } } What is wrong with this code? public class AgeCheck { public static void main(String[] args) { int myAge = 19; // I am 19; let me see if I can drive message(myAge); } // Displays message about driving to user based on given age public static void message(int age) { if (age >= 16) { System.out.println("I'm old enough to drive!"); } else { System.out.println("Not old enough yet... :*("); } } } Larger Digits Write a static method named largerDigits that accepts two integer parameters a and b and returns a new integer c where each digit of c gets its value from the larger of a's and b's digit in the same place. That is, the ones digit of c is the larger of the ones digit of a and the ones digit of b, and the tens digit of c is the larger of the tens digit of a and the tens digit of b, and so on. You may assume that a and b are positive integers (greater than 0). For example, suppose a is 603452384 and b is 921782. Their digits would be combined as follows to produce c: a 603452380 b 920784 -----------------c 952784 (return value) Homework for Chapter 7 Homework Assigned on Due on Practice It: SC 7.8, 7.10 12/2/2014 12/10/2014 Practice It: Ex 7.4 12/2/2014 12/10/2014