Swap I class Swap { public static void swap(int i, int j) {

Slides:



Advertisements
Similar presentations
Chapter 17 Recursion.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
The Singleton Pattern II Recursive Linked Structures.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
CS1101X: Programming Methodology Recitation 9 Recursion II.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
EXAM 1 REVIEW. days until the AP Computer Science test.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
CHAPTER 4 RECURSION. BASICALLY, A METHOD IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Chapter 5 Recursion. Basically, a method is recursive if it includes a call to itself.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
COS 312 DAY 19 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 Layers using non-opaque panels.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
TOWERS OF HANOI. : If n = 1, move disk 1 from pole 'A' to pole 'B'. else: 1.First, move n-1 disks from pole 'A' to pole 'C', using pole 'B' as.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
COS 312 DAY 24 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 6 Corrected Assignment 7 (bonus) – Due May 11 – Will be scored on 15 point scale, points.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
CHAPTER 4: Linked Structures
Recursion -- Introduction
CprE 185: Intro to Problem Solving (using C)
CSE 143 Lecture 19 More Recursive Backtracking
Java Software Structures: John Lewis & Joseph Chase
Chapter 4 Linked Structures - Stacks
Java Software Solutions Foundations of Program Design 9th Edition
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems We will focus on: thinking in.
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Chapter 4 Linked Structures - Stacks
מבוא למדעי המחשב, בן גוריון
Chapter 8 Recursion.
CS2011 Introduction to Programming I Methods (II)
class PrintOnetoTen { public static void main(String args[]) {
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
CSE 143 Lecture 18 More Recursive Backtracking
CSE 143 Lecture 18 More Recursive Backtracking
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Java Software Solutions Foundations of Program Design Sixth Edition
CSC 205 – Java Programming II
Presentation transcript:

Swap I class Swap { public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void swap(String s1, String s2) { String temp = s1; s1 = s2; s2 = temp;

Swap II public static void swap(int[] data, int i, int j) { int temp= data[i]; data[i]= data[j]; data[j]= temp; }

Swap III public static void main(String[] args) { int a = 7, b=8; swap(a,b); System.out.println(a + " " + b); String s1 = "hihihi", s2 = "hahaha"; swap(s1,s2); System.out.println(s1 + " " + s2); int[] a = {0, 1 ,13, 32, 57}; swap(a, 1, 3); System.out.println(a[1] + " " + a[3]); }

Swap III public static void main(String[] args) { int a = 7, b=8; swap(a,b); System.out.println(a + " " + b); String s1 = "hihihi", s2 = "hahaha"; swap(s1,s2); System.out.println(s1 + " " + s2); int[] c = {0, 1 ,13, 32, 57}; swap(c, 1, 3); System.out.println(c[1] + " " + c[3]); }

Static fields I class NewTurtle { private static int counter = 0; private int id; NewTurtle() { ++counter; id = counter; } // // other methods

Static fields II parallelPaint() { tailDown(); turnLeft(id*(360/counter)); // counter > 0 (why ?) moveForward(100); } public static void main(String[] args) { NewTurtle[] turtles = new NewTurtle[6]; for (int i=0; i<turtles.length; i++) { turtles[i].parallelPaint();

Static fields II parallelPaint() { tailDown(); turnLeft(id*(360/counter)); // counter > 0 (why ?) moveForward(100); } public static void main(String[] args) { NewTurtle[] turtles = new NewTurtle[6]; for (int i=0; i<turtles.length; i++) { turtles[i] = new NewTurtle(); turtles[i].parallelPaint();

Finalize method Called when the object is destructed. System.gc() causes the JVM to do maximal effort to reclaim space from discarded objects ASAP For NewTurtle: finalize() { --counter; }

Objects as fields of other classes I class BigInt { boolean[] number; BigInt(boolean[] number) { this.number = number; } public static String toString() { String result = ""; for (int i = 0; i < number.length; i++) result += number[i] ? '1' : '0'; return result;

Objects as fields of other classes I class BigInt { boolean[] number; BigInt(boolean[] number) { this.number = number; } public String toString() { String result = ""; for (int i = 0; i < number.length; i++) result += number[i] ? '1' : '0'; return result;

Objects as fields of other classes II public static void main(String[] args) { boolean[] number = {false, false, false} BigInt zero = new BigInt(number); System.out.println(zero); number[2] = true; BigInt one = new BigInt(number); number[1] = true; BigInt three = new BigInt(number); System.out.println(one); System.out.println(three); }

Maze Traversal I We can use recursion to find a path through a maze From each location, we can search in each direction Recursion keeps track of the path through the maze The base case is an invalid move or reaching the final destination

Maze Traversal II public boolean traverse (int row, int column) { boolean done = false; if (valid (row, column)) // checks the cell is not blocked, not // visited and inside the matrix { grid[row][column] = TRIED; // this cell has been tried if (row == grid.length-1 && column == grid[0].length-1) done = true; // the maze is solved else { done = traverse (row+1, column); // down if (!done) done = traverse (row, column+1); // right

Maze Traversal III if (!done) done = traverse (row-1, column); // up done = traverse (row, column-1); // left } if (done) // this location is part of the final path grid[row][column] = PATH; return done;

Indirect Recursion A method invoking itself is considered to be direct recursion A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again For example, method m1 could invoke m2, which invokes m3, which in turn invokes m1 again This is called indirect recursion, and requires all the same care as direct recursion It is often more difficult to trace and debug

Indirect Recursion m1 m2 m3