1.37k likes | 1.55k Views
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.
E N D
Data Structures Dr. Cong-Cong Xing Dept of Mathematics and Computer Science
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
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
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
Input/output using JOptionPane import javax.swing.JOptionPane;
Operators high
Java Control Statements • Sequence and compound statements • { • ….. • stmnt1; • stmnt2; • stmnt3; • … • } { and } marks the begin and end ; is used to separate individual stament
Selection • if … else… statement if (x > 1) { x = x+10;} else { x = x-10;}
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; }
Loops • while-loop while (counter <= 5) { System.out.print(counter); counter = counter + 1; }
for-loop for (i=1; i<=10; i++) { System.out.println(“this is”+i+”pass”); System.out.println(“i is “+i); }
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
static methods return val type name of method parameter name public static intsq (int x) { return x*x; } …. what is returned parameter type
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));
Structure of class w/ static methods public class class-name { public static … method1(…) { … } public static …. method2(…) { …. } ………………. pubic static void main(String args[]) { ….. } } call call
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)
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
graphically, int[] a = new int[10]; a a[0] a[1] a[2] a[8] a[9]
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
graphically, int[][] a = new int[3][4];
int [] : 1-D array, elements are of type int int [][] : 1-D array, elements are of type int [] int [][] int [] int [] int []
Overview • Design of program: structure chart (style) • How to read the chart? • from top to bottom; from left to right,
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”
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”
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
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
Switch statement switch (expression) { case label1: stmnt1; break; case label2: stmnt2; break; …… case labeln: stmntn; break; default: default stmnt; break; }
Cont’d • Structure chart Switch() exp=ln exp=l3 exp=l1 exp=l2 stmnt1 stmnt2 stmnt3 stmntn
while-loop while (cond) { stmnt1; ….. stmntn; } cond stmntn stmnt1 stmnt2
example • Design: (note how the • loop is represented) while grd !=9999
for-loop For i=1 to 10,i++ for (i=1;i<=10;i++) { System.out.println(i); } Print i
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++
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
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
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
Cont’d • Method display: • purpose: display the result • type: int void • data structure display(int r) print r
Method main Main(String args[]) Num= getN() result= doComp(num) display (result) end
Code sketch (lab?) import javax.swing.*; public class Lab { // put method getNhere // put method doComphere // put method dispalyhere // put method main here }
Idea of Recursive Thinking Copy of itself
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)…. }
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)