Download presentation
Presentation is loading. Please wait.
1
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;
2
Swap II public static void swap(int[] data, int i, int j) {
int temp= data[i]; data[i]= data[j]; data[j]= temp; }
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[] a = {0, 1 ,13, 32, 57}; swap(a, 1, 3); System.out.println(a[1] + " " + a[3]); }
4
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]); }
5
Static fields I class NewTurtle { private static int counter = 0;
private int id; NewTurtle() { ++counter; id = counter; } // // other methods
6
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();
7
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();
8
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; }
9
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;
10
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;
11
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); }
12
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
13
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
14
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;
15
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
16
Indirect Recursion m1 m2 m3
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.