BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Fibonacci Numbers A simple example of program design.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Hello AP Computer Science!. What are some of the things that you have used computers for?
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Computer Science 210 Computer Organization Arrays.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Building java programs chapter 6
Working with arrays (we will use an array of double as example)
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Building java programs, chapter 5 Program logic and indefinite loops.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Simple algorithms on an array - compute sum and min.
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to the.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Dr. Kyung Eun Park Summer 2017
Midterm Review Problems
User-Defined Functions
Building Java Programs
Building Java Programs
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Building Java Programs
Building Java Programs
Building Java Programs
Why did the programmer quit his job?
Single-Dimensional Arrays chapter6
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Presentation transcript:

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.

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

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

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

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 for- each loops in your code!

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

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 and b is Their digits would be combined as follows to produce c: a b c (return value)

Homework for Chapter 7 HomeworkAssigned onDue on Practice It: SC 7.8, /2/201412/10/2014 Practice It: Ex /2/201412/10/2014