1 / 136

Data Structures

Data Structures. Dr. Cong-Cong Xing Dept of Mathematics and Computer Science. Review of Java Basics. The Java Environment . Java source file. Java compiler. Byte code can be disseminated to other machines. It is the “machine code” for JVM. Byte code file.

tomas
Download Presentation

Data Structures

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. Data Structures Dr. Cong-Cong Xing Dept of Mathematics and Computer Science

  2. Review of Java Basics

  3. The Java Environment Java source file Java compiler Byte code can be disseminated to other machines. It is the “machine code” for JVM Byte code file JVM = Java Virtual Machine (a software “computer”) JVM for your platform

  4. The Basic Program Structure public class MyProg { …… // static variables public static void main (String[] args) { … … // computations, calls } ….. public static …. // method 1 public static …. // method 2 } Execution starts here

  5. The “hello, world” (again) public class Lab1 { public static void main(String args[]) { // printout the message System.out.println(“Hello, World!"); // terminate the program System.exit(0); } // end of main } // end of class Lab1

  6. Execution under Dr. Java

  7. Input/output using JOptionPane import javax.swing.JOptionPane;

  8. Primitive Data Types

  9. Operators high

  10. Java Control Statements • Sequence and compound statements • { • ….. • stmnt1; • stmnt2; • stmnt3; • … • } { and } marks the begin and end ; is used to separate individual stament

  11. Selection • if … else… statement if (x > 1) { x = x+10;} else { x = x-10;}

  12. switch (case) statement switch (num) { case 1: System.out.println(“It’s 1”); break; case 2: System.out.println(“It’s 2”); break; case 3: case 4: case 5: System.out.println(“It’s 3 or 4 or 5”); break; default: System.out.println(“Invalid data”); break; }

  13. Loops • while-loop while (counter <= 5) { System.out.print(counter); counter = counter + 1; }

  14. for-loop for (i=1; i<=10; i++) { System.out.println(“this is”+i+”pass”); System.out.println(“i is “+i); }

  15. Methods • Ideas involved • functions, procedures, operations, subprograms, modular programming, abstract data types (ADT) • static (class) methods: the rest ideas • non-static (object) methods: the idea of ADT

  16. static methods return val type name of method parameter name public static intsq (int x) { return x*x; } …. what is returned parameter type

  17. To call/invoke a method Just like you call a math function: Method-name (argument) Ex: sq(1); a = sq (1); System.out.print(sq(1));

  18. Structure of class w/ static methods public class class-name { public static … method1(…) { … } public static …. method2(…) { …. } ………………. pubic static void main(String args[]) { ….. } } call call

  19. The class Math • Built-in class • Includes all commonly used math functions: • Math.sin • Math.cos • Math.log (natural log) • Math.sqrt • Math.PI ….. (see p 615 for more)

  20. Arrays let this array (crated by the right) be referenced/represented by variable a • 1-D array int[] a = new int[10]; declare variable a to be of type int array create an int array of 10 cells in memory

  21. graphically, int[] a = new int[10]; a a[0] a[1] a[2] a[8] a[9]

  22. 2-D arrays • similar to 1-D array • actually, 1-D array of 1-D arrays int[][] matrix = new int[3][4]; // a 3x4 (3 rows, 4 columns) int array referenced by variable matrix

  23. graphically, int[][] a = new int[3][4];

  24. int [] : 1-D array, elements are of type int int [][] : 1-D array, elements are of type int [] int [][] int [] int [] int []

  25. Review of Structure Chart

  26. Overview • Design of program: structure chart (style) • How to read the chart? • from top to bottom; from left to right,

  27. if statement if ( grade >= 60 ) { System.out.println(“you passed”); System.out.println(“good work”); } Its structure chart: grade>=60 T print “you passed” print “good work”

  28. if-else statement • Its structure chart grade>=60 if (grade >=60) { System.out.println(“you passed”); System.out.println(“congratulations!”); } else { System.out.println(“you failed”); System.out.println(“work harder!”); } F T print “you passed” print “you failed” print “congratulations” print “work harder”

  29. Cont’d if (x==0) { System.out.println(“zero”); } else { if (x>0) { System.out.println(“positive”); } else { System.out.println(“negative”); } } X=0? T F zero X>0? T F neg pos

  30. Cont’d if (x==0) System.out.println(“zero”); if (x>0) System.out.println(“positive”); if (x<0) System.out.println(“negative”); X=0? X>0? X<0? T T T zero pos neg

  31. Switch statement switch (expression) { case label1: stmnt1; break; case label2: stmnt2; break; …… case labeln: stmntn; break; default: default stmnt; break; }

  32. Cont’d • Structure chart Switch() exp=ln exp=l3 exp=l1 exp=l2 stmnt1 stmnt2 stmnt3 stmntn

  33. while-loop while (cond) { stmnt1; ….. stmntn; } cond stmntn stmnt1 stmnt2

  34. example • Design: (note how the • loop is represented) while grd !=9999

  35. for-loop For i=1 to 10,i++ for (i=1;i<=10;i++) { System.out.println(i); } Print i

  36. Println() Print”*” Nested loop for (i=1; i<=5; i++) { for (j=1; j<=6-i; j++) { System.out.print(“*”); } System.out.prrintln(); } For i=1,i<=5,i++ For j=1;j<=6-i;j++

  37. Function calls (modular programming) • Problem: Given a positive integer n, compute 1+2+…+n • Requirements: Users input a positive integer n, the program computes the sum from 1 to n. E.g., if n=1, output = 1 if n=2, output = 1+2=3 if n=10, output = 1+2+…+10=55 • Specification: • Input: positive integer n • Output: 1+2+…+n • Relevant formula/methodology: modules, loops

  38. For each method, make its own structure chart • Method getN: • purpose: It takes nothing and returns the positive integer entered by the user • type: void  int • data structure getN() sn=JOpt… convert sn to n return n

  39. Cont’d • Method doComp: • * purpose: computes 1+…+n and returns the result. • * type: int int • * data structure doComp(int n) for i=1 to n sum=0 return sum sum=sum+i

  40. Cont’d • Method display: • purpose: display the result • type: int void • data structure display(int r) print r

  41. Method main Main(String args[]) Num= getN() result= doComp(num) display (result) end

  42. Code sketch (lab?) import javax.swing.*; public class Lab { // put method getNhere // put method doComphere // put method dispalyhere // put method main here }

  43. End of Review of Java basics

  44. Chapter 5 Recursion

  45. 5.1 Introduction to Recursion

  46. Idea of Recursive Thinking Copy of itself

  47. Recursive Thinking • What is recursion? • Solve a (large) problem by solving subproblems that have the same nature as the original problem. • In terms of programming, a function calls itself. ………f(n)…. { …f(n-1)…. }

  48. Recursion in math • In term of functions, solve f(n) by solving typically f(n-1). • Same idea as mathematical induction (Math 358). • Well-known example: factorial • n!=n*(n-1)! Or • fac(n) = n*fac(n-1)

More Related