Swap I - PowerPoint PPT Presentation

About This Presentation
Title:

Swap I

Description:

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; – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 17
Provided by: acil150
Category:
Tags: asap | method | swap

less

Transcript and Presenter's Notes

Title: Swap I


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 datai
  • datai dataj
  • dataj temp

3
Swap III
  • public static void main(String args)
  • int a 7, b8
  • 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(a1 " " a3)

4
Swap III
  • public static void main(String args)
  • int a 7, b8
  • 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(c1 " " c3)

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 gt 0 (why
    ?)
  • moveForward(100)
  • public static void main(String args)
  • NewTurtle turtles new NewTurtle6
  • for (int i0 iltturtles.length i)
  • turtlesi.parallelPaint()

7
Static fields II
  • parallelPaint()
  • tailDown()
  • turnLeft(id(360/counter)) // counter gt 0 (why
    ?)
  • moveForward(100)
  • public static void main(String args)
  • NewTurtle turtles new NewTurtle6
  • for (int i0 iltturtles.length i)
  • turtlesi new NewTurtle()
  • turtlesi.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 lt number.length i)
  • result numberi ? '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 lt number.length i)
  • result numberi ? '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)
  • number2 true
  • BigInt one new BigInt(number)
  • number1 true
  • BigInt three new BigInt(number)
  • System.out.println(zero)
  • 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
  • gridrowcolumn TRIED // this cell
    has been tried
  • if (row grid.length-1 column
    grid0.length-1)
  • done true // the maze is solved
  • else
  • done traverse (row1, column)
    // down
  • if (!done)
  • done traverse (row, column1)
    // right

14
Maze Traversal III
  • 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
  • gridrowcolumn 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
Write a Comment
User Comments (0)
About PowerShow.com