1 / 28

CS1101 Group1

CS1101 Group1. Discussion 5. Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101. Debugging in Dr Java. Checklist Setting a breakpoint Before running program During the debugging process Step into a method Watches. Debugging in Dr Java.

zev
Download Presentation

CS1101 Group1

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. CS1101 Group1 Discussion 5 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101

  2. Debugging in Dr Java Checklist • Setting a breakpoint • Before running program • During the debugging process • Step into a method • Watches

  3. Debugging in Dr Java public class Dummy{ public static void main(String[] args){ int tmp = 0; for (int i = 0 ; i < 10 ; i++){ tmp++; } dummyMethod(); } public static void dummyMethod(){ System.out.println("do nothing1"); System.out.println("do nothing2"); System.out.println("do nothing3"); } } • Ctrl+B (toggle breakpoints at current line) • F7 (Resume/Continue Run) • F11 (Step Over) • F12 (Step Into) • Shift + F12 (Step Out)

  4. Search And Sorting

  5. Searching • Linear Search • Go through all the items in an array • Binary Search • Search by “cutting” the possible solutions by ½ each time

  6. Sorting • Bubble Sort • Insertion Sort • Selection Sort

  7. Stable Sorts • A stable sort is one where the relative ordering of elements with the same value is preserved after sorting. • Two elements having the same key appear in the same order in the sorted sequence as they did in the original sequence. • Example: • Stable sort: Before sorting: 6 2 3a 8 3b 5 9 After sorting: 2 3a 3b 5 6 8 9 • Unstable sort: Before sorting: 6 2 3a 8 3b 5 9 After sorting: 2 3b 3a 5 6 8 9 • Which of the three basic sorts – bubble sort, selection sort, insertion sort – is/are stable? Slide taken from cs1101x lecture notes

  8. Sorting Algorithms Comparison

  9. Sorting Algorithms Comparison

  10. Sorting Question Which of the 3 algorithms is best for data that is almost sorted?

  11. Recursion

  12. Recursion • Base case • Recursive case n! = n * (n-1) * (n-2) * … * 2 * 1 for n > 0 (recursive case) 0! = 1 (base case) int fac(int n){ if (n == 0) return 1; else{ return n * fac(n-1); } }

  13. Recursion • General approaches • Think recursive • Split the problem into simple cases • Work in the opposite way • Work from a base case • See what you need to do for the 2nd last case • etc

  14. Recursion Practice • Can you write a “while” method that does what the while loop is doing

  15. Recursion Practice • Can you write a “while” method that does what the while loop is doingpublic static void While (int i){ if (cond){ //code of while body While(i); } }

  16. Recursion Practice while(i > 0){ System.out.println(i); i--; } public static void While (int i){ if (i > 0 ){ System.out.println(i); i--; While(i); } }

  17. References • Pass by Value • Java is always pass by value! • Pass by Reference

  18. Pass by Value public static void main (String args[]){ //a is an Integer Object //you can just think of it as a //integer value Integer a = new Integer(13); addOne(a); System.out.println(a); } public static void addOne(Integer number){ number = new Integer(number.intValue() + 1); System.out.println("number inside the method : " + number); } What do you think is the output?

  19. Pass by Value public static void main (String args[]){ //a is an Integer Object //you can just think of it as a //integer value Integer a = new Integer(13); addOne(a); System.out.println(a); } public static void addOne(Integer number){ number = new Integer(number.intValue() + 1); System.out.println("number inside the method : " + number); } What do you think is the output? number inside the method : 14 13

  20. Pass by Value public static void main (String args[]){ Integer a = new Integer(13); addOne(a); System.out.println(a); } public static void addOne(Integer number){…} What is really happening on the JVM? 13 0x0010 The value of a is at memory location 0x0010 Note that this is really a simplified view of what’s happening

  21. Pass by Value public static void main (String args[]){ Integer a = new Integer(13); addOne(a); System.out.println(a); } public static void addOne(Integer number){…} What is really happening on the JVM? To find the value of a, just need to go to memory location 0x0010 This information is stored at memory location 0x0020 13 0x0010 0x0010 a 0x0020

  22. Pass by Value public static void main (String args[]){ Integer a = new Integer(13); addOne(a); System.out.println(a); } public static void addOne(Integer number){…} What is really happening on the JVM? What happens when you call a method is that another box with the value 0x0010 is passed in (both are pointing to 0x0010) What this means is that you can, only change the contents at 0x0010, but you cannot define a new totally new object for a (You are allowed to modify 0x0010) 13 0x0010 addOne(a) 0x0010 a 0x0020 0x0010 Copy of a 0x0030

  23. Pass by Reference In Pass by Reference, what happens call a method is that the first box on the left is passed to the method(0x0010) What this means is that you can, change the contents at 0x0010, and you can also give new pointer for Integer a (You are allowed to modify both 0x0010, and 0x0020) public static void main (String args[]){ Integer a = new Integer(13); addOne(a); System.out.println(a); } public static void addOne(Integer number){…} What is really happening on the JVM? 13 0x0010 addOne(a) 0x0010 a 0x0020

  24. Java (Pass by value) • When you are passing primitive data types to methods (int, byte, double etc), you can think of it as you are giving the method the numerical value of the variable, not the variable itself

  25. Java (Pass by value) • When you are passing in references… • Objects, Arrays* • Can really think of Arrays are Objects • You are given access to the content of that Object (but cannot modify the container)

  26. arrcontents 0x0010 0x0010 arr 0x0020 0x0010 Copy of arr 0x0030 Java (Pass by value) int[] arr = new int[10]; method(arr); public static void method(int[] a){ a = new int[20]; }

  27. Java (Pass by value) int[] arr = new int[10]; method(arr); public static void method(int[] a){ a = new int[20]; } new array arrcontents 0x0010 0x0010 arr 0x0020 0x0050 Copy of arr 0x0030

  28. arrcontents 0x0010 0x0010 arr 0x0020 Java (Pass by value) int[] arr = new int[10]; method(arr); public static void method(int[] a){ a = new int[20]; } When you comes out of the method, you don’t really have the previous box and 0x0050

More Related