Chapter 1
Chapter 1
Chapter 1
Introduction
Next step:
x has five choices.
It is a linear list.
2 w
3 4 o f
a
a g
t
h d
s
g t
ADT and OO
1. Data type Definition: is a set of values together with a operation set that operate on these values. Example: int type value set: {,-2,-1,0,1,2,} operation set: {+, -, *, /, %, }
ADT and OO
Most of the programming languages provide a group of predefined data type. Atom data type int, float, double Structure data typearray, struct,
ADT and OO
2. ADTs: Abstract Data Types
Abstract: is a method used to hide the information.
Example: int x ; : x=735; : abstract of int data type assignment float x, y ; : x=x*y+3.0; : abstract of float data type operation
ADT and OO
Abstract data type: is a new programming method that part the usage and implementation, in order to encapsulate and hide the information.
ADT NaturalNunber is objects: , 0, (MAXINT) Function: Zero( ):NaturalNumber IsZero(x):Boolean Add(x,y):NaturalNumber Equal(x,y):Boolean Successor(x):NaturalNumber Subtract(x,y):NaturalNumber end NaturalNumber
ADT and OO
3. OO: object-orientedobjectclassinherit communicate object: attribute values+ operates class: objects of same attributes and operates. an instance is an object of the class. different object has deferent attribute value
ADT and OO
Inherit:
base classintegrate the same part (including attributes and operations) in all the derived classes derived classadd the specific attributes and operations Example: base classpolygon derived classquadrilateral,triangular
ADT and OO
Communication: each class object communicate with others using messages. Message: instructions that one class object used in order to require another class object to perform some operation.
Algorithm definition
Algorithm : an operation sequence of soluting a problem Characteristic: 1.finite 2. deterministic 3. initial action 4. sequence ends
Algorithm definition
Program : is written by languages that can be performed by machine. cant satisfy the finiteness. For example, OS. Algorithm: has multiple descriptive methods, such as language, graph, table.
Algorithm definition
void selectsort(int a[ ],int n) { for (int i = 0; i < n-1; i++) { int k = i; for ( int j = i+1; j < n; j++) if ( a[j] < a[k]) k = j; int temp = a[i]; a[i] = a[k]; a[k] = temp; } }
Mathematics Review
1. Exponents
XAXB = XA+B XA/XB = XA-B (XA)B = XAB XN + XN = 2XN 2N + 2N = 2N+1
Mathematics Review
2. Logarithms( all logarithms are to the base 2)
DEFINITION : XA = B if and only if logXB = A THEOREM 1.1 logAB = logCB/ logCA ; A, B, C > 0, A != 1 THEOREM 1.2 logAB = log A + log B; A, B > 0
i = 1 + 2 + 3++N = (N+1)*N/2
i=1
Mathematics Review
4. Modular Arithmetic
We say that A is congruent to B modulo N, written A B ( mod N), if N divides A-B. Example : 81 61 1 (mod 10)
Mathematics Review
5. The P Word ()
1). Proof by Induction The first step is proving a base case. the Next stap an inductive hypothesis is assumed. theorem is assumed to be true for all cases up to some limit k. Using this assumption, the theorem is then shown to be true for the next value, which is typically k + 1. This proves the theorem( as long as k is finite). Example: see book P6
Mathematics Review
2). Proof by Contradiction Proof by contradiction proceeds by assuming that the theorem is false and showing that this assumption implies that some known property is false, and hence the original assumption was erroneous. Example: proof that there is an infinite number of primes
2) indirect recursive Example 1 factorial function f(n)=n! 1 n<=1 (base) f(n) n*f(n-1) n>1 (recursive component)
f(5)=5f(4)=5*4f( 3)=5*4*3f(2)=5*4*3*2f(1)=120
static long factorial (int n) { if ( n <= 1) return 1; else return n* factorial( n-1 ) }
Compute fib(4):
public void moveDISKs(int n, char fromTower, char toTower, char auxTower) if ( n= =1) Move disk 1 from the fromTower to the toTower; else { moveDISKs(n-1, fromTower, auxTower, toTower); Move disk n from the fromTower to the toTower; moveDISKs(n-1, auxTower, toTower, fromTower); }
public class MemoryCell { public Object read( ) { return storedValue; } public void write( Object x ) { storedValue = x; } private Object storedValue; }
Exceptions
try { } catch(Exception e) { System.out.println (e); } main ( ) { . f ( ); . } f() { g(); .. . } g() {
Exceptions
Java
1.
error
error, error, error 1) error
2) error 0, 3) error
Exceptions
2. error () 1) (error)------OS (out of Memory) 2) (exception)------OS 0, 3. Java 1)
Exceptions
try { 1 // } catch( ) { 2 // } finally { 3 // }
public class Try2 { public static void main( string args[ ]) { int i = 0; int a[ ] = { 5,6,7,8}; for( i = 0; i < 5; i++) { try { system.out.print(a[ + i + ]/ + i + = + (a[i]/i)); } catch(ArrayIndexOutOfBoundsException e) { system.out.print(); } catch( ArithmeticException e) { system.out.print() } catch(Exception e) { system.out.print( + e.getMessage( ) + ! ); // } finally { system.out.println( i = +i ); } } //for system.out.println( } }
Exceptions
Exceptions
i = 0 a[1]/1 = 6 i = 1 a[2]/2 = 3 i = 2 a[3]/3 = 2 i = 3 i = 4
Exceptions
2) throw throwtry example public void set(int age) { if(age > 0 && age < 100 ) this.age = age ; else throw new Exaption ( IllegaAgeData ) ; // }
Exceptions
throws example public void removeAny( ) throws Underflow { if ( isEmpty( ) ) throw new Underflow( ); . }
Input and Output import java.io.* import java.utl.*; public class MaxTest { public static void main( String [ ] args ) { BufferedReader in = new BufferedReader ( new InputStreamReader( System.in ) ); String oneLine; StringTokenizer str; int x; int y; System.out.println( Enter 2 ints on one line: ); try { oneLine = in.readLine( ); str = new StringTokenizer( oneLine ); if( str . countTokens( ) != 2 ) throw new NumberFormatException( ); x = Integer . parseInt( str . nextToken( ) ); y = Integer . parseInt( str . nextToken( ) ); System.out.println( Max: + Math.max( x, y ) ) ; } catch ( Exception e ) { System.err.println( Error: need two ints ); } } }
FileReader
construct
--------> BufferedReader
Code Organization
1. Packages package DataStructures; Most of our classes will be found in a package named DataStructures. This includes the Overflow and Underflow exception classes.
Code Organization
2. The MyInteger Class public class MyInteger implements Comparable { public MyInteger( int x ) { value = x; } public int intValue( ) { return value; } public int compareTo( comparable rhs ) { return value < ( ( MyInteger ) rhs ) . value ? 1: value = = ( ( MyInteger ) rhs ) . value ? 0 : 1; } private int value; } 3 Efficiency Considerations
Chapter 1
Exercises: (English) P26: 1.5, 1.6 (Chinese) P22: 1.5, 1.6
a[n] 1a 2n
Chapter 1
:
1. 1, 2, , n r, . : n = 5 1 2 3 4 5 r=3 5 4 3 5 4 2 5 4 1 5 3 2 5 3 1 5 2 1 4 3 2 4 3 1 4 2 1 3 2 1 2. Hanoi
Generic Objects in Java program1.1 and 1.2 differ only in the data type of the formal parameters and of the value returned. We can write a generic code in which the data type is a variable whose value is determined by the compiler.This generic code is written using the template statement in program1.3
Generic Objects in Java Program1.3 template<class T> T Abc(T a,T b,T c) {return a+b+b*c+(a+b-c)/(a+b)+4; } From this generic code the compiler can construct 1.1 by substituting int for T and 1.2 by substituting float for T.