1 / 16

Swap I

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.

axel
Download Presentation

Swap I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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; } Finalize method

  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(zero); System.out.println(one); System.out.println(three); } }

  12. 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 I

  13. 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 II

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

  15. 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

  16. m1 m2 m3 m1 m2 m3 m1 m2 m3 Indirect Recursion

More Related