Download presentation
Presentation is loading. Please wait.
Published byDale Richard Modified over 9 years ago
2
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms
3
days until the AP Computer Science test
4
Opportunity for teenagers to contribute towards open source projects. Achievable tasks of different difficulties. http://www.google-melange.com
5
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.
6
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]; System.out.println(“#2345: ” + temperatures[2345]); System.out.println(“#22: ” + temperatures[22]);
7
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. 1) Create a new class called ArrayPractice. 2) In your main method: a) Create an int array of length 5 and initialize the elements to some non-default values. b) Print out each element of the array. c) Traverse the array and multiply each element by 2. d) Print out each element of the array. 3) 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.
8
Traversing an array Array traversal - Processing each array element sequentially from the first to the last. for (int i = 0; i.length; i++)) { ; } Example: for (int i = 0; i < temperatures.length; i++)) { System.out.println(temperatures[i]); }
9
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 ( : ) { } int daysBelowFreezing = 0; for (double x : temperatures) { if (x < 32.0) { daysBelowFreezing++; } Syntax Yoda
10
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!
11
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 for- each loops in your code!
12
if/else review public static void mystery(int n) { System.out.print(n + “ ”); if (n > 10) { n = n / 2; } else { n = n + 7; } if (n * 2 < 25) { n = n + 10; } System.out.println(n); } What output is produced by: mystery(40); mystery(8); mystery(0); mystery(12); mysterm(20);
13
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... :*("); }
14
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... :*("); }
15
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)
16
Homework for Chapter 7 HomeworkAssigned onDue on Practice It: SC 7.8, 7.10 12/2/201412/10/2014 Practice It: Ex 7.4 12/2/201412/10/2014
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.