Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used.

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Programming with Recursion. Example of Recursion (Ask Until the User Gets It Right) import java.util.* ; public class CountDown { private int count; public.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
CS180 RECURSION March 28,2008. Announcements Project 7 : Recursive Expression Evaluators Milestone Due : 4/2/2008 Project Due : 4/9/2008 Exam 2 to be.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Building Java Programs Chapter 13 Searching reading: 13.3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Working with arrays (we will use an array of double as example)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
EKT472: Object Oriented Programming
CIS Principles of Programming
Templates.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
Basics of Recursion Programming with Recursion
Recursion Chapter 11.
Suggested self-checks: Section 7.11 #1-11
Chapter 6 Arrays.
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.

CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 (already past) interview: Saturday January 25.class files in public directory for HW 2 & 3 Do not need to turnin the HW if you take an interview before the turnin deadline

Reading Order Chapters 1-6, then Chapter 11 (Recursion), then Chapter 10, only section 10.1 (vectors), then Chapter 7 (Inheritance)

Midterm Warning In lecture Tuesday February 4. Quiz grades are not so great. Pick up graded quizzes from TA. Do Self-Test Exercises. Sample midterm on web page and in public.

You may need to make an inner class static public class Match { public static class Person { …. }//end Person …. }//end Match

Length Instance Variable String[] a = new String[10]; a.length has the value 10 a.length is read-only; You cannot change it.

Entire Array as an Argument Array variables are reference variables, just like class objects. Array parameters are just like class parameters (Call-by-value for a reference.) Arrays know their size. One array type for all arrays with the same base type, e.g. double[] is the type for any array of doubles of any size.

public static double[] averageArray (int firstScore, int[] nextScore) { double[] temp = new double[nextScore.length]; int i; for (i = 0; i < temp.length; i++) temp[i] = average(firstScore, nextScore[i]); return temp; }

What is wrong? Species[] a = new Species[3]; a[0].readInput(); Error message “Null Pointer Exception” a[0] is a varaible of type Species, but is just a variable. It has no refernce to any object. Need a[0] = new Species();

Species[] a = new Species[3]; for (int i = 0; i < a.length; i++) a[i] = new Species(); a[0].readInput(); //OK a[1].readInput(); //OK a[2].readInput(); //OK

Array as Instance Variables public class OneWayNoRepeatsList { public static int START_POSITION = 1; public static int DEFAULT_SIZE = 50; private int countOfEntries; //can be less than entry.length. private String[] entry;

public class OneWayNoRepeatsList { public static int START_POSITION = 1; public static int DEFAULT_SIZE = 50; private int countOfEntries; private String[] entry; public OneWayNoRepeatsList (int maximumNumberOfEntries) { entry = new String[maximumNumberOfEntries]; countOfEntries = 0; }

Selection Sort See text, other type of slides used here

Recursion (Chapter 11) Recursive method == one that invokes itself Anything will compile. Need some care to get method to run OK. Read all of Chapter 11

writeVertical Example Static method ClassName.writeVertical(1234); Outputs

public static void writeVertical(int n) { if (n < 10) System.out.println(n); else //n is two or more digits long: { writeVertical(n/10); System.out.println(n%10); }

ClassName.writeVertcal(123); Equivalent to writeVertical(123/10); System.out.println(123%10); Equivalent to writeVertical(12); System.out.println(3); Equivalent to writeVertical(12/10); System.out.println(12%10); System.out.println(3);

writeVertical(12/10); System.out.println(12%10); System.out.println(3); Equivalent to writeVertical(1); System.out.println(2); System.out.println(3); Equivalent to System.out.println(1); System.out.println(2); System.out.println(3);

Stack Like a stack of paper Last-in/First-out Used to implement recursion

Successful Recursive Method Two cases Case with recursive calls Stopping case (Base case) with no recursive calls

Binary Search Search an array to see if a given value is in the array. Array must be sorted. Very fast.

public class ArraySearcher { private int[] a; public ArraySearcher(int[] theArray) { a = theArray } public int find(int target) {return search(target, 0, a.length - 1);} //Searches a[first] through a[last] //Returns the index of target if found. //Returns -1 if target is not found. private int search(int target, int first, int last)

privateint search(int target,int first,int last) { int result = -1;//to keep compiler happy. int mid; if (first > last) result = -1; else { Recursive Case, Next slide } return result; }

mid = (first + last)/2; if (target == a[mid]) result = mid; else if (target < a[mid]) result = search(target, first, mid - 1); else //(target > a[mid]) result = search(target, mid + 1, last);